백엔드/Spring(Boot)
컴포넌트 스캔 필터 (@ComponentScan)
김어찐
2021. 8. 12. 22:28
728x90
includeFilters : 컴포넌트 대상에 포함
excludeFilters : 컴포넌트 대상에 제외
컴포넌트 스캔 테스트
public class ComponentFilterAppConfigTest {
@Test
void filterScan()
{
ApplicationContext ac = new AnnotationConfigApplicationContext(ComponentFilterAppConfig.class);
BeanA beanA = ac.getBean("beanA", BeanA.class);
Assertions.assertThat(beanA).isNotNull();
org.junit.jupiter.api.Assertions.assertThrows(
NoSuchBeanDefinitionException.class, () -> ac.getBean("beanB", BeanB.class)
);
}
@ComponentScan(
includeFilters = @ComponentScan.Filter(type = FilterType.ANNOTATION, classes = MyIncludeComponent.class),
excludeFilters = @ComponentScan.Filter(type = FilterType.ANNOTATION, classes = MyExcludeComponent.class)
)
static class ComponentFilterAppConfig{
}
}
BeanA
package hello.core.scan.filter;
@MyIncludeComponent
public class BeanA {
}
BeanB
package hello.core.scan.filter;
@MyExcludeComponent
public class BeanB {
}728x90