Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I override Spring Boot application.properties programmatically?

I have jdbc property files which I take from external configuration web-service In spring boot in order to set mysql props it's easy as adding those to application.properties:

spring.datasource.url=jdbc:mysql://localhost/mydb spring.datasource.username=root spring.datasource.password=root spring.datasource.driver-class-name=com.mysql.jdbc.Driver 

How could I override those programticlly in my app?

same goes for Spring-batch props:

database.driver=com.mysql.jdbc.Driver database.url=jdbc:mysql://localhost/mydv database.username=root database.password=root 
like image 582
rayman Avatar asked Mar 16 '15 08:03

rayman


People also ask

Can we change application properties in spring boot?

Spring boot provides command line configuration called spring.config.name using that we can change the name of application. properties. Here properties file name will be my-config.


2 Answers

You can add additional property sources in a lifecycle listener reacting to ApplicationEnvironmentPrepared event.

Something along the lines of:

public class DatabasePropertiesListener implements ApplicationListener<ApplicationEnvironmentPreparedEvent> {   public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {     ConfigurableEnvironment environment = event.getEnvironment();     Properties props = new Properties();     props.put("spring.datasource.url", "<my value>");     environment.getPropertySources().addFirst(new PropertiesPropertySource("myProps", props));   } } 

Then register the class in src/main/resources/META-INF/spring.factories:

org.springframework.context.ApplicationListener=my.package.DatabasePropertiesListener 

This worked for me, however, you are sort of limited as to what you can do at this point as it's fairly early in the application startup phase, you'd have to find a way to get the values you need without relying on other spring beans etc.

like image 107
Lukas Hinsch Avatar answered Sep 29 '22 07:09

Lukas Hinsch


Just to provide another option to this thread for reference as when I started to look for an answer for my requirement this came high on the search list, but did not cover my use case.

I was looking to programmatically set spring boot property at start up, but without the need to work with the different XML/Config files that spring supports.

The easiest way is to set the properties at the time the SpringApplication is defined. The basic example below sets the tomcat port to 9999.

@SpringBootApplication public class Demo40Application{      public static void main(String[] args){         SpringApplication application = new SpringApplication(Demo40Application.class);          Properties properties = new Properties();         properties.put("server.port", 9999);         application.setDefaultProperties(properties);          application.run(args);     } } 
like image 33
Roger Thomas Avatar answered Sep 29 '22 08:09

Roger Thomas