0

私は春の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)

春が変更を選択できない理由と、問題を解決する方法を知りたいだけですか?

4

2 に答える 2

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>    
于 2014-01-27T13:39:12.667 に答える