7

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

jscherman
  • 5,839
  • 14
  • 46
  • 88

1 Answers1

8

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

Raniz
  • 10,882
  • 1
  • 32
  • 64
  • 4
    Although this works, I would consider this a workaround. I took the liberty to create an enhancement request for this. https://jira.spring.io/browse/SPR-13151 – M. Deinum Jun 22 '15 at 05:59
  • Thanks to you both! I accept your answer since apparently is the only way to do this... Regards! – jscherman Jun 30 '15 at 02:34
  • 1
    Good job to @M.Deinum ! The feature has been implemented and I our team is using it happily :) – sashok_bg Aug 09 '16 at 11:23