Chapter 13. Put a Spring in your Step: Hibernate with Spring
What Is Spring?
Writing a Data Access Object
Creating an Application Context
Putting It All Together
13.1. What Is Spring?
If you've been paying attention to Java programming over the
last few years, you've probably heard of the Spring Framework.
The Spring Framework is an Inversion of Control
(IoC) container with a number of additional integrated
features and modules. To some it is just an IoC
container; to others it is an entire platform for application development.
(We will expand on what is meant in the next section.)
Created by Rod Johnson in 2000, Spring rose to prominence as an
alternative to Enterprise Java Beans
(EJBs). Between 2000 and 2004, Sun Microsystems
released a series of (somewhat terrible) specifications for
EJBs. EJBs were difficult to
understand, develop, and deploy without writing volumes
of repetitive code and/or using proprietary tools to shield yourself from
the underlying technology. Rod's seminal text, Expert
One-on-One J2EE Design and Development, published by WROX in
2002, introduced a large number of developers to the idea of replacing
EJBs with a simple IoC container and
a number of APIs that served to isolate your programs
from the rougher edges in Sun's APIs. Instead of
interacting directly with the JDBC,
JMS, or the JTA
APIs, you could use templates and helpers within
Spring. While Sun's core enterprise APIs are still
important to know, a side-effect of using Spring was to lessen the
choke-hold Sun had on defining and proposing new libraries and
applications. Instead of programming to JDBC or
JNDI directly, you could write your program to operate
within Spring's container which was relatively agnostic about what
underlying technology you might choose to implement persistence or
messaging or whatever you were trying to do. Spring wasn't the only
project to try to break through Sun's monopoly on setting standards in the
Java space, but it has unarguably been the most successful.
You might write a few of your persistence layer objects to interact
with JDBC directly, but you also might choose to
implement a few using some custom proprietary code from Oracle, and you
may write the rest using Hibernate. Spring enables this choice by
providing an agnostic IoC container and a collection of
useful abstractions. The discussion
surrounding enterprise development no longer revolves around Sun and the
various standards produced by the Java Community Process
(JCP); instead, Spring has encouraged a proliferation
of choice by providing a common "bus" to which most new projects can
build. Look at Spring's integration with web frameworks for an example:
Wicket, Struts 2 (was WebWork), Stripes, GWT, and
Tapestry all provide first-class Spring integration, and Spring's
documentation details integration with JavaServer Faces, Tapestry, and
WebWork. Spring provides out-of-the-box integration for various Object
Relational Mapping frameworks, including Hibernate,
JDO, Oracle TopLink, iBatis SQL
Maps, and JPA. Spring is the platform that enables a
"freedom of choice" for implementation, and almost every new open source
library or project must, in some way, integrate with the Spring Framework
to become relevant to the large audience of developers who use it. The
consensus among many is that while Sun might tell you that Java is a
"platform," it is really just a language—Spring is the real platform.
After you read this chapter, you'll have an idea of how Spring takes much
of the work out of using Hibernate.
13.1.1. What is inversion of control?
There's no single definition of IoC, but
a central concept is Dependency
Injection. From Wikipedia:
...a pattern in which responsibility for object creation and
object linking is removed from [the objects] and transferred to a
factory.
Spring, the lightweight container, assumes responsibility for
wiring together a set of components, and injects dependencies either via
JavaBean properties or constructor arguments. Using Spring you develop a
series of simple pieces, and then you tell Spring how to wire them
together to create a larger system. This comes in very handy if your
architecture relies on reusable "components." In this chapter, our Data
Access Objects (DAOs) are implemented as components
(or beans) in a Spring Application Context and our example classes
depend on these components as bean properties. If we wanted to reuse our
DAOs in another system such as a command-line utility or a web
application, those systems would depend on the same components as bean
properties, and Spring would take responsibility for wiring our
components together based on an XML description of our program's
construction.
Spring encourages reusability by taking on the responsibility of
gluing together a set of focused components. Your DAOs don't care what
they are connected to or what components are using them; they are
focused solely on providing an interface to whatever component depends
on them. If you are interested in a longer description of both
Dependency Injection and Inversion of Control, you should read Martin
Fowler's Inversion
of Control Containers and the Dependency Injection
Pattern.
13.1.2. Combining Spring and Hibernate
Now that you know what Spring is all about, let's get back to
Hibernate. This chapter focuses on the DAO pattern, transaction
abstractions, and helpers provided by the Spring Framework.
What can you expect from this chapter? While using Spring with
Hibernate is straightforward, there are a few steps we need to take
before we can really take advantage of Spring's Hibernate integration.
First, because Spring is an IoC container, we need to
modify the examples from the previous chapters and create objects to
"wire together." We'll be exploring the DAO pattern in the next section.
After we've created one, we will modify our current set of examples to
implement a common interface Test to which we
will apply a Transactional annotation. Then we
will create a Spring application context by writing an
XML document which tells Spring what objects to
create and what objects to wire together. Finally, we will write a
command-line program that loads our Spring application context and
starts an example program.
NOTE
Enough with this introduction. Let's get to the code.
13.1.3. Adding the Spring framework as a project dependency
To use Spring in the example project, we'll need to add
another dependency to the build. Open up build.xml and add the dependency highlighted
in bold in Example 13-1.
Example 13-1. Adding Spring as a dependency
Code View: <artifact:dependencies pathId="dependency.class.path"> |
Great—now when we run the Ant build, we're going to notice that
the Maven Ant task will download spring-2.5.jar from one of the Maven
repositories.
No comments:
Post a Comment