exclude @Component from @ComponentScan

后端 未结 7 1476
暖寄归人
暖寄归人 2020-11-28 04:10

I have a component that I want to exclude from a @ComponentScan in a particular @Configuration:

@Component(\"foo\") class Foo {
...         


        
7条回答
  •  清歌不尽
    2020-11-28 04:22

    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 {}
    

提交回复
热议问题