使用Spring动态加载属性文件

5

我写了一个PropertyUtils类(从互联网上),它将加载属性。

<bean id="propertiesUtil" class="com.myApp.PropertiesUtil" >
    <property name="locations">
        <list>
            <value>classpath:myApp/myApp.properties</value>         
        </list>
    </property>
</bean>

而一个 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);
    }

}

稍后,我可以通过调用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);
}

但是我有疑问:

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

在你的方法loadProperties()中,你循环每个属性是否有特定的原因?你不能使用一个以Properties为参数的Properties类构造函数吗? - Parvez
3个回答

10

这个链接提供了一个很好的介绍。 - Till
Commons configuration 2.x不再使用FileChangedReloadingStrategy,并已被替换。请参见https://commons.apache.org/proper/commons-configuration/userguide/upgradeto2_0.html#Reloading。 - 2Aguy

9

您可以像下面这样使用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>

然后您可以调用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);
        }
    }
} 

如果你将“myProperties” bean 重命名为“messageSource”,那么你可以直接调用 ApplicationContext.getMessage(String code, Object[] args, Locale locale)。
对于你的网络应用程序,请将属性文件放在网络应用程序类路径之外(因为网络服务器可能会缓存它们)。例如,WEB-INF/conf/myApp.properties。

你能举个例子说明如何获取属性吗?org.springframework.context.ApplicationContext.getMessage() 这个方法不存在。 - RaceBase

-1
也许更简单的方法是这样做:
<util:properties location="classpath:config.properties" id="configProperties" />
<context:property-placeholder properties-ref="configProperties"  />

以这种方式,您可以通过注入/访问名称为“configProperties”的bean从任何bean中访问您的属性。 不确定这是否是您所需的,因为无法在运行时修改configProperties。但这是一种清洁的方式,您不需要编写任何其他类。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接