chanjarster
chanjarster

Reputation: 526

Can Spring support multiple messages.properties files in classpath?

There are a JAR A and JAR B, both have messages.properties in classpath /

And there is WAR C, which has dependencies on JAR A and JAR B.

And when I start WAR C I can only get i18n message from JAR A or JAR B.

So, how can Spring support multiple messages.properties files in classpath?

BTW, WAR C is a spring boot project, and spring.messages.basename=messages

Upvotes: 4

Views: 8066

Answers (2)

chanjarster
chanjarster

Reputation: 526

Problem solved.

According to this question Does Spring MessageSource Support Multiple Class Path?

have to ditch ResourceBundleMessageSource and write a custom implementation of MessageSource (most likely by subclassing AbstractMessageSource) which uses PathMatchingResourcePatternResolver to locate the various resources and expose them via the MessageSource

I make a copy of ReloadableResourceBundleMessageSource and write the code by this guide and problem solved.

Upvotes: 2

ninja.coder
ninja.coder

Reputation: 9648

Yes, Spring supports loading multiple Properties. But properties should be unique. If we have same properties in the two Properties Files then the file which will be loaded later will override the previous Properties from previous file.

For example, if the Properties file from JAR A has two properties {USERNAME, PASSWORD} and JAR B Properties file also has the same two properties then you'll get {USERNAME, PASSWORD} from the file which is loading later.

You can use wildcard to import all the Property Files with same name on your classpath.

In Spring you can mention different Properties files in the form of Array as follows:

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

or

<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>

Upvotes: 1

Related Questions