4

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?
jediz
  • 4,459
  • 5
  • 36
  • 41
Saurab Parakh
  • 1,054
  • 3
  • 12
  • 19

3 Answers3

9

1.) I have these 3 ways:

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

Community
  • 1
  • 1
Armando
  • 310
  • 5
  • 18
  • If you want to RELOAD a properties file during a program execution you can check this post http://learningviacode.blogspot.com/2012/07/reloadable-messagesources.html – Armando Apr 16 '14 at 00:31
  • What did the trick for me was indeed removing the ".properties" at the end and adding "file:" at the start. Thank you for pointing that in this answer. – user1884155 Jul 30 '18 at 16:27
5

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.
Kamal Singh
  • 990
  • 8
  • 13
  • Do you know where "a plain path" be related to? It cannot find the properties under the project folder or the same folder of the java file. – hychou Dec 20 '17 at 08:47
  • 1
    Can you provide the folder structure you are using? I termed "plain path" from application context. The application context in web application is actually the directory which contains **WEB-INF**. For maven based project structure it is in **ProjectName/src/main/webapp/**. Suppose your file name is **application.properties** which resides in same directory as **WEB-INF**. Than the value should be **application**. If it is under **WEB-INF** than use **WEB-INF/application**. If still does not solve please feel free to comment again. – Kamal Singh Dec 21 '17 at 11:31
1

I ran into similar question (by the title of your question) and managed to instantiate Spring ResourceBundleMessageSource with java.util.Properties.

ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
messageSource.setCommonMessages(properties);

Kind of naive approach, but did the job for my unit testing.

jediz
  • 4,459
  • 5
  • 36
  • 41