Spring 4 中的多个@ComponentScan?
multiple @ComponentScan in Spring 4?
我正在使用带有 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.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 {}
}
应该可行