< context:include-filter>怎么做?和< context:exclude-filter>在春季工作? [英] How do <context:include-filter> and <context:exclude-filter> work in Spring?

查看:227
本文介绍了< context:include-filter>怎么做?和< context:exclude-filter>在春季工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几项服务:

  • 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语法,&&amp;替换):

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 &amp; due to XML syntax):

<context:exclude-filter type="aspectj" 
    expression="example..*Service* &amp;&amp; !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.

这篇关于&lt; context:include-filter&gt;怎么做?和&lt; context:exclude-filter&gt;在春季工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆