【问题标题】:How to automatically reload messages.properties files in Java/Spring?如何在 Java/Spring 中自动重新加载 messages.properties 文件?
【发布时间】:2013-08-09 17:58:04
【问题描述】:

我一直在使用 Java/Spring 使用 #springMessage() 标签和 message.properties 文件开发一个国际网站。查看我最近的问题:In Java/Spring, how to gracefully handle missing translation values?

我希望能够编辑(覆盖)messages.properties 文件并能够在我的浏览器中立即看到新的翻译(无需重新启动 Tomcat)。

我认为http://commons.apache.org/proper/commons-configuration/userguide/howto_filebased.html#Automatic_Reloading 是我需要的,但我不确定如何编辑我的 webmvc-config.xml 以使用它。

【问题讨论】:

  • 重新加载属性文件可能还不够。如果您的任何 bean 依赖于文件中的值,则必须重新创建它们。这意味着抓取应用程序上下文并刷新它,重新生成其中的所有 bean。最好通过重新启动应用程序来做到这一点。你可以做的是有一个配置类,它引用一个不由 Spring 处理的配置文件。

标签: java spring tomcat spring-mvc


【解决方案1】:

想通了。在我编辑 webmvc-config.xml 后它起作用了:

<bean id="messageSource"
    class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basename">
        <value>${content.path.config}/WEB-INF/messages</value>
    </property>
    <property name="defaultEncoding" value="UTF-8" />
    <property name="cacheSeconds" value="2"/>
</bean>

(我只需要添加 cacheSeconds 属性。)

【讨论】:

    猜你喜欢
    • 2021-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多