ResourceBundle not found for MessageSource when placed inside a folder

Prasanth picture Prasanth · Jun 22, 2012 · Viewed 96.9k times · Source

I am trying to use resource bundles with Spring's Message Source. Here is the way I am doing it:

@Component
public class MessageResolver implements MessageSourceAware {

    @Autowired
    private MessageSource messageSource;

    public void setMessageSource(MessageSource messageSource) {
        this.messageSource = messageSource;
    }

    public String getMessage(){
        return messageSource.getMessage("user.welcome", new Object[]{"Rama"} , Locale.US);
    }

}

And here is my folder structure:

enter image description here

messages_en_US.properties contains just one line:

user.welcome=Welcome {0}

Here is the xml configuration used:

<bean name="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
    <property name="basename">
        <value>resourcebundles/messages</value>
    </property>
</bean>

Here is the error I am getting:

WARNING: ResourceBundle [resourcebundles/messages] not found for MessageSource: Can't find bundle for base name resourcebundles/messages, locale en_US
Exception in thread "main" org.springframework.context.NoSuchMessageException: No message found under code 'user.welcome' for locale 'en_US'.

But if I move my resource bundle to directly under the resources folder, it is working fine. In this case, here is the xml configuration I am using:

<bean name="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename">
    <value>messages</value>
</property>

Is is that if I have to use ResourceBundleMessageSource, I should put my resource bundles directly under the resources? If i have to keep it in specified folder only, is there any other way to get this one work?

Thanks!

Answer

jis117 picture jis117 · Aug 30, 2013

boy, maybe you can change the xml configuration as follows:

use

org.springframework.context.support.ReloadableResourceBundleMessageSource

instead of

org.springframework.context.support.ResourceBundleMessageSource

all configuration like this:

<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basename" value="classpath:resourcebundles/messages" />
    <property name="useCodeAsDefaultMessage" value="true" />
</bean>