the life cycle of the dependencies. The client also becomes much more testable. The client has
no environment-specific code to tie it to a particular framework.
Dependency Injection is a core concept of the Spring Framework, and we will take advan-
tage of it throughout all the code that appears in this book. This has been a high-level overview
of the capabilities of IoC. You may find more helpful information in Pro Springor via Martin
Fowler’s Dependency Injection article (http://www.martinfowler.com/articles/injection.html).
Spring ApplicationContexts
Looking specifically to the Spring Framework, we now turn our attention to the
ApplicationContext. It is this object that actually forms the heart and soul of a Spring
application. It is inside the ApplicationContextthat the actual Dependency Injection is per-
formed. If Dependency Injection is the core concept of Spring, then the ApplicationContextis
its core object.
The ApplicationContextis a specialization of a BeanFactory, which is the registry of all the
objects managed by Spring. Under normal circumstances, the BeanFactoryis responsible for
creating the beans, wiring them with any dependencies, and providing a convenient lookup
facility for the beans. The BeanFactoryis also aware of some Spring-specific interfaces, such as
BeanNameAwareand InitializingBean. These interfaces, along with others, help to define the
life cycle of beans managed by the BeanFactory.
The ApplicationContextcan be thought of as a full-service BeanFactory. Applications
typically interact with an ApplicationContextinstead of a BeanFactory. Web applications, for
instance, have their own specialized WebApplicationContext.
ApplicationContextsadd additional features to a BeanFactory. They can automatically
process the BeanFactoryafter initialization by running BeanFactoryPostProcessors. They
provide internationalization (i18n) facilities for resolving messages, an event-routing mecha-
nism for loosely coupled producers and consumers, and support life cycle interfaces such as
ApplicationContextAware.
The ApplicationContextis normally configured via an XML file. This file uses a simple
DTD that sacrifices brevity for readability. A simple Spring applicationContext.xmllooks
like this:
<?xml version="1.0"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
The previous configuration specified two beans for the ApplicationContextto create and
manage. The first is the CashRegisterinstance, the second being an instance of PriceMatrix.
The
ApplicationContextto find a bean inside its context with the name of priceMatrixBean, and
use it when calling the setPriceMatrixmethod on the cashRegisterinstance.
CHAPTER 2 ■SPRING FUNDAMENTALS 17