How can i externalize datasource configuration with spring-boot? -


i'm trying move existing spring-application spring-boot , therefore recreate things worked without boot.

i want configure properties (like spring.datasource.*) external source. specificly folder several properties files.

i set configuration class creates propertyplaceholder configurers this:

@configuration public class propertysourceconfiguration {  @bean public static propertysourcesplaceholderconfigurer defaultsplaceholderconfigurer() throws ioexception {     propertysourcesplaceholderconfigurer propertyconfigurer = new propertysourcesplaceholderconfigurer();     propertyconfigurer.setlocations(new pathmatchingresourcepatternresolver().getresources("classpath*:/*-defaults.properties"));     propertyconfigurer.setignoreunresolvableplaceholders(true);     return propertyconfigurer; }  @bean public static propertysourcesplaceholderconfigurer externalplaceholderconfigurer() throws ioexception {     propertysourcesplaceholderconfigurer propertyconfigurer = new propertysourcesplaceholderconfigurer();     propertyconfigurer.setlocations(new             pathmatchingresourcepatternresolver().getresources("file:/my-config-path/*.properties"));     propertyconfigurer.setorder(1);     propertyconfigurer.setignoreunresolvableplaceholders(true);     return propertyconfigurer; } 

this seems work things (like amqp or own config properties) when try use spring-data-jpa ignored. basicly setting spring.datasource.url (and other things used auto-config) in files has no effect.

looking through logs of propertysourcespropertyresolver figured out these configurer fall under localproperties group not used when looking spring.datasource.*.

is there way fix or better way add external properties files context?

i know set spring.config.location similar can not pass command-line properties application , need config within application. afaik not possible property.

edit: setting spring.config.location:

attempt 1:

public class servletinitializer extends springbootservletinitializer {      @override     protected springapplicationbuilder configure(springapplicationbuilder application) {         return application.sources(campaignservicestarter.class);     }     @override     public void onstartup(servletcontext servletcontext) throws servletexception {         super.onstartup(servletcontext);         servletcontext.setinitparameter("spring.config.location", "file:/my-config-path/*.properties");     } } 

attempt 2:

public class servletinitializer extends springbootservletinitializer {      @override     protected springapplicationbuilder configure(springapplicationbuilder application) {         return application.sources(campaignservicestarter.class).properties("spring.config.location=file:/my-config-path/*.properties");     } } 

in both cases external properties not picked @ (even in places worked before, amqp config)

how use external configuration explained in this section of spring boot reference guide.

the spring.config.location path directory contains application.properties file. takes comma separated list of values specify multiple paths. doesn't take wildcard. path not expression match multiple property files. if want change default application use spring.config.name make else.

the defaults of spring boot opinionated rest of spring boot (with default configuration etc.).

if want more extensive (pre) configuration should use applicationcontextinitializer manually add propertysources environment. mentioned here in spring boot reference guide.

an example of how initializer might look.

public class configurationinitializer implements applicationcontextinitializer {      private static final string default_props = "classpath*:/*-defaults.properties";     private static final string external_props = "file:/my-config-path/*.properties";      public void initialize(configurableapplicationcontext applicationcontext) {         final resource[] defaultconfigs = applicationcontext.getresources(default_props);         final resource[] externalconfigs = applicationcontext.getresources(external_props);          final configurableenvironment env = applicationcontext.getenvironment();         final mutablepropertysources mps =  env.getpropertysources();         (resource r : externalconfigs) {             mps.addlast(new resourcepropertysource(r.getfilename(), r);         }         (resource r : defaultconfigs) {             mps.addlast(new resourcepropertysource(r.getfilename(), r);         }        } } 

then when building application object add follows.

public class servletinitializer extends springbootservletinitializer {      @override     protected springapplicationbuilder configure(springapplicationbuilder application) {         return application.sources(campaignservicestarter.class)             .initializers(new configurationinitializer());     } } 

now configs should added list of property sources.


Comments

Popular posts from this blog

IF statement in MySQL trigger -

c++ - What does MSC in "// appease MSC" comments mean? -

javascript - Blogger related post gadget image Resize s72-c [ Need Expert Help ] -