In the following sections, we will discuss some of the most important features in ApplicationContext
besides DI.
Internationalization with MessageSource
One area where Spring really excels is in support for internationalization (i18n). Using the MessageSource
interface, your application can access String resources, called messages, stored in a variety of different
languages. For each language you want to support in your application, you maintain a list of messages
that are keyed to correspond to messages in other languages. For instance, if I wanted to display "The
quick brown fox jumped over the lazy dog" in English and in Czech, I would create two messages, both
keyed as msg; the one for English would say "The quick brown fox jumped over the lazy dog," and the one
for Czech would say "Príšerne zlutoucký kun úpel dábelské ódy."
Although you don't need to use ApplicationContext to use MessageSource, the ApplicationContext
interface actually extends MessageSource and provides special support for loading messages and for
making them available in your environment. The automatic loading of messages is available in any
environment, but automatic access is provided only in certain Spring-managed scenarios, such as when
you are using Spring's MVC framework to build a web application. Although any class can implement
ApplicationContextAware and thus access the automatically loaded messages, we suggest a better
solution later in this chapter in the section "Using MessageSource in Stand-Alone Applications."
Before we continue, if you are unfamiliar with i18n support in Java, we suggest that you at least
check out the JavaDocs ( for the Locale
and ResourceBundle classes.
Using ApplicationContext and MessageSource
Aside from ApplicationContext, Spring provides three MessageSource implementations:
ResourceBundleMessageSource, ReloadableResourceBundleMessageSource, and StaticMessageSource.
The StaticMessageSource is not really meant to be used in a production application because you can't
configure it externally, and this is generally one of the main requirements when you are adding i18n
capabilities to your application. The ResourceBundleMessageSource loads messages using a Java
ResourceBundle. ReloadableResourceBundleMessageSource is essentially the same, except it supports
scheduled reloading of the underlying source files.
All of the three MessageSource implementations also implement another interface called
HierarchicalMessageSource, which allows for many MessageSource instances to be nested. This is key to
the way ApplicationContext works with MessageSources.
To take advantage of ApplicationContext's support for MessageSource, you must define a bean in
your configuration of type MessageSource and with the name messageSource. ApplicationContext takes
this MessageSource and nests it within itself, allowing you to access the messages using the
ApplicationContext. This can be hard to visualize, so take a look at the following example.
Listing 5-33 shows a simple application that accesses a set of messages for both the English and
Czech locales.
Listing 5-33. Exploring MessageSource Usage
package com.apress.prospring3.ch5.context;
import java.util.Locale;
public class MessageSourceDemo {
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks