1.10.4 스캐닝을 커스터마이징 하기위해 필터를 사용

기본적으로 @Component, @Repository, @Service, @Controller, 또는 @Component 와 함께 사용자 anntation이 달린 annotation 된 클랙스는 검색된 후보 구성 요소입니다. 그러나 사용자 지정 필터를 적용하여 이 동작을 수정하고 확장 할 수 있습니다. @ComponentScan의 annotation 인 includeFilters또는 excludeFilters 의 매개변수로 추가하세요. (또는component-scan 의 하위요소 include-filterexclude-filter ). 각 필터 요소에는 typeexpression이 필요합니다. 다음 표에서는 필터링 옵션에 대해 설명합니다.

표 5. 필터 유형

필터 유형

표현식의 예

기술

annotation (기본값)

org.example.SomeAnnotation

타겟 컴퍼넌트의 타입 레벨에서 표현는 annotation.

assingable

org.example.SomeClass

대상 구성 요소들을 할당 (확장 또는 구현) 될 수있는 클래스 (또는 인터페이스)입니다.

aspectj

org.example..*Service+

타겟 컴포넌트에 의해 매치 될 AspectJ 타입 표현식.

regex(정규식)

org\.example\.Default.*

대상 구성 요소 클래스 이름과 일치하는 정규 표현식.

custom(관습)

org.example.MyTypeFilter

org.springframework.core.type .TypeFilter인터페이스를 구현한 커스텀 구현 .

다음 예제는 모든 @Repositoryannotation을 무시하고 대신 "stub" 레파지토리를 대신 사용 하는 설을 보여줍니다 .

@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>

<component-scan/> ㅇ요소의 속성으로 useDefaultFilters=false주석 을 설정 하거나use-default-filters="false"의 속성으로 제공 하여 기본 필터를 비활성화 할 수도 있습니다. 이것은 사실상 @Component,@Repository, @Service, @Controller, 또는 @Configurationannotation이 붙은 클래스들 자동으로 감지하지 않도록 합니다..

Last updated