Saurab Parakh
Saurab Parakh

Reputation: 1072

External properties file as spring MessageSource not working

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?

Upvotes: 4

Views: 14769

Answers (3)

jediz
jediz

Reputation: 4717

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.

Upvotes: 1

Kamal Singh
Kamal Singh

Reputation: 1000

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.

Upvotes: 5

Armando
Armando

Reputation: 310

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.

Upvotes: 9

Related Questions