Multiple @ComponentScan in Spring 4?

I am using Spring 4.16 with Java annotations and I want to do something like:

@Configuration @ComponentScan(basePackages = "com.example.business", includeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = ServiceComponent.class)) @ComponentScan(basePackages = "com.example.business.framework") public class ServicesBaseConfiguration { } 

Obviusly, it does not compile. But I hope you understand my point. I want to have several ComponentScans with various packages and filters.

I cannot unify both components of ComponentsScan because it will not create any component from the framework, but those that are annotated with ServiceComponent, am I right?

Do you know how I can solve this? thanks in advance

+6
source share
1 answer

Create two empty inner classes and add the @ComponentScan annotation to them:

 @Configuration @Import({ServicesBaseConfiguration.Filtered.class, ServicesBaseConfiguration.Unfiltered.class}) public class ServicesBaseConfiguration { @Configuration @ComponentScan(basePackages = "com.example.business", includeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = ServiceComponent.class)) public static class Filtered {} @Configuration @ComponentScan(basePackages = "com.example.business.framework") public static class Unfiltered {} } 

This should work

+7
source

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


All Articles