我的包结构如下:
(...)
com.domain.group.dao
com.domain.group.service
(...)
com.domain.users.dao
com.domain.users.service
有没有办法告诉 spring 应该只扫描包以“dao”或“service”结尾的所有类?
这是类似问题的示例,但它不能解决我的问题。
<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-filter
s 和exclude-filter
s 但顺序很重要:首先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>
<context:component-scan base-package="org.example">
<context:include-filter type="regex" expression="YOUR REGEX"/>
</context:component-scan>
PS:您可以有多个包含过滤器,一个用于服务正则表达式,一个用于 dao