Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter specific packages in @ComponentScan

I want to switch from XML based to Java based configuration in Spring. Now we have something like this in our application context:

<context:component-scan base-package="foo.bar">     <context:exclude-filter type="annotation" expression="o.s.s.Service"/> </context:component-scan> <context:component-scan base-package="foo.baz" /> 

But if I write something like this...

 @ComponentScan(     basePackages = {"foo.bar", "foo.baz"},     excludeFilters = @ComponentScan.Filter(        value= Service.class,         type = FilterType.ANNOTATION     )  ) 

... it will exclude services from both packages. I have the strong feeling I'm overlooking something embarrassingly trivial, but I couldn't find a solution to limit the scope of the filter to foo.bar.

like image 625
Landei Avatar asked Apr 26 '13 13:04

Landei


People also ask

How do you exclude certain beans?

You need a method with '@Bean' annotation that crate and instance of the class, or annotate the class with '@Componen', '@Service' etc. annotation for annotation scanning to find it ? Does @ComponentScan(excludeFilters = @ComponentScan. Filter(type = FilterType.

How do I exclude a particular class in Spring boot?

If the class is not on the classpath, you can use the excludeName attribute of the annotation and specify the fully qualified name instead. Finally, you can also control the list of auto-configuration classes to exclude by using the spring. autoconfigure. exclude property.

How do I scan a base package in Spring boot?

With Spring, we use the @ComponentScan annotation along with the @Configuration annotation to specify the packages that we want to be scanned. @ComponentScan without arguments tells Spring to scan the current package and all of its sub-packages.

What is FilterType Assignable_type?

4. FilterType. ASSIGNABLE_TYPE. The ASSIGNABLE_TYPE filters all classes during the component scan that either extend the class or implement the interface of the specified type.


1 Answers

You simply need to create two Config classes, for the two @ComponentScan annotations that you require.

So for example you would have one Config class for your foo.bar package:

@Configuration @ComponentScan(basePackages = {"foo.bar"},      excludeFilters = @ComponentScan.Filter(value = Service.class, type = FilterType.ANNOTATION) ) public class FooBarConfig { } 

and then a 2nd Config class for your foo.baz package:

@Configuration @ComponentScan(basePackages = {"foo.baz"}) public class FooBazConfig { } 

then when instantiating the Spring context you would do the following:

new AnnotationConfigApplicationContext(FooBarConfig.class, FooBazConfig.class); 

An alternative is that you can use the @org.springframework.context.annotation.Import annotation on the first Config class to import the 2nd Config class. So for example you could change FooBarConfig to be:

@Configuration @ComponentScan(basePackages = {"foo.bar"},      excludeFilters = @ComponentScan.Filter(value = Service.class, type = FilterType.ANNOTATION) ) @Import(FooBazConfig.class) public class FooBarConfig { } 

Then you would simply start your context with:

new AnnotationConfigApplicationContext(FooBarConfig.class) 
like image 180
DB5 Avatar answered Sep 20 '22 20:09

DB5