Cichy
Cichy

Reputation: 1329

Spring component scan custom filter

I got package structure like:

(...)
com.domain.group.dao
com.domain.group.service
(...)
com.domain.users.dao
com.domain.users.service

Is there any way to tell the spring that should scan just all the classes which package ends with "dao" or "service" ?

here's example of similar problem, but it doesn't resolve my problem.

Upvotes: 3

Views: 6142

Answers (2)

Ralph
Ralph

Reputation: 120841

 <context:component-scan base-package="com.domain">
     <context:include-filter type="regex" expression=".*dao\.[^.]*"/>
     <context:include-filter type="regex" expression=".*service\.[^.]*"/>
 </context:component-scan>

@See Spring Reference Chapter 4.10.3 Using filters to customize scanning


Appendix: (because of the comments)

It is possible to have multiple include-filters and exclude-filters but the order is important: first include-filter then exclude-filter- see spring-context-3.0.xsd:

<xsd:element name="component-scan">
    ...
    <xsd:complexType>
        <xsd:sequence>
            <xsd:element name="include-filter" type="filterType" minOccurs="0" maxOccurs="unbounded">
                ...
            </xsd:element>
            <xsd:element name="exclude-filter" type="filterType" minOccurs="0" maxOccurs="unbounded">
                ...
            </xsd:element>
        </xsd:sequence>
        ...
    </xsd:complexType>
</xsd:element>

Upvotes: 5

Paul Whelan
Paul Whelan

Reputation: 16809

  <context:component-scan base-package="org.example">
     <context:include-filter type="regex" expression="YOUR REGEX"/>
  </context:component-scan>

PS: you can have multiple include-filters one for service regex and one for dao

This is taken from the Using filters to customize scanning section of the Spring reference guide.

Upvotes: 1

Related Questions