Study Record/Spring Core

[Core] @ComponentScan

고고잉93 2024. 4. 29. 16:57
728x90

Appconfig에서 일일이 @Bean을 달기 힘들다 → @ComponentScan 해결

 

@ComponentScan 

  ● ComponentScan은 @Component가 붙은 모든 Class를 스프링 빈으로 등록한다.

  ● @Component 뿐만 아니라 @Service, @Controller, @Repository, 등이 있다.

     - @Controller : 스프링 MVC컨트롤러로 인식

     - @Repository : 데이터 접근 계층으로 인식.

     - @Service : 비즈니츠 계층 인식용( 딱히 기능이 없다고 한다.)

 

  ● Filter

    includeFilters, excludeFilters를 이용하여 컴포넌트스캔에 추가할 혹은 제외할 대상을 지정할 수 있다.

@Configuration
@ComponentScan( includeFilters = @ComponentScan.Filter(type = FilterType.ANNOTATION, classes =
            						MyIncludeComponent.class),
            excludeFilters = @ComponentScan.Filter(type = FilterType.ANNOTATION, classes =
                    MyExcludeComponent.class)
    )
    static class ComponentFilterAppConfig {
    }
더보기
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MyExcludeComponent { // ComponentScan에서 제외
}

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MyIncludeComponent { // ComponentScan에 추가.
}

@MyIncludeComponent  //  ←← 어노테이션 기입
public class BeanA {
}

@MyExcludeComponent //  ←← 어노테이션 기입
public class BeanB {
}

 

728x90

'Study Record > Spring Core' 카테고리의 다른 글

[Lecture-Core] Singleton  (0) 2024.04.27