【问题标题】:Can Spring support multiple messages.properties files in classpath?Spring 可以在类路径中支持多个 messages.properties 文件吗?
【发布时间】:2015-09-15 02:22:47
【问题描述】:

有一个 JAR A 和 JAR B,在类路径中都有 messages.properties /

还有WAR C,它依赖于JAR A和JAR B。

当我启动 WAR C 时,我只能从 JAR A 或 JAR B 获得 i18n 消息。

那么,Spring 如何在类路径中支持多个messages.properties 文件呢?

顺便说一句,WAR C 是一个 spring boot 项目,spring.messages.basename=messages

【问题讨论】:

    标签: spring internationalization spring-boot


    【解决方案1】:

    问题解决了。

    根据这个问题Does Spring MessageSource Support Multiple Class Path?

    必须放弃ResourceBundleMessageSource 并编写MessageSource 的自定义实现(很可能通过继承AbstractMessageSource),它使用PathMatchingResourcePatternResolver 定位各种资源并通过MessageSource 公开它们

    我复制了ReloadableResourceBundleMessageSource 并按照本指南编写代码并解决了问题。

    【讨论】:

      【解决方案2】:

      是的,Spring 支持加载多个属性。但是属性应该是唯一的。如果我们在两个属性文件中具有相同的属性,那么稍后将加载的文件将覆盖先前文件中的先前属性。

      例如,如果 JAR A 中的属性文件有两个属性 {USERNAME, PASSWORD} 并且 JAR B 属性文件也有相同的两个属性,那么您将从稍后加载的文件中获得 {USERNAME, PASSWORD}。

      您可以使用通配符在您的类路径中导入所有同名的属性文件。

      在Spring中你可以用Array的形式提到不同的Properties文件,如下:

      <context:property-placeholder
      location="classpath:war.properties,
                classpath*:message.properties"
      ignore-unresolvable="true"/>
      

      <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
          <property name="locations">
              <list>
                  <value>classpath:war.properties</value>
                  <value>classpath*:message.properties</value>
              </list>
          </property> 
          <property name="ignoreUnresolvablePlaceholders" value="true"/>
      </bean>
      

      【讨论】:

      • 谢谢,WAR C是一个spring boot项目,MessageSource是由MessageSourceAutoConfiguration自动配置的。我怎样才能让它工作?
      猜你喜欢
      • 2018-04-12
      • 1970-01-01
      • 2011-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-26
      • 2018-11-09
      • 1970-01-01
      相关资源
      最近更新 更多