Figure 4-3. Spring's Dependency Injection mechanism
Although Dependency Injection is the preferred mechanism for wiring together collaborators and
dependent objects, you need Dependency Lookup to access the dependent objects. In many
environments, Spring cannot automatically wire up all of your application components using
Dependency Injection, and you must use Dependency Lookup to access the initial set of components.
For example, in stand-alone Java applications, you need to bootstrap Spring's container in the main()
method and obtain the dependencies (via the ApplicationContext interface) for processing
programmatically. However, when you are building web applications using Spring's MVC support,
Spring can avoid this by gluing your entire application together automatically. Wherever it is possible to
use Dependency Injection with Spring, you should do so; otherwise, you can fall back on the
Dependency Lookup capabilities. You will see examples of both in action during the course of this
chapter, and we will point them out when they first arise.
An interesting feature of Spring's IoC container is that it has the ability to act as an adaptor between
its own Dependency Injection container and external Dependency Lookup containers. We will discuss
this feature later in this chapter.
Spring supports both Constructor and Setter Injection and bolsters the standard IoC feature set with
a whole host of useful additions to make your life easier.
The rest of this chapter introduces the basics of Spring's DI container complete with plenty of
Dependency Injection with Spring
Spring's support for Dependency Injection is comprehensive and, as you will see in Chapter 5, goes
beyond the standard IoC feature set we have discussed so far. The rest of this chapter addresses the
basics of Spring's Dependency Injection container, looking at Setter, Constructor, and Method Injection,
along with a detailed look at how Dependency Injection is configured in Spring.
Beans and BeanFactories
The core of Spring's Dependency Injection container is the BeanFactory interface. A BeanFactory is
responsible for managing components, including their dependencies as well as their life cycles. In
Spring, the term bean is used to refer to any component managed by the container. Typically your beans
adhere, at some level, to the JavaBeans specification, but this is not required, especially if you plan to use
Constructor Injection to wire your beans together.
If your application needs only DI support, you can interact with the Spring DI container via the
BeanFactory interface. In this case, your application must create an instance of a class that implements
the BeanFactory interface and configures it with bean and dependency information. After this is
complete, your application can access the beans via the BeanFactory and get on with its processing.
In some cases, all of this setup is handled automatically (for example, in a web application, Spring's
ApplicationContext will be bootstrapped by the web container during application startup via a Spring-
provided ContextLoaderListener class declared in the web.xml descriptor file). But in many cases, you
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks