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

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

问题描述

我将 Spring 4.16 与 Java Annotations 一起使用,我想执行以下操作:

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 {

}

显然,它无法编译.但我希望你明白我的意思.我想要多个带有不同包和过滤器的 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天全站免登陆