< context:include-filter>怎么做?和< context:exclude-filter>在春季工作? [英] How do <context:include-filter> and <context:exclude-filter> work in Spring?
问题描述
我有几项服务:
- example.MailService
- example.LDAPService
- example.SQLService
- example.WebService
- example.ExcelService
- example.MailService
- example.LDAPService
- example.SQLService
- example.WebService
- example.ExcelService
注释.如何排除除一项以外的所有服务?
annotated with @Service
annotation. How can I exclude all services except one?
例如,我只想使用 MailService .我使用以下配置:
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.
如果存在一个包含 MailService 的规则,为什么所有服务都被排除在外?
Why all services are excluded if exists one rule to include MailService?
推荐答案
在包含过滤器之后应用包含过滤器,因此您必须将两个表达式组合为一个排除过滤器. AspectJ表达式允许它(由于XML语法,&
被&
替换):
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 &
due to XML syntax):
<context:exclude-filter type="aspectj"
expression="example..*Service* && !example..MailService*" />
这是一个正则表达式,因此您的表达式.* Service"表示任意数量的任何字符,后跟"Service"".这明确排除了要包含的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.
这篇关于< context:include-filter>怎么做?和< context:exclude-filter>在春季工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!