This forum is now a read-only archive. All commenting, posting, registration services have been turned off. Those needing community support and/or wanting to ask questions should refer to the Tag/Forum map, and to http://spring.io/questions for a curated list of stackoverflow tags that Pivotal engineers, and the community, monitor.

How to access MessageSource from a Spring Service?

Jan 5th, 2005, 10:04 PM

I am using Spring to expose a several services that are being accesed by a rich client via HTTP. I have defined a messageSource bean in my application context, but I am unclear as to how my services should access this. My service implementors, being plane POJOs do not know about the ApplicationContext and therefore cannot call getMessage(..) on the app context. Should I just inject the message source into my service beans so that they can do resource lookup or is there a better best practice? Thanks!

You can either make the beans implement the marker interface MessageSourceAware, to have the context injected into them as a MessageSource, or if you do not want to introduce an unneeded spring dependency you can manually inject the "messageSource" bean itself, via a normal property declaration. The latter is normallhy preferred, if a little bit more verbose.