如何在Spring中动态维护,更新应用程序属性? [英] How to maintain, update application properties dynamically in Spring?
问题描述
我想在Spring应用程序中维护一个应用程序属性列表,例如服务端点,应用程序变量等.这些属性应该能够动态更新(可能由系统管理员通过网页更新).
I would like to maintain a list of application properties like service endpoints, application variables, etc. in a Spring application. These properties should be able to updated dynamically (possibly through an web page by system administrator).
spring是否具有内置功能来满足这一要求?
Does spring has an inbuilt feature to accomplish this requirement?
推荐答案
我不确定spring是否具有动态更新属性文件的实现.
I am not sure spring has an implementation for updating the properties file dynamically.
您可以执行类似的操作,例如使用FileInputStream
将属性文件读取到Properties
对象中.然后,您将能够更新属性.稍后,您可以使用FileOutputStream
将属性写回到同一文件.
You can do something like reading the properties file using FileInputStream
into a Properties
object. Then you will be able to update the properties. Later you can write back the properties to the same file using the FileOutputStream
.
// reading the existing properties
FileInputStream in = new FileInputStream("propertiesFile");
Properties props = new Properties();
props.load(in);
in.close();
// writing back the properties after updation
FileOutputStream out = new FileOutputStream("propertiesFile");
props.setProperty("property", "value");
props.store(out, null);
out.close();
这篇关于如何在Spring中动态维护,更新应用程序属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!