2

I'm running a Spring Boot Application within a Tomcat instance packaged as a war file. I would like to be able to add "packages" to my instance in the form of rest services. To that end I need to be able to configure scanBasePackages in the @SpringBootApplication annotation at runtime. i.e. when tomcat starts up. For now I have ...

@SpringBootApplication(scanBasePackages="path1, path2")
public class RestApplication extends SpringBootServletInitializer {
   //code
}

...but I would like to have path2 be configurable and alternately be able to add path3, etc. if desired. How can I achieve this? I understand that I can't configure the String in the annotation so my question is about what other alternatives I have to this annotation for setting this.

Cheers.

crowmagnumb
  • 6,621
  • 9
  • 33
  • 42

2 Answers2

4

you can try to use something like this in your project

@Configuration
@Profile("custom-profile")    
@ImportResource( {"file:path/additional-context.xml" } )  
public class ConfigClass { }  

and configure additional packages scanning in this file then.

<context:component-scan base-package="x.y.z.service, x.y.z.controller, x.y.z.dao" />

Note, your resource additional-context.xml declared as external and you have ability to change it without rebuilding war at all.

@ImportResource will be handled after declaring profile "custom-profile" as active. It's a safe way for starting application with "default configuration" without any "additional-context.xml" file of disk.

ivanenok
  • 594
  • 5
  • 9
  • Ah cheers! But, for my case, it turns out to push the problem back a bit. For the "generic" install I want there to be no additional configuration file. If I make this be a file that doesn't exist or an empty string it complains that FileNotFound and the war fails to start. So the only thing I can think of is to have a dummy xml file that has nothing in it for the standard install. To do this maybe I need to use some sort of env variable so that my ImportResource value could be "classpath:${ENV_VAR}spring-context.xml". If ENV_VAR is not set it defaults to dummy spring-context.xml. thoughts? – crowmagnumb Mar 11 '16 at 21:38
  • @crowmagnumb you can declare your configuration as a part of a custom profile then, a import will not be doing on starting "default bundle" in this way. After activation required profile via standard Spring spring.profiles.active your custom configuration will be loaded without any problems. – ivanenok Mar 12 '16 at 16:34
2

Have you tried this:

@SpringBootApplication(scanBasePackages={"path1", "path2"})

Hope this helps :)

Andrés S.
  • 61
  • 4