如何使用注释在 Spring 4 中重新加载属性文件? [英] How can I reload properties file in Spring 4 using annotations?
问题描述
我有一个简单的应用程序,我在其中使用多个属性文件来获取其他用户编辑的内容(站点链接等).
I have a simple application where i am using several properties-files to fetch content edited by other users (links to sites etc).
我加载属性的类如下所示:
The class where i load the properties look like this:
@Configuration
@PropertySource("classpath:salestipsWhitelist.properties")
public class SalestipsWhitelist {
@Autowired
Environment env;
public Environment getEnv() {
return env;
}
public void setEnv(Environment env) {
this.env = env;
}
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
}
一些属性文件:
UPS_MPP_M_L=True
UPS_MPP_M_M=True
UPS_MPP_M_MP=True
UPS_MPP_M_S=True
这工作正常,但如果我对属性文件进行更改,我必须重新加载应用程序以查看所做的任何更改.
This works fine, but if i make changes to the properties-file, i have to reload the app to visualise any changes made.
如果我将位置移动到磁盘而不是类路径,是否可以定期或手动重新加载?我不希望这在更改时自动完成,因为我想控制何时完成/更新.
Is it possible, if i move the location to disk instead of classpath, to reload this periodically or manually? I do not want this to be done automatically on change, as i want to have control over when this is done / updated.
推荐答案
这是一种享受.需要 Java 7、Apache commons logging、Apache commons lang (v2.6) 和 Apache commons 配置:
This works a treat. Requires Java 7, Apache commons logging, Apache commons lang (v2.6) and Apache commons Configuration:
package corejava.reloadTest;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy;
public class MyApplicationProperties {
private static PropertiesConfiguration configuration = null;
static {
try {
configuration = new PropertiesConfiguration("test.properties");
} catch (ConfigurationException e) {
e.printStackTrace();
}
configuration.setReloadingStrategy(new FileChangedReloadingStrategy());
}
public static synchronized String getProperty(final String key) {
return (String) configuration.getProperty(key);
}
}
并使用以下方法进行测试:
and test it with:
package corejava.reloadTest;
public class TestReloading {
public static void main(String[] args) {
while (true) {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(MyApplicationProperties.getProperty("key"));
}
}
}
更改test.properties时的输出是这样的(test.props的原始内容是key=value,后来在程序执行中改为key=value1):
Output when you change test.properties is something like this (original contents of test.props was key=value, later changed to key=value1 mid-program execution):
value
value
value
value
value
Jan 17, 2015 2:05:26 PM org.apache.commons.configuration.PropertiesConfiguration reload
INFO: Reloading configuration. URL is file:/D:/tools/workspace /AutoReloadConfigUsingApacheCommons/resources/test.properties
value1
value1
value1
您还可以考虑官方 Spring 框架参考文档 Refreshable beans ,为此使用 DSL(例如 Groovy).
You could also consider Official Spring Framework Reference Documentation Refreshable beans , using a DSL like Groovy for this.
这篇关于如何使用注释在 Spring 4 中重新加载属性文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!