26

I have several services:

  • example.MailService
  • example.LDAPService
  • example.SQLService
  • example.WebService
  • example.ExcelService

annotated with @Service annotation. How can I exclude all services except one?


For example I want to use only MailService. I use the following configuration:

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

but now all services are excluded.

Why all services are excluded if exists one rule to include MailService?

informatik01
  • 16,038
  • 10
  • 74
  • 104

3 Answers3

34

Another way to perform this registration is with a single inclusion filter.

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

The "use-default-filters" attribute must be set to "false" in this case to keep Spring from adding a default filter equivalent to

<context:include-filter type="annotation" 
                        expression="org.springframework.stereotype.Component"/>
David
  • 1,261
  • 1
  • 9
  • 10
  • 11
    +1 - setting `use-default-filters="false"` is key to preventing scan of other components in the same base-package. Also works great with regex. – lreeder Jun 24 '13 at 22:19
14

Include filters are applied after exclude filters, so you have to combine both expressions into one exclude filter. AspectJ expressions allow it (& is replaced by &amp; due to XML syntax):

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

This is a regex, so your expression ".*Service" means 'any number of any character followed by "Service"'. This explicitly excludes the MailService you want to include.

Community
  • 1
  • 1
axtavt
  • 239,438
  • 41
  • 511
  • 482
  • what do you mean "Include filters are applied after exclude filters" ? that exclude filter is interpreted first and include filter then allows those types despite they are defined in the exclude filter ? That totally doesn't work for me – lisak Jan 03 '11 at 11:50
  • 3
    @lisak: In more clear words: include filter can't include types excluded by the exclude filter. – axtavt Jan 03 '11 at 12:48
  • would you be please so kind and look at this http://stackoverflow.com/q/4584509/306488 ? It makes me crazy... – lisak Jan 03 '11 at 12:52
11

It looks like you want to use filter type "regex". Here's an example from the 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>
earldouglas
  • 13,265
  • 5
  • 41
  • 50
  • If you want to include just one Type in the component scan the regex filter type is the easiest way to do that. – Speck Nov 03 '11 at 19:29