SpringinPractice.pdf

(6861 KB) Pobierz
MEAP Edition
Manning Early Access Program
Spring in Practice version 8
Copyright 2011 Manning Publications
For more information on this and other Manning titles go to
www.manning.com
Licensed to Josh Long <starbuxman@gmail.com>
T
ABLE OF
C
ONTENTS
Part 1 Getting Started
1 Introducing Spring: the dependency injection container
2 Persisting data with Spring, JPA & Hibernate
3 Building web applications with Spring Web MVC
Part 2 Application Recipes
4 Basic web forms
5 Enhancing Spring MVC applications with Web Flow
6 Authenticating users
7 Authorizing user requests
8 Communicating with users and customers
9 Creating a rich text comment engine
10 Integration testing
11 Product search and image management
12 Rich content authoring
13 Tracking employee time
14 Rapid prototyping and cloud deployments
15 Enterprise integration
16 Create a Spring-based "site-up" framework
Licensed to Josh Long <starbuxman@gmail.com>
1
1
Introducing Spring: the
dependency injection container
Hello, and welcome to Spring in Practice! This chapter begins what we hope will be an
informative and enjoyable exploration of the Spring Framework. Our perspective is applied,
not theoretical, and we approach Spring not by breaking the technology itself down into its
constituent pieces, but rather by looking at important problems from across multiple
business domains and seeing how to pull different pieces of the framework in to develop
solutions. We call this a "problem-centric" approach, and we expect that it will help you get
started more quickly if you're relatively new to the framework, because you won't have to
work quite as hard to integrate a bunch of moving parts. And if you're more experienced with
the framework, we hope that you'll benefit not only from seeing examples of the latest and
greatest that Spring has to offer (at least at the time of this writing), but also from seeing
how we treat the domains themselves. We try hard to describe realistic problems and offer
realistic solutions.
In a problem-centric approach to a technology framework, one issue that we have to
confront is the fact that certain pieces of the technology reappear across many or even most
problems. We know you don't want to read the same thing over and over, so what we've
done is identify those pieces that we think are sufficiently ubiquitous to treat in a technology-
centric way, treat them one time, and then not really deal with them again in any detail.
That's what the first three chapters of the book are intended to address. In chapter 2 we'll
talk about persistence and transaction management, which are of course concerns that will
come up over and over. In chapter 3 we'll look at Spring Web MVC, which for us will be
similarly recurrent since most of our recipes revolve around web applications. We want to
cover the basics for those topics one time and then just move on.
Please post comments or corrections to the Author Online forum:
http://www.manning-sandbox.com/forum.jspa?forumID=503
Licensed to Josh Long <starbuxman@gmail.com>
2
In the current chapter, we'll give a brief rundown of each the components that make up
the framework, but since dependency injection is such an important, fundamental concept,
we'll focus primarily on that. We'll provide a little upfront theory by examining a concept
called
inversion of control
and how it relates to dependency injection. After that, we'll dive
into a small example that shows how to use the container hands-on.
Most of the objects we configure with Spring are architectural components as opposed to
domain objects. Common configuration targets include, for instance, infrastructure (JDBC
data sources, JavaMail sessions, and so forth), data access objects, service beans and MVC
controllers. Spring is flexible and allows us to configure such components and their
interdependencies using XML, Java 5 annotations, or even Java itself.
1
We'll show you how to
do both in this chapter.
1.1 What is Spring and why use it?
The Spring Framework is a container environment and complementary set of APIs that aim
to simplify the development of enterprise Java software. At the framework's core lies a
dependency injection container that allows us to specify components and their dependencies,
often in a highly automated manner. It's easy, for example, to make the container discover
components for us (we can provide base packages for the container to scan), and it's even
easy to make the container discover the dependencies. But if we prefer to be more explicit
about our component and dependency definitions—maybe we see the configuration as an
executable piece of architectural documentation—that's of course an option for us as well.
It's entirely up to us.
The container is feature-rich—sufficiently rich that it's not possible to give it a full
treatment in a single chapter. But we're not aiming to be comprehensive. Instead, our aim is
to equip you with the knowledge and techniques you'll need to understand the rest of this
book. We're going to highlight and explain some best practices around using the dependency
injection container, and we're going to show how to tackle real problems that we think are
representative of the type of work that developers are asked to do every day.
1.1.1 The major pieces of the framework
Figure 1.1 shows a high-level block diagram of the parts we're going to discuss.
2
1
2
See
http://www.springsource.org/javaconfig
for information on the Java Configuration project.
This graphic was inspired by one in the official Spring reference documentation here:
http://static.springframework.org/spring/docs/2.5.x/reference/introduction.html
Please post comments or corrections to the Author Online forum:
http://www.manning-sandbox.com/forum.jspa?forumID=503
Licensed to Josh Long <starbuxman@gmail.com>
Zgłoś jeśli naruszono regulamin