2013-10-27 132 views
0

我是新來的春天3.2.2。我試圖在運行時使用Spring提供的工具ReloadableResourceBundleMessageSource重新加載資源包。但它似乎不能按預期工作。我已經創建了以下獨立的Java應用程序示例。春季問題ReloadableResourceBundleMessageSource

請查找下面的應用程序上下文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文件,但它似乎屬性被添加到屬性文件,但彈簧不能夠接受這種變化並給出下面的錯誤。

Oct 28, 2013 12:46:13 AM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh 
INFO: Refreshing org[email protected]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.s[email protected]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不能挑選變化以及如何解決問題?

回答

0

這可能是因爲classpath特定的屬性文件正在被jvm緩存,所以Spring將不會再看到屬性文件中的任何更改以重新加載。

改爲引用絕對文件並使用classpath外部的屬性文件重試您的測試,您應該看到測試正常工作。

參考:從javadoc

由於應用服務器通常緩存從 classpath中加載的所有文件,有必要存儲資源在其他地方(爲 例如,在的「WEB-INF」目錄一個網絡應用程序)。否則,類路徑中文件的更改 將不會反映在應用程序中。

隨着 「類路徑:」 前綴,資源仍然可以從 classpath中加載,但 「cacheSeconds」 值不是 「-1」(緩存永遠) * 在這種情況下不起作用 *。

+0

你的答案,但由於春季是六必居不能從文件中的絕對路徑讀爲好。我在該文件(jinesh.propeties)中添加了簡單屬性,但spring無法從該文件讀取任何屬性。任何想法 ? – Beast

+1

我認爲絕對文件的問題是不需要指定擴展名,所以只需要指定basename爲'file:/D:/Jinesh/jinesh'而不是'file:/D:/Jinesh/jinesh.properties'。另外,我在本地嘗試了類似的設置,並且我看到了房地產的變化。 –

+0

即使刪除.properties擴展名,我仍然面臨同樣的問題。我無法讀取文件文件中指定的任何屬性:/ D:/ Jinesh/jinesh。你有什麼想法爲什麼這是行不通的? – Beast

0

除了什麼在六必居他的回答說,關於.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>