当前位置: 首页 > 面试题库 >

如何在Spring 4中使用注释重新加载属性文件?

莫兴言
2023-03-14
问题内容

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

我加载属性的类如下所示:

@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

这可以正常工作,但是如果我对properties-file进行更改,则必须重新加载应用程序以可视化所做的任何更改。

如果我将位置移动到磁盘而不是类路径,是否可以定期或手动重新加载?我不希望更改时自动完成此操作,因为我想控制何时完成/更新。


问题答案:

需要Java 7,Apache Commons日志记录,Apache Commons lang(v2.6)和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的原始内容为key = value,后来在程序执行中更改为key = value1):

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

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

  • 我最近开始使用带有Page对象模式的Selenium2和Page Factory。我使用@FindBy注释声明了WebElements,这些注释在初始化类时由PageFactory初始化。但是,我希望将@findby注释与locators.properties文件一起使用。不幸的是,我似乎无法做到这一点,因为注释被限制为只允许常量表达式。一般来说,这似乎是Java注释的一个限制,但我只是想知道是否

  • 问题内容: 这是我想在属性文件中执行的操作 现在我能处理的文件是 有什么办法做这样的事情 问题答案: 您可以使用Apache的共享配置写入和读取属性的文件,特别是setComment()函数中的PropertiesConfigurationLayout它允许您指定每个属性的注释。 请注意,上述链接是指Commons Configuration v1.x,而同时发布了v2.0,它具有不同的程序包名称

  • 问题内容: 我有一个申请。在运行文件夹下,还有一个附加的配置文件: 应用程序启动时,它将使用文件中的值并将其注入到: 问题:如何触发这些属性的重新加载?我希望能够在运行时更改配置,并更新字段(也许可以通过在应用程序内部调用servlet 触发更新)。 但是如何? 问题答案: 使用下面的bean每1秒重新加载config.properties。 你的主要配置如下所示: 而不是使用@Value,每次你