2013-04-06 41 views
2

我在做一個Spring web應用程序。我使用Spring 3.1和Eclipse。我在Eclipse中通過Jetty運行應用程序。在不重新啓動應用程序的情況下修改Spring消息文本?

我有一個包含文本,如這許多JSP頁面:

<spring:message code="label.subject"/> 

這種類型的文本來自於Spring上下文中定義一個名爲messages_en.properties:

<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> 
<property name="basenames"> 
<list> 
<value>messages</value> 
</list> 
</property> 
</bean> 

我需要不斷修改messages_en.properties中的文本。但是,如果不重新啓動Jetty,新文本不會顯示在應用程序中,這對我來說很不方便。

如何在不重新啓動Jetty的情況下修改Spring消息文本?

感謝您的幫助!

問候。

回答

0

你可以用很多方式做到這一點。您可以使用文件監視器監視文件以進行更改,並以編程方式重新加載資源。你可以看看這個:http://docs.oracle.com/javase/tutorial/essential/io/notification.html

基本上這個想法是一開始你的應用程序註冊一個文件監視器在你的資源,然後當你改變它,只是重新初始化你的資源。

+0

感謝您提供這種通用解決方案。很高興知道! – curious1 2013-04-06 19:11:58

3

答案在ResourceBundleMessageSource's documentation itself:使用ReloadableResourceBundleMessageSource

<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> 
    <property name="basenames"> 
    <list> 
     <value>messages</value> 
    </list> 
    </property> 
    <property name="cacheSeconds" value="1"/> 
</bean> 
+0

這正是我正在尋找的東西,在Spring中構建的東西。謝謝! – curious1 2013-04-06 18:13:07

+0

我在Eclipse(juno)+ Jetty 8.1.2 + Java 7中使用了你的配置,它似乎無法正常工作。你知道爲什麼嗎? – curious1 2013-04-07 06:33:07

+0

讓我猜...你正在編輯src /下的messages.properties,而不是目標/?我剛剛嘗試複製您的設置並犯了這個錯誤。 – kryger 2013-04-07 22:47:02

相關問題