【问题标题】:Update values on properties file on Runtime在运行时更新属性文件的值
【发布时间】:2017-06-26 23:17:14
【问题描述】:

我的配置如下:

@Configuration
public class PropertyConfiguration {

    @Bean
    @Profile("local")
    public static PropertyPlaceholderConfigurer propertyPlaceholderConfigurer() {
        PropertyPlaceholderConfigurer configurer = new PropertyPlaceholderConfigurer();
        configurer.setLocation(new FileSystemResource("path/to/resources/app-local.properties"));
        configurer.setIgnoreUnresolvablePlaceholders(true);
        return configurer;
    }
}

我的app-local.properties 文件包含以下值:

cache.time.milliseconds=1000

所以,我访问的值是:

    @Value("${cache.time.milliseconds}")
    private long cachingTime;

我得到了正确的值。

System.out.println(cachingTime);

现在,我想将cachingTime 更新为其他值并提供更新后的值。例如,从 1000 到 99。

有没有办法在运行时更新这个属性值??

或者除了重启应用或服务器之外,还有其他方法可以更新这个值吗?

我正在使用 Spring Boot 1.4.3.RELEASE。

我试图用谷歌搜索它,但没有一个答案给我解决方案。 :(

感谢您的帮助。

【问题讨论】:

    标签: java spring-mvc spring-boot properties-file


    【解决方案1】:

    如果您要更改属性文件上的值,它不会影响运行时,因为所有配置都是在服务器启动时完成的,如果您不想重新部署代码库,您可以做一件事,更改属性文件值然后重启服务器。

    【讨论】:

    • 我知道...但我的要求是我不应该重新启动服务器或应用程序。 :)
    • 没有其他选择
    【解决方案2】:

    你可以看看 spring-boot admin 一次。尽管它充当监视服务器,但它使您能够更新属性和环境变量。

    http://codecentric.github.io/spring-boot-admin/1.5.3/

    附上一张截图,由以代码为中心的人证明了概念。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-03
      • 2015-02-26
      • 1970-01-01
      • 2018-03-30
      • 2020-06-28
      相关资源
      最近更新 更多