Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
487 views
in Technique[技术] by (71.8m points)

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

I have several services:

  • example.MailService
  • example.LDAPService
  • example.SQLService
  • example.WebService
  • example.ExcelService

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


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.

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...