Exclude subpackages from Spring autorun?

Is there an easy way to exclude a package / subpackage from autwiring in Spring 3.1?

For example, if I wanted to enable component scanning with the base package com.example , is there an easy way to exclude com.example.ignore ?

(Why? I would like to exclude some components from my integration tests)

+50
spring autowired
May 23 '12 at 17:46
source share
8 answers

I'm not sure that you can explicitly exclude packages using the <exclude-filter> filter, but I'm sure that using a regular expression filter will effectively get you there:

  <context:component-scan base-package="com.example"> <context:exclude-filter type="regex" expression="com\.example\.ignore\..*"/> </context:component-scan> 

To make it based on annotations, you must annotate every class that you want to exclude for integration tests, with something like @ com.example.annotation.ExcludedFromITests. Then the scan component will look like this:

  <context:component-scan base-package="com.example"> <context:exclude-filter type="annotation" expression="com.example.annotation.ExcludedFromITests"/> </context:component-scan> 

This is clearer because now you yourself have documented in the source code itself that the class is not intended to be included in the application context for integration tests.

+63
May 23 '12 at 18:11
source share

I am using @ComponentScan as follows for the same use case. This is the same as BenSchro10 answer , but it uses annotations. Both use a filter with type=AspectJ

 import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.boot.autoconfigure.jersey.JerseyAutoConfiguration; import org.springframework.boot.autoconfigure.jms.JmsAutoConfiguration; import org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.FilterType; import org.springframework.context.annotation.ImportResource; @SpringBootApplication @EnableAutoConfiguration @ComponentScan(basePackages = { "com.example" }, excludeFilters = @ComponentScan.Filter(type = FilterType.ASPECTJ, pattern = "com.example.ignore.*")) public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } 
+37
May 12 '15 at 19:27
source share

This works in Spring 3.0.5. So, I would think that this would work in version 3.1

 <context:component-scan base-package="com.example"> <context:exclude-filter type="aspectj" expression="com.example.dontscanme.*" /> </context:component-scan> 
+10
May 23 '12 at 18:05
source share

It seems you did it via XML, but if you were working in Spring's new best practice, your configuration would be in Java, and you could exclude them like this:

 @Configuration @EnableWebMvc @ComponentScan(basePackages = "net.example.tool", excludeFilters = {@ComponentScan.Filter( type = FilterType.ASSIGNABLE_TYPE, value = {JPAConfiguration.class, SecurityConfig.class}) }) 
+8
Aug 10 '16 at 18:47
source share

For Spring 4, I use the following (I am posting it since the question is 4 years old and more people are using Spring 4 than Spring 3.1):

 @Configuration @ComponentScan(basePackages = "com.example", excludeFilters = @Filter(type=FilterType.REGEX,pattern="com\\.example\\.ignore\\..*")) public class RootConfig { // ... } 
+8
Jan 11 '17 at 11:19 on
source share

I think you should reorganize your packages in a more convenient hierarchy, so they are not part of the base package.

But if you cannot do this, try:

 <context:component-scan base-package="com.example"> ... <context:exclude-filter type="regex" expression="com\.example\.ignore.*"/> </context:component-scan> 

You can find more examples here: Using filters to configure scanning

+4
May 23 '12 at 18:10
source share

One thing that seems to work for me is this:

 @ComponentScan(basePackageClasses = {SomeTypeInYourPackage.class}, resourcePattern = "*.class") 

Or in XML:

 <context:component-scan base-package="com.example" resource-pattern="*.class"/> 

This overrides the default value of resourcePattern , which is equal to "**/*.class" .

It would seem that this is the safest way for a type to ONLY include your base package, since this Pattern resource will always be the same with your base package.

+3
Apr 27 '16 at 14:28
source share

You can also use @SpringBootApplication , which according to the Spring documentation performs the same functions as after three annotations: @ Configuration , @EnableAutoConfiguration @ComponentScan in one annotation.

 @SpringBootApplication(exclude= {Foo.class}) public class MySpringConfiguration {} 
+2
Jul 09 '17 at 15:20
source share



All Articles