使用Spring动态加载属性文件 [英] Dynamically loading properties file using Spring
问题描述
我编写了一个PropertyUtils类(来自互联网),它将加载属性
I have written a PropertyUtils class (from internet), which will load the Properties
<bean id="propertiesUtil" class="com.myApp.PropertiesUtil" >
<property name="locations">
<list>
<value>classpath:myApp/myApp.properties</value>
</list>
</property>
</bean>
和一个PropertiesUtil类如下所示
and a PropertiesUtil class is like below
public class PropertiesUtil extends PropertyPlaceholderConfigurer {
private static Map<String, String> properties = new HashMap<String, String>();
@Override
protected void loadProperties(final Properties props) throws IOException {
super.loadProperties(props);
for (final Object key : props.keySet()) {
properties.put((String) key, props.getProperty((String) key));
}
}
public String getProperty(final String name) {
return properties.get(name);
}
}
后来,我可以获得物业通过调用PropertiesUtil.getProperty()方法。
Later, I can get the property by calling PropertiesUtil.getProperty() method.
但现在我想略微修改它,如果myApp.properties被用户修改/更改,它应该是再次加载
But now I want to modify it slightly such that, If the myApp.properties get modified/changed by user, it should be loaded again
可能我需要FileWatcher类
Probably I need FileWatcher class
public abstract class FileWatcher extends TimerTask {
private long timeStamp;
private File file;
public FileWatcher(File file) {
this.file = file;
this.timeStamp = file.lastModified();
}
@Override
public final void run() {
long timeStampNew = this.file.lastModified();
if (this.timeStamp != timeStampNew) {
this.timeStamp = timeStampNew;
onChange(this.file);
}
}
protected abstract void onChange(File newFile);
}
但我怀疑是
- 如何使用类路径创建File对象:myApp / myApp.properties
(因为绝对路径未知) - 我如何调用/调用spring到
加载/将新的myApp.properties传递给PropetisUtil类[在
onChange方法中]。
推荐答案
您可以使用Spring的ReloadableResourceBundleMessageSource类,如下所示:
You can use Spring's ReloadableResourceBundleMessageSource class like below:
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
<bean id="myProperties" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<!-- check property file(s) every 1 second -->
<property name="cacheSeconds" value="1"/>
<property name="basenames">
<list>
<value>myApp/myApp</value>
</list>
</property>
</bean>
</beans>
然后你可以调用MessageSource.getMessage()方法来获取属性值。这是一个例子:
And then you can call MessageSource.getMessage() method to get the property value. Here is an example:
package com.example;
import org.springframework.context.ApplicationContext;
import org.springframework.context.MessageSource;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyApp {
public static void main(String[] args) throws InterruptedException {
ApplicationContext ctx = new ClassPathXmlApplicationContext("myApp/MyApp.xml");
MessageSource myProperties = (MessageSource) ctx.getBean("myProperties");
while (true) {
System.out.println(myProperties.getMessage("myApp.propertyOne", null, null));
Thread.sleep(1000);
}
}
}
您可以直接调用ApplicationContext。 getMessage(字符串代码,Object [] args,Locale语言环境)如果你将'myProperties'bean重命名为'messageSource'。
You can directly call ApplicationContext.getMessage(String code, Object[] args, Locale locale) if you renamed your 'myProperties' bean as 'messageSource'.
对于你的网络应用程序,请把你的属性Web应用程序类路径之外的文件(因为Web服务器可能会缓存它们)。例如,WEB-INF / conf / myApp.properties
For your web apps, please put your property files outside of your web app's classpath (because the web server might cache them). For instance, WEB-INF/conf/myApp.properties
这篇关于使用Spring动态加载属性文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!