multiple @ComponentScan in Spring 4?

陌路散爱 提交于 2019-12-05 22:25:22

问题


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, it doesn't compile. But i hope you get my point. I want to have multiple ComponentScans with differents packages and filters.

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


回答1:


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

}

That should work



来源:https://stackoverflow.com/questions/30970748/multiple-componentscan-in-spring-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!