Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reload property value when external property file changes ,spring boot

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

But I hope spring app will reload the changed value when it is updated, just like reading from files. Since property file is easy enough to meet my need, I hope I don' nessarily need a db or file.

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

@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 boot my application with

-Dspring.config.location=conf/my.properties
like image 488
JaskeyLam Avatar asked Sep 08 '16 09:09

JaskeyLam


1 Answers

I'm afraid you will need to restart Spring context.

like image 173
luboskrnac Avatar answered Sep 30 '22 16:09

luboskrnac