<context:include-filter> 和 <context:exclude-filter> 在 Spring 中如何工作?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3710444/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 04:08:24  来源:igfitidea点击:

How do <context:include-filter> and <context:exclude-filter> work in Spring?

springconfiguration

提问by Francisco Villa Ramos

I have several services:

我有几个服务:

  • example.MailService
  • example.LDAPService
  • example.SQLService
  • example.WebService
  • example.ExcelService
  • 例子.MailService
  • 示例.LDAP服务
  • 例子.SQLService
  • 例子.WebService
  • 例子.ExcelService

annotated with @Serviceannotation. How can I exclude all services except one?

@Service注解注解。如何排除除一项服务之外的所有服务?


For example I want to use only MailService. I use the following configuration:


例如,我只想使用MailService。我使用以下配置:

<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.

但现在所有服务都被排除在外。

Why all services are excluded if exists one rule to include MailService?

如果存在一个包含MailService 的规则,为什么会排除所有服务?

采纳答案by axtavt

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):

包含过滤器在排除过滤器之后应用,因此您必须将两个表达式组合成一个排除过滤器。AspectJ 表达式允许它(由于 XML 语法&被替换&amp;):

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

这是一个正则表达式,因此您的表达式“.*Service”表示“任意数量的任何字符后跟“Service””。这明确排除了您要包含的 MailService。

回答by David

Another way to perform this registration is with a single inclusion filter.

执行此注册的另一种方法是使用单个包含过滤器。

<context:component-scan base-package="example" use-default-filters="false">
    <context:include-filter type="aspectj" expression="example..MailService*" />
</context:component-scan>

The "use-default-filters" attribute must be set to "false" in this case to keep Spring from adding a default filter equivalent to

在这种情况下,“use-default-filters”属性必须设置为“false”,以防止 Spring 添加等效于的默认过滤器

<context:include-filter type="annotation" 
                        expression="org.springframework.stereotype.Component"/>

回答by earldouglas

It looks like you want to use filter type "regex". Here's an example from the Spring Reference:

看起来您想使用过滤器类型“regex”。这是Spring 参考中的一个示例:

<beans>

   <context:component-scan base-package="org.example">
      <context:include-filter type="regex" expression=".*Stub.*Repository"/>
      <context:exclude-filter type="annotation"
                              expression="org.springframework.stereotype.Repository"/>
   </context:component-scan>

</beans>