You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Spring框架中使用Apache Commons PropertiesConfiguration作为MessageSource?

看起来你正在用Spring 4.3.1.RELEASE版本的ReloadableResourceBundleMessageSource来处理国际化消息,我把你给出的Bean配置整理成清晰的格式,顺便补充点实用说明:

国际化消息源配置(Spring 4.3.1.RELEASE)
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basenames">
        <list>
            <value>classpath:messages/messages</value>
        </list>
    </property>
    <property name="cacheSeconds" value="1" />
    <!-- 你这里的default开头的属性内容被截断了,常见的补充配置比如编码设置:
    <property name="defaultEncoding" value="UTF-8" />
    -->
</bean>

关键配置项解析:

  • basenames:指定国际化资源文件的基础路径,这里指向类路径下的messages/messages,实际运行时会自动加载对应语言后缀的文件,比如messages_en.properties(英文)、messages_zh_CN.properties(简体中文)等。
  • cacheSeconds:设置缓存时间为1秒,这个配置非常适合开发阶段——修改资源文件后不用重启应用,等待1秒就能看到更新;生产环境建议调整为更长时间(比如3600秒)或者设为-1启用永久缓存,提升性能。
  • defaultEncoding(补充建议):几乎都要加上这个配置,指定资源文件的编码格式(比如UTF-8),避免读取中文等非ASCII字符时出现乱码问题。

内容的提问来源于stack exchange,提问作者Sebastian D'Agostino

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 04:20:45