본문 바로가기
Spring

Java Configuration

by Coarti 2023. 8. 7.

[ 이전 글 ] @Component 분석 ( 3 )

 

@Component 분석 ( 3 )

@Conponent의 대상은 크게 3가지로 다음과 같다 @Controller 사용자 입출력 @Service 사용자 요청에 맞게 데이터를 제공 @Repository ( DAO ) 데이터에 접근하여 Service에 전달 MVC패턴으로 웹 어플리케이션을 만

cloakinghost.tistory.com

<context:component-scan base-package="spring.di.ui"/>
<bean id="exam" class="spring.di.entity.ScoreExam" p:kor="10"  p:eng="20"/>

XML의 내용을 아래와 같이 바꿀 수 있다

package spring.di;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import spring.di.entity.Exam;
import spring.di.entity.ScoreExam;

@ComponentScan("spring.di.ui")
@Configuration
public class DIConfig {
	@Bean
	public Exam exam() {
		return new ScoreExam(); // IoC Container에 담김
	}
}

main() 부분도 변경하자

//		ApplicationContext context = new ClassPathXmlApplicationContext("spring/di/setting.xml");

	        // 첫번째 방법
//		ApplicationContext context = new AnnotationConfigApplicationContext(DIConfig.class);

		// 두번째 방법
		AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
		context.register(DIConfig.class, OtherConfig.class); // 1개도 가능
		context.refresh();
728x90

'Spring' 카테고리의 다른 글

Around Advice ( ver. Java Project )  (0) 2023.08.07
Aspect Oriented Programming  (0) 2023.08.07
@Component 분석 ( 3 )  (0) 2023.08.07
@Component 사용법 ( 2 )  (0) 2023.08.07
@Component 사용법 ( 1 )  (0) 2023.08.05