当前位置: 首页 > 知识库问答 >
问题:

使用Spring动态加载属性文件

钦永贞
2023-03-14

我已经写了一个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。属性被用户修改/更改,应该重新加载

可能我需要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创建File对象:myApp/myApp.properties(因为绝对路径未知)
  2. 如何调用/调用Spring加载/传递新的myApp.properties到PropetisUtil类[在onChange方法中]。

共有3个答案

强才捷
2023-03-14

可能更简单的方法是这样做:

<util:properties location="classpath:config.properties" id="configProperties" />
<context:property-placeholder properties-ref="configProperties"  />

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

太叔小云
2023-03-14

您可以使用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,则可以直接调用Applicationontext.getMessage(String code, Object[]args, Locale locale)。

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

邰勇军
2023-03-14

以下内容支持您正在寻找的内容:

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

PropertiesConfiguration config = new PropertiesConfiguration("usergui.properties");
config.setReloadingStrategy(new FileChangedReloadingStrategy());

这应该能奏效。我自己也没用过,但似乎很简单。

 类似资料:
  • 问题内容: 我已经编写了一个PropertyUtils类(来自互联网),它将加载属性 而PropertiesUtil类如下所示 稍后,我可以通过调用PropertiesUtil.getProperty()方法来获取属性。 但是现在我要稍作修改,以便如果myApp.properties被用户修改/更改,则应再次加载 可能我需要FileWatcher类 但我的怀疑是 如何使用classpath:myA

  • 是否可以从服务器加载Spring Boot配置。json文件,而不是。亚马尔。房产?从文档来看,这是不支持开箱即用的——我想知道这是否可能,如果可能的话,人们将如何着手呢?

  • 我在web应用程序中工作。我正在使用spring分析加载基于环境的属性。下面是样品 在web.xml中,我们可以给出配置文件 现在,我的查询是针对我需要创建一个单独的Java类的每个环境。 问题:是否可能只有一个类,比如提供配置文件名称作为某个绑定参数,比如。 同时,让我知道是否有其他更好的选择,以实现同样的。

  • 我在尝试添加

  • 问题内容: 我希望对我在Spring中涉及到属性文件的问题有所帮助。所以我的设置是这样的: opto-mapping.properties –该文件位于我的src文件夹中,其中包含针对我的优化资源的翻译,如下所示: 每次运行“优化”构建时,都会更新此属性文件。然后我用 将属性文件导入所需的jsp中。然后通过使用以下内容引用内容: 除了属性文件需要重新启动tomcat重启外,所有这些工作都很漂亮。我

  • 问题内容: 我在Spring 3中使用属性文件。当Spring初始化其contex时,它将加载属性文件,并将其放入带有@Value批注的所有bean中。 我希望有可能更新文件中的某些属性,并在服务器上公开一个JMX,该JMX将新属性重新加载到Spring中-无需重新启动服务器并重新加载其上下文。 我可以通过使用一些 Spring方法 来重新加载属性并将其填充到所有bean中来实现此功能,还是应该自