我正在使用带有 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/