在Spring Boot中动态更改application.properties值 [英] Dynamically change application.properties values in spring boot

查看:406
本文介绍了在Spring Boot中动态更改application.properties值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我正在Spring Boot中基于​​REST的项目上工作.我已经在"application.properties"文件中添加了api网址.

Currently i am working on a REST based project in Spring Boot. I have added the api url in 'application.properties' file.

api-base-url=http://localhost:8080/RestServices/v1

还有来自Java的'api-base-url'值访问.

And also this 'api-base-url' value access from java.

在某些情况下,我需要动态更改"api-base-url".我已经动态更改了'api-base-url'的值&工作正常.

In some situations i need to change the 'api-base-url' dynamically. I have change 'api-base-url' value dynamically & working fine.

但是我的问题是当wildfly重新启动时,配置将重置为默认值.

But my problem is that when wildfly restart then the configuration will be reset to default.

即这是我的默认值

api-base-url=http://localhost:8080/RestServices/v1

动态更改为

api-base-url=http://10.34.2.3:8080/RestServices/v1

当wildfly重新启动时,配置将重置为默认值.即

when wildfly restart then the configuration will be reset to default. i.e.

api-base-url=http://localhost:8080/RestServices/v1

对此有什么解决办法吗?

Have any solution for this?

推荐答案

正如@moilejter所建议的,一种可能的方法是保留在数据库表中,并且在开始时,您只需从该表中读取而不是从application.properties文件中读取即可.您的application.properties文件可以保存数据库连接所需的信息.

As @moilejter suggests, one possible way is to persist in database table and at start time you simply read from that table instead of application.properties file. Your application.properties files can hold information necessary for database connection.

您还需要JMX方法或REST API,以在您的应用程序中触发url发生了更改,然后只需从同一表中读取该URL.这样一来,即使应用重新启动,您也将很安全,并且不会丢失覆盖.

You would also need a JMX method or a REST API to trigger in your application that the url has changed and which inturn, would simply read from same table. This way you would be safe even if app restarts and you won't lose the override.

您可以将BeanFactoryPostProcessor与Environment Bean结合使用,以利用Spring占位符概念.

You can use BeanFactoryPostProcessor coupled with Environment bean to leverage spring placeholder concept.

这篇关于在Spring Boot中动态更改application.properties值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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