Update property value when changing external property file, spring boot

I am using spring boot and I have two external property files, so I can easily change its value.

But I hope that the spring application will reload the changed value when it is updated, just like reading from files. Since the properties file is simple enough to meet my needs, I hope I don't need a db or file.

I use two different ways to load the property value, the sample code will look like this:

@RestController public class Prop1Controller{ @Value("${prop1}") private String prop1; @RequestMapping(value="/prop1",method = RequestMethod.GET) public String getProp() { return prop1; } } @RestController public class Prop2Controller{ @Autowired private Environment env; @RequestMapping(value="/prop2/{sysId}",method = RequestMethod.GET) public String prop2(@PathVariable String sysId) { return env.getProperty("prop2."+sysId); } } 

I will download my application using

 -Dspring.config.location=conf/my.properties 
+8
source share
5 answers

I'm afraid you will have to restart the Spring context.

+2
source

I think the only way to achieve your need is to enable spring-cloud . There is a /refresh endpoint that updates context and beans.

I'm not quite sure if you need spring-cloud-config-server (its microservice is very easy to build) where your config is stored (Git or svn). Or if it can also be used only in the application.properties file in the application.

Here you can find the document in the update area and the spring cloud.

+1
source

(1) Spring Cloud RestartEndPoint

You can use RestartEndPoint : programmatically restart Spring application to load / update Spring context

RestartEndPoint is the drive endpoint, complete with spring-cloud-context .

However, RestartEndPoint will not track file changes; you will have to handle this yourself.


(2) devtools

I do not know if this is suitable for a production application or not. You can hack devtools bit to do what you want.

Take a look at this other answer that I wrote for another question: Forcing spring-boot DevTools on Jar

Devtools controls file changes :

Applications that use spring-boot-devtools will automatically restart whenever files change on the class path.

Technically, devtools is created only to work in the IDE. With the help of hacking, it also works when starting from a jar. However, I can not do this for a real production application, you decide whether it suits your needs.

0
source

You can use Spring Cloud to

Add this as a dependency

 compile group: 'org.springframework.cloud', name: 'spring-cloud-starter', version: '1.1.2.RELEASE' 

And then use the @RefreshScope annotation

A Spring @Bean, which is marked as @RefreshScope, will receive special treatment when a configuration is changed. This fixes the stateful beans problem, which gets their configuration when they are initialized. For example, if a DataSource has open connections, when the database URL changes through the environment, we probably want the owners of these connections to be able to do what they do. Then, the next time someone takes a connection from the pool, he gets one with a new URL.

It also matters if you have a Spring Actuator.

There are several additional management endpoints in the Spring Boot Actuator application:

POST for

/ env to update the environment and rebind @ConfigurationProperties and log levels

/ refresh to reload the boot belt context and update @RefreshScope beans

Spring cloud doc

0
source

You can monitor the implementation of the ContextRefresher.refresh () code.

 public synchronized Set<String> refresh() { Map<String, Object> before = extract( this.context.getEnvironment().getPropertySources()); addConfigFilesToEnvironment(); Set<String> keys = changes(before, extract(this.context.getEnvironment().getPropertySources())).keySet(); this.context.publishEvent(new EnvironmentChangeEvent(context, keys)); this.scope.refreshAll(); return keys; } 
0
source

All Articles