Spring可以在classpath中支持多个messages.properties文件吗?

时间:2015-09-15 02:22:48

标签: spring internationalization spring-boot

有一个JAR A和JAR B,在classpath /

中都有messages.properties

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

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

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

BTW,WAR C是一个春季启动项目,spring.messages.basename=messages

2 个答案:

答案 0 :(得分:3)

问题解决了。

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

  

必须抛弃ResourceBundleMessageSource并编写MessageSource的自定义实现(最有可能通过继承AbstractMessageSource),它使用PathMatchingResourcePatternResolver来查找各种资源并通过{{1}公开它们}

我制作MessageSource的副本并按照本指南编写代码并解决问题。

答案 1 :(得分:0)

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

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

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

在Spring中,您可以按照以下形式提及不同的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>