My messages.properties is really a big file. So, I tried moving some of the properties in messages.properties to a new file, say newmessages.properties and updated spring bean configuration xml with both the files as follows:
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:i18n/messages"/>
<property name="defaultEncoding" value="UTF-8"/>
</bean>
<bean id="anotherMessageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:i18n/newmessages"/>
<property name="defaultEncoding" value="UTF-8"/>
</bean>
But, I am not able access any properties defined in the new property file. Is it really possible to specify multiple property files(for a single locale)?
The basenames (s
at the end) property accept an array of basenames:
Set an array of basenames, each following the above-mentioned special convention. The associated resource bundles will be checked sequentially when resolving a message code.
@see java doc: ReloadableResourceBundleMessageSource.setBasenames
So you should have only one messages source, with a list files (try to seperatate them by comma).
<bean id="anotherMessageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basenames" value="classpath:i18n/newmessages,classpath:i18n/messages"/>
<property name="defaultEncoding" value="UTF-8"/>
</bean>
Another clean way to doing same:
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basenames">
<list>
<value>classpath:messages1</value>
<value>classpath:messages2</value>
</list>
</property>
<property name="defaultEncoding" value="UTF-8"/>
</bean>
Alternative solution to those already mentioned would be using the property parentMessageSource
that delegates the message lookup to the parent if it does not find it in the current instance.
In your case it is probably better to stay with the basenames
array. Having the hierarchic message source could make more sense if the message sources were using different implementations. E.g. the second one reading messages from db.
Note that in this case, when Spring finds two instances of MessageSource
, so the primary one will be the one with the id messageSource
.
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="parentMessageSource"><ref bean="anotherMessageSource"/></property>
<property name="basename" value="classpath:i18n/messages"/>
<property name="defaultEncoding" value="UTF-8"/>
</bean>
<bean id="anotherMessageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:i18n/newmessages"/>
<property name="defaultEncoding" value="UTF-8"/>
</bean>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With