tags:

views:

3040

answers:

3

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.

A: 

have you tried to define the method as:

public void setMessageSource(ReloadableResourceBundleMessageSource messageSoure) {
    this.messageSoure = messageSoure;
}
Vladimir
Yes, that what I first did, but that failed on initialization with an error about not finding a bean of type ReloadableResourceBundleMessageSource.
Joel
+1  A: 

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.

springify
I have already tried wiring the bean via the XML configuration but I get the same results. I haven't tried @Qualifier yet. I'll give that a go. Thanks.
Joel
Autowiring by name, which is @Qualifier's fallback method, will not help in that case, either. Using the <qualifier> element with your MessageSource definition (http://static.springframework.org/spring/docs/2.5.x/reference/beans.html#beans-autowired-annotation-qualifiers) might do the trick.
springify
Alternatively, make your MessageSource client ApplicationContextAware and access the 'messageSource' bean programatically.
springify
As springify pointed out, if you have an @autowired MessageSource, then Spring will always give you a reference to the application context's own internal MessageSource, which delegates to any others it may have found. If you don't want this, then remove the @autowired annotation completely, and manually inject your MessageSource bean. Don't mess about with @Qualifier or the like, it doesn't need to be that complicated.
skaffman
A: 

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.