2
@ComponentScan(  //CS1
    basePackages = {"com.package.A", "com.package.B"},
    excludeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE,
                                           value = {com.Package.A.SomeClass.class
                                           })
)

@ComponentScan(  //CS2
    basePackages = { "com.package.A"}
)
@EnableAutoConfiguration
@SpringBootApplication
public class Application {
  public static void main(String[] args) throws Exception {
    ConfigurableApplicationContext ctx = SpringApplication.run(Application.class, args);
  }
}

Above is my SpringBootApplication's main class.As you can see,I have to use Annnotation ,not xml. There are two @ComponentScan Annotations.And it is not permitted for Spring, of course. For me, the two different @ComponentScan means two different way to start up my application. And if I choose to use CS1(it means @ComponentScan1), I hava to comment CS2,and vice versa.

It's not elegant or graceful.Especially for those who are newbie for Spring.So I wonder know how can I config it dynamic according to my .properties file.Such as a param in my .properties file called "isScanA" is ture, then I can use CS1. Or any other elegant way.

I have tried a lot.

  1. Use placeholder. Such as @ComponentScan(basePackage="${scan.basePackage}") .And change the value in .properties file when needed. But this way can't fix the excludeFilters. Because if I use FilterType.ASSIGNABLE_TYPE to assign the class which need to be exclude, the value should be a Class type not a String,where if value = {"${scan.excludeClass}"} be used.

  2. Programmatic way.

    /**
     * Create a new AnnotationConfigApplicationContext, scanning for bean definitions
     * in the given packages and automatically refreshing the context.
     * @param basePackages the packages to check for annotated classes
     */
    public AnnotationConfigApplicationContext(String... basePackages) {
        this();
        scan(basePackages);
        refresh();
    }
    

I called this method in my main function.But it also can't fix the excludeFilters problem, the reason is here:Doing context:component-scan programatic way?

...

I really tried a lot , but still can't fix. So I need your help.

Forgive my poor English, plz.

Thanx a lot, at least you have take a little time to read.

Community
  • 1
  • 1
Dai Kaixian
  • 1,045
  • 2
  • 14
  • 24
  • 1
    Why do you want to even do this? Looks like you are trying to fix something you shouldn't be fixing in the first place. – M. Deinum Nov 01 '16 at 12:15
  • 2
    Have a look at Spring Profiles: they sound exactly like what you need – David Lavender Nov 01 '16 at 12:20
  • @M.Deinum Actually, I am trying to replace the rpc system with direct call in the same jvm. CS2 is the old way to load the service bean which is supported by rpc.And CS1 is the new way to load service bean which can be called in the same jvm. I just want to make two ways choosable for developer.He can start up the app in rpc way or direct calll way.It depends on the value he set in the .properties file.I think it's more graceful than comment the codes. – Dai Kaixian Nov 01 '16 at 13:55
  • 1
    Then use Spring Profiles (or property) and use a condition to select a `@Configuration` to load which has one or the other `@ComponentScan`. – M. Deinum Nov 01 '16 at 14:05
  • @M.Deinum Yeah, I have read the doc about Spring Profile.It can help.Thx – Dai Kaixian Nov 01 '16 at 14:08

1 Answers1

5

Maybe you are look for Spring's Profile! Spring Profile allow you to determine profiles for Configurations and Beans. I think you must separate the configuration classes to have two profiles! Take a look at those examples!

Here is the documentation:

http://docs.spring.io/autorepo/docs/spring-boot/current/reference/html/boot-features-profiles.html

Community
  • 1
  • 1
Bruno
  • 2,889
  • 1
  • 18
  • 25