Spring 4中有多个@ComponentScan?

jsc*_*man 7 java spring annotations spring-4

我使用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 {

}
Run Code Online (Sandbox Code Playgroud)

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

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

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

Ran*_*niz 8

创建两个空的内部类并将@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 {}

}
Run Code Online (Sandbox Code Playgroud)

这应该工作

  • 虽然这有效,但我认为这是一种解决方法.我冒昧地为此创建了一个增强请求.https://jira.spring.io/browse/SPR-13151 (4认同)