exclude @Component from @ComponentScan
The configuration seem alright, except that you should use excludeFilters
instead of excludes
:
@Configuration @EnableSpringConfigured
@ComponentScan(basePackages = {"com.example"}, excludeFilters={
@ComponentScan.Filter(type=FilterType.ASSIGNABLE_TYPE, value=Foo.class)})
public class MySpringConfiguration {}
Using explicit types in scan filters is ugly for me. I believe more elegant approach is to create own marker annotation:
@Retention(RetentionPolicy.RUNTIME)
public @interface IgnoreDuringScan {
}
Mark component that should be excluded with it:
@Component("foo")
@IgnoreDuringScan
class Foo {
...
}
And exclude this annotation from your component scan:
@ComponentScan(excludeFilters = @Filter(IgnoreDuringScan.class))
public class MySpringConfiguration {}