하나의 설정 클래스에서 다른 설정 클래스를 추가하는 방법이다. 설정 클래스들을 그룹지을 수 있어 편리해진다.
예를 들어, 설정 파일 AppConf1, AppConf2가 있다고 하자.
@Configuration
public class AppConf1 {
@Bean
public MemberDao memberDao() {
return new MemberDao();
}
@Bean
public MemberPrinter memberPrinter() {
return new MemberPrinter();
}
}
@Configuration
public class AppConf2 {
@Bean
public MemberInfoPrinter memberInfoPrinter() {
return new MemberInfoPrinter();
}
}
이 두 가지의 설정 클래스를 모두 사용하기 위해서는 아래와 같이 AnnotationConfigApplicationContext에 전달할 수도 있다.
ctx = new AnnotationConfigApplicationContext(AppConf1.class, AppConf2.class);
하지만 사용해야 할 설정 클래스들이 많아진다면 클래스를 모두 나열하는 위의 방법은 불편하다.
만약 AppConf1 파일에 @Import(AppConf2.class)를 추가한다면,
@Configuration
// Import annotation 추가
@Import(AppConf2.class)
public class AppConf1 {
@Bean
public MemberDao memberDao() {
return new MemberDao();
}
@Bean
public MemberPrinter memberPrinter() {
return new MemberPrinter();
}
}
AnnotationConfigApplicationContext에는 AppConf1만 전달하여도 AppConf2 설정 클래스를 사용할 수 있게 된다.
ctx = new AnnotationConfigApplicationContext(AppConf1.class);
Import된 설정 파일에서 또 다시 Import도 가능하기 때문에 설정파일이 굉장히 많아졌을 때 관리하기 편하다!
'SPRING' 카테고리의 다른 글
Spring Qualifier Annotation (0) | 2021.04.12 |
---|