3

我的包结构如下:

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

有没有办法告诉 spring 应该只扫描包以“dao”或“service”结尾的所有类?

这是类似问题的示例,但它不能解决我的问题。

4

2 回答 2

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

@参见Spring 参考章节 4.10.3 使用过滤器自定义扫描


附录:(因为评论

可以有多个include-filters 和exclude-filters 但顺序很重要:首先include-filter然后exclude-filter- 请参阅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>
于 2012-10-10T08:21:10.323 回答
1
  <context:component-scan base-package="org.example">
     <context:include-filter type="regex" expression="YOUR REGEX"/>
  </context:component-scan>

PS:您可以有多个包含过滤器,一个用于服务正则表达式,一个用于 dao

这取自Spring 参考指南的使用过滤器自定义扫描部分

于 2012-10-10T08:16:08.603 回答