복붙노트

[SPRING] <context : include-filter>와 <context : exclude-filter>는 Spring에서 어떻게 작동합니까?

SPRING

는 Spring에서 어떻게 작동합니까?

몇 가지 서비스가 있습니다.

@Service 주석이 주석 처리됩니다. 어떻게 하나를 제외한 모든 서비스를 제외 할 수 있습니까?

예를 들어 MailService 만 사용하려고합니다. 다음 구성을 사용합니다.

<context:component-scan base-package="example">
    <context:include-filter type="aspectj" expression="example..MailService*" />
    <context:exclude-filter type="aspectj" expression="example..*Service*" />
</context:component-scan>

그러나 지금 모든 서비스는 제외됩니다.

MailService를 포함하는 규칙이 하나라도있는 경우 모든 서비스가 제외되는 이유는 무엇입니까?

해결법

  1. ==============================

    1.포함 필터는 제외 필터 다음에 적용되므로 두 표현식을 하나의 제외 필터로 결합해야합니다. AspectJ 표현식은 그것을 허용한다 (XML 구문 때문에 &로 대체된다) :

    포함 필터는 제외 필터 다음에 적용되므로 두 표현식을 하나의 제외 필터로 결합해야합니다. AspectJ 표현식은 그것을 허용한다 (XML 구문 때문에 &로 대체된다) :

    <context:exclude-filter type="aspectj" 
        expression="example..*Service* &amp;&amp; !example..MailService*" />
    

    이것은 정규식이므로 표현식 ". * 서비스"는 "임의의 문자와 그 뒤에"서비스 "가 오는 숫자를 의미합니다. 이것은 명시 적으로 포함시키고 자하는 MailService를 제외시킵니다.

  2. ==============================

    2.이 등록을 수행하는 또 다른 방법은 단일 포함 필터를 사용하는 것입니다.

    이 등록을 수행하는 또 다른 방법은 단일 포함 필터를 사용하는 것입니다.

    <context:component-scan base-package="example" use-default-filters="false">
        <context:include-filter type="aspectj" expression="example..MailService*" />
    </context:component-scan>
    

    이 경우 스프링에 "default-filters"속성을 추가하지 않도록 "use-default-filters"속성을 "false"로 설정해야합니다.

    <context:include-filter type="annotation" 
                            expression="org.springframework.stereotype.Component"/>
    
  3. ==============================

    3.필터 유형 "regex"를 사용하려는 것 같습니다. 다음은 Spring Reference의 예제이다.

    필터 유형 "regex"를 사용하려는 것 같습니다. 다음은 Spring Reference의 예제이다.

    <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>
    
  4. from https://stackoverflow.com/questions/3710444/how-do-contextinclude-filter-and-contextexclude-filter-work-in-spring by cc-by-sa and MIT license