Spring 4中有多个@ComponentScan? [英] multiple @ComponentScan in Spring 4?

查看:576
本文介绍了Spring 4中有多个@ComponentScan?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有Java Annotations的Spring 4.16,我想做类似的事情:

I am using Spring 4.16 with Java Annotations, and i want to do something like:

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

}

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

Obviusly, it doesn't compile. But i hope you get my point. I want to have multiple ComponentScans with differents packages and filters.

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

I cannot unify both ComponentsScan because it wouldn't create any component from framework but those which are annotated with ServiceComponent, am i right?

你知道我怎么解决这个问题?在此先感谢

Do you know how could i solve this? Thanks in advance

推荐答案

创建两个空的内部类并放入 @ComponentScan 注释:

Create two empty internal classes and put the @ComponentScan annotation on them:

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

}

这应该有效

这篇关于Spring 4中有多个@ComponentScan?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆