• Home
  • About
    • Junseok photo

      Junseok

      개발자 블로그

    • Learn More
    • Facebook
    • Instagram
    • Github
  • Posts
    • All Posts
    • All Tags
  • Java
    • java-basic
    • java-solid
    • java-pattern
    • java-logging
  • Javascript
  • Angular
  • spring
    • spring-framework
    • spring-boot
    • spring-test
  • server
    • jeus
    • webtob
    • tomcat
  • test
    • junit
    • assertj
    • hamcrest
    • dbunit
    • spring
  • docker
  • unix
  • maven
  • db
  • network
  • eclipse
  • intellij
  • microservices
  • etc

ComponentScan 애너테이션으로 자동 검색

11 Mar 2018

Reading time ~1 minute

기본적으로 @Component, @Repository, @Service, @Controller 또는 @Component로 어노테이션이 달린 클래스만이 검색된 후보 구성 요소입니다.

그러나 사용자 지정 필터를 적용하여 이 동작을 수정하고 확장 할 수 있습니다.
@ComponentScan 어노테이션의 includeFilters 또는 excludeFilters 매개 변수 (또는 component-scan 요소의 include-filter 또는 exclude-filter 하위 요소)로 추가하십시오.

각 필터 요소에는 유형 및 표현식 속성이 필요합니다.
다음 표에서는 필터링 옵션에 대해 설명합니다.

Filter Types

Filter Type Example Expression(표현식의 예) Description
annotation (default) org.example.SomeAnnotation 타겟 컴퍼넌트의 타입 레벨에 존재하는 어노테이션.
assignable org.example.SomeClass 대상 구성 요소가 할당 (확장 / 구현) 될 수있는 클래스 (또는 인터페이스)입니다.
aspectj org.example..*Service+ 타겟 컴포넌트에 의해 매치 될 AspectJ 타입 표현식.
regex org.example.Default.* 대상 구성 요소 클래스 이름과 일치하는 정규식입니다.
custom org.example.MyTypeFilter org.springframework.core.type.TypeFilter 인터페이스의 커스텀 구현입니다.

다음 예는 @Repository 어노테이션을 모두 무시하고 대신 “stub” repositories를 사용하는 구성을 보여줍니다.

@Configuration
@ComponentScan(basePackages = "org.example",
        includeFilters = @Filter(type = FilterType.REGEX, pattern = ".*Stub.*Repository"),
        excludeFilters = @Filter(Repository.class))
public class AppConfig {
    ...
}

XML을 사용 위와 동일

<beans>
    <context:component-scan base-package="org.example">
        <context:include-filter type="regex"
                expression=".*Stub.*Repository"/>
        <context:exclude-filter type="annotation"
                expression="org.springframework.stereotype.Repository"/>
    </context:component-scan>
</beans>

어노테이션에서 useDefaultFilters = false를 설정하거나
use-default-filters = "false"를 <component-scan /> 요소의 속성으로 제공하여
기본 필터를 비활성화 할 수도 있습니다.

이것은 실제로 @Component, @Repository, @Service, @Controller, 또는
@Configuration으로 어노테이션된 클래스의 자동 검출을 무효로 합니다.



springframeworkannotation Share Tweet +1