【问题标题】:multiple @ComponentScan in Spring 4?Spring 4中的多个@ComponentScan?
【发布时间】:2015-09-07 08:40:23
【问题描述】:

我正在使用带有 Java 注释的 Spring 4.16,我想做一些类似的事情:

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

}

显然,它无法编译。但我希望你明白我的意思。我想拥有多个具有不同包和过滤器的 ComponentScan。

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

你知道我该如何解决这个问题吗?提前致谢

【问题讨论】:

    标签: java spring annotations spring-4


    【解决方案1】:

    创建两个空的内部类并在它们上面加上@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 {}
    
    }
    

    应该可以的

    【讨论】:

    • 虽然这可行,但我认为这是一种解决方法。我冒昧地为此创建了一个增强请求。 jira.spring.io/browse/SPR-13151
    • 谢谢你们!我接受你的回答,因为显然是这样做的唯一方法......问候!
    • @M.Deinum 做得好!该功能已经实现,我的团队正在愉快地使用它:)
    猜你喜欢
    • 2022-01-15
    • 2015-04-12
    • 1970-01-01
    • 2014-04-21
    • 2015-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多