我正在使用带有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注释那些组件,对吗?

你知道我该怎么解决吗?提前致谢

最佳答案

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

}

那应该工作

关于java - Spring 4中有多个@ComponentScan?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30970748/

10-16 09:15