您可以使用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" > <!-- 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 pre,Object []
args,Locale locale)。
对于您的Web应用程序,请将属性文件放置在Web应用程序的类路径之外(因为Web服务器可能会缓存它们)。例如,WEB-INF / conf /
myApp.properties
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)