2013-04-06 39 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

相关问题