How to exclude all @Component classes except one in @ComponentScan?

My code has the following annotation:

@ComponentScan(basePackageClasses={MyClass.class},
            excludeFilters={@Filter(Component.class)}, //@Component
            includeFilters={@Filter(type=ASSIGNABLE_TYPE, classes=MyClass.class)}
        )

MyClassannotated with help @Component, but still wants to enable it while scanning components. However, component scan filters seem to use logic instead of or. How to achieve what I want to do?

+4
source share
1 answer

@Configurationmore determined than @ComponentScanin all cases.

Instead of going around the annotation @ComponentScan. You should try to explicitly specify yours MyClass.classin the class @Configurationas @Bean, for example:

@Configuration
public class MyClassConfiguraiton {

    @Bean
    public MyClass myClass() {
        return new MyClass();
    }
}

@Import @ComponentScan:

@Import(MyClassConfiguratrion.class)

( @Configuration - @Component).

0

Source: https://habr.com/ru/post/1649920/


All Articles