본문 바로가기
백엔드/Spring(Boot)

컴포넌트 스캔 필터 (@ComponentScan)

by 김어찐 2021. 8. 12.
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

'백엔드 > Spring(Boot)' 카테고리의 다른 글

Spring 클래스 초기화, 종료  (0) 2021.08.16
@Autowired 옵션  (0) 2021.08.13
@ComponentScan  (0) 2021.08.12
ApplicationContext란  (0) 2021.08.10
Bean 조회  (0) 2021.08.10