Spring 4中有多个@ComponentScan?

我使用Spring 4.16和Java Annotations,我想做的事情如下:

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

很可惜,它没有编译。 但我希望你明白我的观点。 我想要多个ComponentScans包含不同的包和filter。

我无法统一两个ComponentsScan,因为它不会从框架创建任何组件,而是那些用ServiceComponent注释的组件,我是对的吗?

你知道我怎么解决这个问题? 提前致谢

创建两个空的内部类并在其上放置@ComponentScan注释:

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

这应该工作