我是春季 3.2.2 的新手。我试图在运行时使用 spring 提供的设施 ReloadableResourceBundleMessageSource 重新加载资源包。但它似乎没有按预期工作。我为此创建了以下独立的 Java 应用程序示例。
请找到以下应用程序上下文 xml 文件和示例的源代码。
<?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.0.xsd">
<bean name="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basenames">
<list>
<value>classpath:standard</value>
<value>com/springinterlocalization/format</value>
<value>file:/D:/Jinesh/jinesh.properties</value>
</list>
</property>
<property name="cacheSeconds" value="1"/>
</bean>
</beans>
import java.io.FileOutputStream;
import java.io.IOException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author Krunali
*
*/
public class TestSpringInterLocalization {
/**
* @param args
*/
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
ApplicationContext ctx=new ClassPathXmlApplicationContext("com/springinterlocalization/springinternationalizationcontext.xml");
System.out.println(ctx.getMessage("jinesh.thirdtest", null, null));
System.out.println("Updating the properties file");
FileOutputStream fos = null;
try {
fos = new FileOutputStream("C:\\workspace\\SpringExamples\\src\\standard.properties");
fos.write("jinesh.latestaddproperty=this is the newly added property."
.getBytes("UTF-8"));
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("going to sleep for 10 secs while the bundles are reloaded");
Thread.sleep(10 * 1000);
System.out.println(ctx.getMessage("jinesh.latestaddproperty", null, null));
}
}
在上面的示例中,我尝试将jinesh.latestaddproperty添加到类路径standard.properties文件中的属性文件中,但似乎该属性已添加到属性文件中,但 spring 无法获取该更改并给出以下错误.
Oct 28, 2013 12:46:13 AM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@75dc818d: startup date [Mon Oct 28 00:46:13 IST 2013]; root of context hierarchy
Oct 28, 2013 12:46:13 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [com/springinterlocalization/springinternationalizationcontext.xml]
Oct 28, 2013 12:46:14 AM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@6af7de45: defining beans [messageSource]; root of factory hierarchy
Updating the properties file
going to sleep for 10 secs while the bundles are reloaded
Exception in thread "main" org.springframework.context.NoSuchMessageException: No message found under code 'jinesh.latestaddproperty' for locale 'null'.
at org.springframework.context.support.AbstractMessageSource.getMessage(AbstractMessageSource.java:155)
at org.springframework.context.support.AbstractApplicationContext.getMessage(AbstractApplicationContext.java:1234)
at com.springinterlocalization.TestSpringInterLocalization.main(TestSpringInterLocalization.java:37)
我只想知道为什么 spring 无法选择更改以及如何解决问题?