Spring ReloadableResourceBundleMessageSource问题

时间:2013-10-27 19:19:33

标签: java spring spring-mvc

我是第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无法选择更改以及如何解决问题?

2 个答案:

答案 0 :(得分:0)

这可能是因为jvm正在缓存特定于类路径的属性文件,因此Spring不会在属性文件中看到任何更改以重新加载。

而是引用绝对文件并使用类路径外的属性文件重试测试,您应该看到测试工作正常。

参考:来自javadoc

  

由于应用程序服务器通常会缓存从中加载的所有文件   classpath,有必要将资源存储在其他地方(for   例如,在&#34; WEB-INF&#34;网络应用程序的目录)。否则改变   类路径中的文件不会反映在应用程序中。

  

使用&#34;类路径:&#34;前缀,资源仍然可以从中加载   classpath,但&#34; cacheSeconds&#34; &#34; -1&#34;以外的值(永远缓存)   * 在这种情况下不起作用 *。

答案 1 :(得分:0)

除了Biju在关于.properties扩展的回答中所说的内容之外,file:D:之间不应有任何斜杠。

结果:

<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</value>
            </list>
        </property>
        <property name="cacheSeconds" value="1"/>
    </bean>
</beans>