In Spring, the ApplicationContext interface is an extension to BeanFactory. In addition to DI services,
the ApplicationContext also provides other services, such as transaction and AOP service, message
source for internationalization (i18n), and application event handling, to name a few.
In developing Spring-based application, it's recommended that you interact with Spring via the
ApplicationContext interface. Spring supports the bootstrapping of ApplicationContext by manual
coding (instantiate it manually and load the appropriate configuration) or in a web container
environment via the ContextLoaderListener. From this point onward, all the sample code in this
topic will use ApplicationContext.
Configuring ApplicationContext
Having discussed the basic concepts of IoC and DI and gone through a simple example of using Spring's
BeanFactory interface, let's dive into the details on how to configure a Spring application.
In the following sections, we will go through various aspects of configuring Spring applications.
Specifically, we will focus our attention on the ApplicationContext interface, which provides many more
configuration options than the traditional BeanFactory interface.
Spring Configuration Options (XML and Java Annotations)
Before we dive into the details of configuring Spring's ApplicationContext, let's take a look at the options
that are available for defining an application's configuration within Spring.
Originally, Spring supports defining beans either through properties or an XML file, and the XML file
was used by most Spring application developers for quite some time. Since the release of JDK 5 and
Spring's support of Java annotations, Spring (starting from Spring 2.5) also supports using Java
annotations when configuring ApplicationContext.
So, which one is better, XML or annotations? There have been lots of debates on this topic, and you
can find numerous discussions about this topic on the Internet (for example, try the Spring Community
Forum at There is no definite answer, and each approach has its pros
and cons. Using XML file can externalize all configuration from Java code, while annotations allow the
developer to define and view the DI setup from within the code. Spring also supports a mix of the two
approaches in a single ApplicationContext (the XML file configuration will override the annotation
ones). One common approach nowadays is to define the application infrastructure (e.g., data source,
transaction manager, JMS connection factory, JMX, etc.) in XML file, while defining the DI configuration
(injectable beans and beans' dependencies) in annotations. However, no matter which option you
choose, stick to it and deliver the message clearly across the entire development team. Agreeing on the
style to use and keeping it consistent across the application will make ongoing development and the
maintenance activities much easier.
To facilitate your understanding of both the XML and annotation configuration, we'll provide
sample code for XML and annotations side by side whenever appropriate.
Basic Configuration Overview
For XML configuration, you need to declare the required namespace base provided by Spring that your
application requires. Listing 4-13 shows the most basic sample, which declares only the beans
namespace for you to define the Spring beans.
Besides beans, Spring provides a large number of other namespaces for different purposes. Listing
4-14 is the namespace declaration for the Spring configuration that will be used by the samples in this
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks