如何在Spring中动态维护,更新应用程序属性? [英] How to maintain, update application properties dynamically in Spring?

查看:120
本文介绍了如何在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屋!

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