Joel
Joel

Reputation: 1486

How to Inject Spring ReloadableResourceBundleMessageSource

In order to programmatically refresh the resource bundle cache, I am using Spring's ReloadableResourceBundleMessageSource. I am having trouble injecting it into my bean where I want to invoke the clearCache() method.

I've had to resort to the following:

private ReloadableResourceBundleMessageSource messageSource;

@Autowired
public void setMessageSource(MessageSource messageSource) {
  this.messageSource = (ReloadableResourceBundleMessageSource((DelegatingMessageSource)messageSource).getParentMessageSource();
}

This works, but there must be a better way. The message resource is defined as follows:

<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basenames" >
<list>
   <value>WEB-INF/content/Content</value>
 </list>
 </property>
</bean>

I don't understand why Spring is injecting a message source of type DelegatingMessageSource.

Upvotes: 4

Views: 19124

Answers (4)

Muhammad Imran Tariq
Muhammad Imran Tariq

Reputation: 23352

When you try to run it through messageSource in your controller, you get NOTHING, empty string. And if you look closely, you will find that you have a DelegatingMessageSource in your messageSource property, with an empty parent source, which means it is EMPTY, i.e. always returns blank and this exception occurs in controller

ReloadableResourceBundleMessageSource incompatible with org.springframework.context.support.DelegatingMessageSource

Read more..

Upvotes: 0

Bill
Bill

Reputation:

You usually get the DelegatingMessageSource injected when Spring can't find "messageSource" defined. Are you sure you're defining it properly or that it's visible where necessary? I think the problem here is how the XML configuration has been setup.

I had a similar situation with Spring Web Flow and the form action stuff. In my XML configuration the "messageSource" wasn't visible and causing the DelegatingMessageSource to be injected. I placed the "messageSource" bean definition into the webflow configuration and then everything worked and I stopped getting the DelegationMessageSource object. However, this is an ugly fix since now I have "messageSource" defined in two places.

Anyway, this problem only started after I switched to Spring 2.5.6. I'm using Webflow 1. Once I have a chance I will try and update to Webflow 2 and see what happens. Maybe that will fix the issue.

Upvotes: 2

Nils Wloka
Nils Wloka

Reputation: 1483

I don't think that autowiring by type will work in this case, as the autowire candidate will most likely be the ApplicationContext itself (see section 3.8.2 of the reference documentation). This leads to all those layers you have to dig through to get your original ReloadableResourceBundleMessageSource.

Try passing a reference to the messageSource bean via XML configuration instead. Annotating the property with @Qualifier('messageSource') should work as well.

Upvotes: 3

Vladimir
Vladimir

Reputation: 6871

have you tried to define the method as:

public void setMessageSource(ReloadableResourceBundleMessageSource messageSoure) {
    this.messageSoure = messageSoure;
}

Upvotes: 0

Related Questions