我怎样才能重新加载在Spring 4属性文件中使用注释? [英] How can I reload properties file in Spring 4 using annotations?

查看:278
本文介绍了我怎样才能重新加载在Spring 4属性文件中使用注释?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的应用程序,其中我用几个属性,文件获取其他用户编辑内容(网站链接等)。

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();
   }
}

一些属性的文件:

Some of the properties file:

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的公共记录,阿帕奇公地郎(V2.6)和Apache公地配置:

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);
    }
}

和测试它:

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的​​原始内容是关键=值,后改为键=值中旬程序执行):

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框架参考文档 刷新的豆,使用 DSL 如Groovy这一点。

You could also consider Official Spring Framework Reference Documentation Refreshable beans , using a DSL like Groovy for this.

这篇关于我怎样才能重新加载在Spring 4属性文件中使用注释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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