外部属性文件更改时重新加载属性值,spring boot [英] Reload property value when external property file changes ,spring boot

查看:39
本文介绍了外部属性文件更改时重新加载属性值,spring boot的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 spring boot,并且我有两个外部属性文件,以便我可以轻松更改其值.

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

但我希望 spring 应用程序在更新时会重新加载更改的值,就像从文件中读取一样.由于属性文件很容易满足我的需要,我希望我不需要数据库或文件.

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);
    }
}

我将使用

-Dspring.config.location=conf/my.properties

推荐答案

恐怕您需要重新启动 Spring 上下文.

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

这篇关于外部属性文件更改时重新加载属性值,spring boot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆