I have several services:
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?
The ANNOTATION filter type includes or excludes classes in the component scans which are marked with given annotations.
Auto Components Scanning Put this “ context:component ” in bean configuration file, it means, enable auto scanning feature in Spring. The base-package is indicate where are your components stored, Spring will scan this folder and find out the bean (annotated with @Component) and register it in Spring container.
You may use the exclude attribute with the annotation @SpringBootApplication.
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"/>
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*" />
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With