使用Spring动态加载属性文件

Red*_*ddy 4 java resources spring configuration-files

我编写了一个PropertyUtils类(来自互联网),它将加载属性

<bean id="propertiesUtil" class="com.myApp.PropertiesUtil" >
    <property name="locations">
        <list>
            <value>classpath:myApp/myApp.properties</value>         
        </list>
    </property>
</bean>
Run Code Online (Sandbox Code Playgroud)

和一个PropertiesUtil类如下所示

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

}
Run Code Online (Sandbox Code Playgroud)

稍后,我可以通过调用PropertiesUtil.getProperty()方法获取属性.

但是现在我想略微修改它,如果myApp.properties被用户修改/更改,它应该再次加载

可能我需要FileWatcher类

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);
}
Run Code Online (Sandbox Code Playgroud)

但我的怀疑是

  1. 如何使用类路径创建File对象:myApp/myApp.properties(因为绝对路径未知)
  2. 如何调用/调用spring将新的myApp.properties加载/传递给PropetisUtil类[在onChange方法中].

Til*_*ill 10

以下内容支持您的需求:

https://commons.apache.org/proper/commons-configuration/userguide/howto_properties.html

PropertiesConfiguration config = new PropertiesConfiguration("usergui.properties");
config.setReloadingStrategy(new FileChangedReloadingStrategy());
Run Code Online (Sandbox Code Playgroud)

这应该可以解决问题.我自己没有用它,但它看起来很简单.


Ant*_*nyY 7

您可以使用Spring的ReloadableResourceBundleMessageSource类,如下所示:

<?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>
Run Code Online (Sandbox Code Playgroud)

然后,您可以调用MessageSource.getMessage()方法来获取属性值.这是一个例子:

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);
        }
    }
} 
Run Code Online (Sandbox Code Playgroud)

如果将"myProperties"bean重命名为"messageSource",则可以直接调用ApplicationContext.getMessage(String code,Object [] args,Locale locale).

对于您的Web应用程序,请将您的属性文件放在Web应用程序的类路径之外(因为Web服务器可能会缓存它们).例如,WEB-INF/conf/myApp.properties