Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

External properties file as spring MessageSource not working

Tags:

java

spring

Consider below code:

<bean id="busmessageSource" 
        class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basenames">
        <list>
            <value>classpath:bundles/resource</value>
            <value>classpath:bundles/override</value>
            <value>file:/C:/mmt/override</value>
        </list>
    </property>
    <property name="cacheSeconds" value="100" />
</bean>

Here properties from bundles/resource and bundles/override get fetched when I call busmessageSource.getMessage("anykey", null, null)

but it fails when I try to fetch values for properties in C:/mmt/override

  1. What is correct way of configuring messagesource with external file from the disk.
  2. Also I want file:/C:/mmt/override to override values in classpath:bundles/override if any with the same key exist. How do I override properties from an external file outside of my war folder?
like image 712
Saurab Parakh Avatar asked Dec 08 '22 10:12

Saurab Parakh


2 Answers

1.) I have these 3 ways:

  • One solution is to add your "C:/mmt/" folder to your resource classpath.
  • This is another way that may help you ResourceBundle not found for MessageSource when placed inside a folder
  • Use this code: (Worked for me)
<beans:bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <beans:property name="basename">
        <beans:value>file:/path/to/messages</beans:value>
    </beans:property>
</beans:bean>

Note1: You must use the } file: prefix and the ReloadableResourceBundleMessageSource class.

Note2: Do not put the ".properties" extension.

2.) You override previous values when you load a new properties file with same property names (keys). You must ensure that you fetch last the properties file you want to use.

like image 133
Armando Avatar answered Dec 11 '22 09:12

Armando


You can try

<bean id="messageSource"
  class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="defaultEncoding" value="UTF-8"/>
    <property name="basenames">
        <list>
            <value>classpath:bundles/resource</value>
            <value>classpath:bundles/override</value>
            <value>file:C:/mmt/override</value>
        </list>
    </property>
</bean>

About message resource keep note:

  1. A plain path will be relative to the current application context.
  2. A "classpath:" URL will be treated as classpath resource.
  3. A "file:" URL will load from an absolute file system path.
  4. Any other URL, such as "http:", is possible too.
like image 45
Kamal Singh Avatar answered Dec 11 '22 09:12

Kamal Singh