jump to navigation

Spring Aggregating MessageSource March 26, 2010

Posted by Phill in General J2EE, Spring.
Tags: , , ,
comments closed

If you’ve ever worked on a modular project which needs to be internationalised, chances are you’ll have run into the requirement of combining messages from various different modules.

By that I mean, each module has its own message source, but you want them to be combined into one MessageSource. This could be required for various different reasons which I won’t go into here, but suffice it to say that this is possible with Spring.

I created an AggregatingMessageSource which would scan the ApplicationContext for instances of MessageSource. When a message was requested, it would search each one sequentially for the relevant message.

Anyway, if you need such a class, I have uploaded it to get you started. Obviously, customise it to your own needs: AggregatingMessageSource.java.

More on JSF and Spring Integration: MessageSources June 18, 2008

Posted by Phill in Frameworks.
Tags: , , ,
comments closed

A couple of days ago I wanted to move my application messages file out of the classpath into WEB-INF for some reason which I can’t actually remember now. Anyway, Spring definitely supports loading message files from WEB-INF, but I don’t think JSF does (I think it loads resources from the classpath).

Plus, it makes sense to have all messages loaded from the one MessageSource – that way, if I add another messages file, the configuration will only need to be changed in one place.

After a bit of Googling, I managed to get it to work. If you want to have JSF using a Spring MessageSource, you will need to use the following class: MessageSourcePropertyResolver. Copy and paste that into your codebase somewhere. Then, edit your faces-config.xml file and add in the appropriate element:


Then, you can use the MessageSource in your JSF pages like this:#{messageSourceBeanName[‘message.key’]}

Tip: I don’t like typing out “messageSource” every time I want to use a message in a JSF page – I always used the shorter “msg” before. You can do this with Spring – just alias your MessageSource bean with the name “msg”. For example,

<alias name=”messageSource” alias=”msg” />