본문 바로가기

SPRING/Spring

[스프링| 스프링 핵심 원리 | 기본편 | 컴포넌트 스캔] 필터, 중복등록과 충돌

 

package hello.core.scan.filter;
 import org.junit.jupiter.api.Assertions;
 import org.junit.jupiter.api.Test;
 import org.springframework.beans.factory.NoSuchBeanDefinitionException;
 import org.springframework.context.ApplicationContext;
 import
 org.springframework.context.annotation.AnnotationConfigApplicationContext;
 import org.springframework.context.annotation.ComponentScan;
 import org.springframework.context.annotation.Configuration;
 import org.springframework.context.annotation.FilterType;
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.springframework.context.annotation.ComponentScan.Filter;
 public class ComponentFilterAppConfigTest {
     @Test
     void filterScan() {
         ApplicationContext ac = new
 AnnotationConfigApplicationContext(ComponentFilterAppConfig.class);
         BeanA beanA = ac.getBean("beanA", BeanA.class);
         assertThat(beanA).isNotNull();
         Assertions.assertThrows(
                 NoSuchBeanDefinitionException.class,
                 () -> ac.getBean("beanB", BeanB.class));
}
     @Configuration
     @ComponentScan(
     includeFilters = @Filter(type = FilterType.ANNOTATION, classes =
MyIncludeComponent.class),
            excludeFilters = @Filter(type = FilterType.ANNOTATION, classes =
MyExcludeComponent.class)
    )
    static class ComponentFilterAppConfig {
    }
}

@ComponentScan(
  includeFilters = @Filter(type = FilterType.ANNOTATION, classes =
MyIncludeComponent.class),
  excludeFilters = @Filter(type = FilterType.ANNOTATION, classes =
MyExcludeComponent.class)
)