更新由Spring注入的属性文件以包含上次运行时间戳 [英] Updating a properties file injected by Spring to include a last run timestamp
问题描述
我有一个使用Spring加载的属性文件的应用程序。然后将Properties实例注入到几个类中。问题是这些属性中的一些被更新 - 例如,我们有一个lastRun时间戳,我们要在这里存储。也许有更好的方法来存储这样的东西(建议欢迎),但我怎么能去更新属性文件?
< util:properties id =propslocation =some.properties/>
props.store(...)
方法需要一个写或输出流(所有我假设是未知的,因为春天处理这个加载)..?
有没有更好的方法,或者我应该从Spring context.xml传递文件路径,并将其发送到各种bean并加载/存储属性文件老式的方式吗?
PropertiesFactoryBean没有location属性的访问器,但是你可以从BeanDefinition获得location属性。 / p>
BeanDefinition def = ctx.getBeanFactory()。getBeanDefinition(props);
String location = def.getPropertyValues()。getPropertyValue(location)。getValue();
档案档案= ctx.getResource(location).getFile();
编辑
<包括一个示例类来做到这一点。您可以在bean中定义bean的定义文件,并在适当的地方注入。
/ **
*属性
* /
公共类SpringPropertyUpdater实现ApplicationContextAware {
private ConfigurableApplicationContext ctx;
private static final String LOCATION_PROPERTY =location;
private static final Log log = LogFactory.getLog(SpringPropertyUpdater.class);
$ b / **
*使用新值更新托管属性
* /
public void updateProperties(String name,Properties props,String comments){
ConfigurableListableBeanFactory fb = ctx.getBeanFactory();
BeanDefinition bf = fb.getBeanDefinition(name);
String location =(String)bf.getPropertyValues()。getPropertyValue(LOCATION_PROPERTY).getValue();
Resource res = ctx.getResource(location);
尝试{
File file = res.getFile();
props.store(new FileOutputStream(file),comments);
catch(IOException e){
log.error(e);
$ b $ **
* {@inheritDoc}
* /
public void setApplicationContext(ApplicationContext applicationContext)throws BeansException {
this.ctx =(ConfigurableApplicationContext)applicationContext;
}
}
I have an application which makes used of a properties file loaded by Spring. The Properties instance is then injected into a few classes. The question is some of these properties are updated - for example we have a lastRun timestamp which we want to store here. Maybe there is a better way to go about storing something like this (suggestions welcome), but how can I go about updating the properties file?
<util:properties id="props" location="some.properties"/>
The props.store(...)
method requires either a write or an output stream (all of which I'm assuming is unknown as Spring handles this loading)..?
Is there a better approach or should I just be passing in the file path from the Spring context.xml and sending it to various beans and loading/storing the properties file the old fashioned way?
The PropertiesFactoryBean don't have accessor for location property but you can get the location property from BeanDefinition.
BeanDefinition def = ctx.getBeanFactory().getBeanDefinition("props");
String location = def.getPropertyValues().getPropertyValue("location").getValue();
File file = ctx.getResource(location).getFile();
EDIT
Include a sample class to do it. You can define the bean in bean the definition file and inject where appropiate.
/**
* Update Spring managed properties
*/
public class SpringPropertyUpdater implements ApplicationContextAware {
private ConfigurableApplicationContext ctx;
private static final String LOCATION_PROPERTY = "location";
private static final Log log = LogFactory.getLog(SpringPropertyUpdater.class);
/**
* Update managed properties with new value
*/
public void updateProperties(String name, Properties props, String comments) {
ConfigurableListableBeanFactory fb = ctx.getBeanFactory();
BeanDefinition bf = fb.getBeanDefinition(name);
String location = (String) bf.getPropertyValues().getPropertyValue(LOCATION_PROPERTY).getValue();
Resource res = ctx.getResource(location);
try {
File file = res.getFile();
props.store(new FileOutputStream(file), comments);
} catch (IOException e) {
log.error(e);
}
}
/**
* {@inheritDoc}
*/
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.ctx = (ConfigurableApplicationContext) applicationContext;
}
}
这篇关于更新由Spring注入的属性文件以包含上次运行时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!