spring - Filter specific packages in @ComponentScan -


i want switch xml based java based configuration in spring. have 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 write this...

 @componentscan(     basepackages = {"foo.bar", "foo.baz"},     excludefilters = @componentscan.filter(        value= service.class,         type = filtertype.annotation     )  ) 

... exclude services both packages. have strong feeling i'm overlooking embarrassingly trivial, couldn't find solution limit scope of filter foo.bar.

you need create 2 config classes, 2 @componentscan annotations require.

so example have 1 config class foo.bar package:

@configuration @componentscan(basepackages = {"foo.bar"},      excludefilters = @componentscan.filter(value = service.class, type = filtertype.annotation) ) public class foobarconfig { } 

and 2nd config class foo.baz package:

@configuration @componentscan(basepackages = {"foo.baz"}) public class foobazconfig { } 

then when instantiating spring context following:

new annotationconfigapplicationcontext(foobarconfig.class, foobazconfig.class); 

an alternative can use @org.springframework.context.annotation.import annotation on first config class import 2nd config class. example change foobarconfig be:

@configuration @componentscan(basepackages = {"foo.bar"},      excludefilters = @componentscan.filter(value = service.class, type = filtertype.annotation) ) @import(foobazconfig.class) public class foobarconfig { } 

then start context with:

new annotationconfigapplicationcontext(foobarconfig.class) 

Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -