gpt4 book ai didi

Spring组件扫描自定义过滤器

转载 作者:行者123 更新时间:2023-12-01 11:49:31 26 4
gpt4 key购买 nike

我的包结构如下:

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

有什么方法可以告诉 spring 只扫描所有以“dao”或“service”结尾的类吗?

here's example of similar problem ,但这并没有解决我的问题。

最佳答案

 <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 Reference Chapter 4.10.3 Using filters to customize scanning


附录:(因为评论)

可以有多个 include-filterexclude-filter 但顺序很重要:第一个 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>

关于Spring组件扫描自定义过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12814709/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com