Spring in Action, Second Edition
Craig Walls with Ryan Breidenbach
  • August 2007
  • ISBN 9781933988139
  • 768 pages
This title is out of print and no longer for sale.

"5/5 stars...great instructive book."

Pedot Nicola, Java User Group Trento

Fourth edition of this book is available

In this second edition, Spring in Action has been completely updated to cover the exciting new features of Spring 2.0. The book begins by introducing you to the core concepts of Spring and then quickly launches into a hands-on exploration of the framework. Combining short code snippets and an ongoing example developed throughout the book, it shows you how to build simple and efficient J2EE applications. You will see how to solve persistence problems, handle asynchronous messaging, create and consume remote services, build web applications, and integrate with most popular web frameworks. You will learn how to use Spring to write simpler, easier to maintain code so that you can focus on what really matters — your critical business needs.

Table of Contents show full


preface to the first edition


about this book

about the title

about the cover illustration

Part 1 Core Spring

1. Springing into action

1.1. What is Spring?

1.1.1. Spring modules

1.2. A Spring jump start

1.3. Understanding dependency injection

1.3.1. Injecting dependencies

1.3.2. Dependency injection in action

1.3.3. Dependency injection in enterprise applications

1.4. Applying aspect-oriented programming

1.4.1. Introducing AOP

1.4.2. AOP in action

1.5. Summary

2. Basic bean wiring

2.1. Containing your beans

2.1.1. Introducing the BeanFactory

2.1.2. Working with an application context

2.1.3. A bean’s life

2.2. Creating beans

2.2.1. Declaring a simple bean

2.2.2. Injecting through constructors

2.3. Injecting into bean properties

2.3.1. Injecting simple values

2.3.2. Referencing other beans

2.3.3. Wiring collections

2.3.4. Wiring nothing (null)

2.4. Autowiring

2.4.1. The four types of autowiring

2.4.2. Mixing auto with explicit wiring

2.4.3. To autowire or not to autowire

2.5. Controlling bean creation

2.5.1. Bean scoping

2.5.2. Creating beans from factory methods

2.5.3. Initializing and destroying beans

2.6. Summary

3. Advanced bean wiring

3.1. Declaring parent and child beans

3.1.1. Abstracting a base bean type

3.1.2. Abstracting common properties

3.2. Applying method injection

3.2.1. Basic method replacement

3.2.2. Using getter injection

3.3. Injecting non-Spring beans

3.4. Registering custom property editors

3.5. Working with Spring’s special beans

3.5.1. Postprocessing beans

3.5.2. Postprocessing the bean factory

3.5.3. Externalizing configuration properties

3.5.4. Resolving text messages

3.5.5. Decoupling with application events

3.5.6. Making beans aware

3.6. Scripting beans

3.6.1. Putting the lime in the coconut

3.6.2. Scripting a bean

3.6.3. Injecting properties of scripted beans

3.6.4. Refreshing scripted beans

3.6.5. Writing scripted beans inline

3.7. Summary

4. Advising beans

4.1. Introducing AOP

4.1.1. Defining AOP terminology

4.1.2. Spring’s AOP support

4.2. Creating classic Spring aspects

4.2.1. Creating advice

4.2.2. Defining pointcuts and advisors

4.2.3. Using ProxyFactoryBean

4.3. Autoproxying

4.3.1. Creating autoproxies for Spring aspects

4.3.2. Autoproxying @AspectJ aspects

4.4. Declaring pure-POJO aspects

4.5. Injecting AspectJ aspects

4.6. Summary

Part 2 Enterprise Spring

5. Hitting the database

5.1. Learning Spring’s data access philosophy

5.1.1. Getting to know Spring’s data access exception hierarchy

5.1.2. Templating data access

5.1.3. Using DAO support classes

5.2. Configuring a data source

5.2.1. Using JNDI data sources

5.2.2. Using a pooled data source

5.2.3. JDBC driver-based data source

5.3. Using JDBC with Spring

5.3.1. Tackling runaway JDBC code

5.3.2. Working with JDBC templates

5.3.3. Using Spring’s DAO support classes for JDBC

5.4. Integrating Hibernate with Spring

5.4.1. Choosing a version of Hibernate

5.4.2. Using Hibernate templates

5.4.3. Building Hibernate-backed DAOs

5.4.4. Using Hibernate 3 contextual sessions

5.5. Spring and the Java Persistence API

5.5.1. Using JPA templates

5.5.2. Configuring an entity manager factory

5.5.3. Building a JPA-backed DAO

5.6. Spring and iBATIS

5.6.1. Configuring an iBATIS client template

5.6.2. Building an iBATIS-backed DAO

5.7. Caching

5.7.1. Configuring a caching solution

5.7.2. Proxying beans for caching

5.7.3. Annotation-driven caching

5.8. Summary

6. Managing transactions

6.1. Understanding transactions

6.1.1. Explaining transactions in only four words

6.1.2. Understanding Spring’s transaction management support

6.2. Choosing a transaction manager

6.2.1. JDBC transactions

6.2.2. Hibernate transactions

6.2.3. Java Persistence API transactions

6.2.4. Java Data Objects transactions

6.2.5. Java Transaction API transactions

6.3. Programming transactions in Spring

6.4. Declaring transactions

6.4.1. Defining transaction attributes

6.4.2. Proxying transactions

6.4.3. Declaring transactions in Spring 2.0

6.4.4. Defining annotation-driven transactions

6.5. Summary

7. Securing Spring

7.1. Introducing Spring Security

7.2. Authenticating users

7.2.1. Configuring a provider manager

7.2.2. Authenticating against a database

7.2.3. Authenticating against an LDAP repository

7.3. Controlling access

7.3.1. Voting access decisions

7.3.2. Casting an access decision vote

7.3.3. Handling voter abstinence

7.4. Securing web applications

7.4.1. Proxying Spring Security’s filters

7.4.2. Handling the security context

7.4.3. Prompting the user to log in

7.4.4. Handling security exceptions

7.4.5. Enforcing web security

7.4.6. Ensuring a secure channel

7.5. View-layer security

7.5.1. Conditionally rendering content

7.5.2. Displaying user authentication information

7.6. Securing method invocations

7.6.1. Creating a security aspect

7.6.2. Securing methods using metadata

7.7. Summary

8. Spring and POJO-based remote services

8.1. An overview of Spring remoting

8.2. Working with RMI

8.2.1. Wiring RMI services

8.2.2. Exporting RMI services

8.3. Remoting with Hessian and Burlap

8.3.1. Accessing Hessian/Burlap services

8.3.2. Exposing bean functionality with Hessian/Burlap

8.4. Using Spring’s HttpInvoker

8.4.1. Accessing services via HTTP

8.4.2. Exposing beans as HTTP Services

8.5. Spring and web services

8.5.1. Exporting beans as web services using XFire

8.5.2. Declaring web services with JSR-181 annotations

8.5.3. Consuming web services

8.5.4. Proxying web services with an XFire client

8.6. Summary

9. Building contract-first web services in Spring

9.1. Introducing Spring-WS

9.2. Defining the contract (first!)

9.2.1. Creating sample XML messages

9.3. Handling messages with service endpoints

9.3.1. Building a JDOM-based message endpoint

9.3.2. Marshaling message payloads

9.4. Wiring it all together

9.4.1. Spring-WS: The big picture

9.4.2. Mapping messages to endpoints

9.4.3. Wiring the service endpoint

9.4.4. Configuring a message marshaler

9.4.5. Handling endpoint exceptions

9.4.6. Serving WSDL files

9.4.7. Deploying the service

9.5. Consuming Spring-WS web services

9.5.1. Working with web service templates

9.5.2. Using web service gateway support

9.6. Summary

10. Spring messaging

10.1. A brief introduction to JMS

10.1.1. Architecting JMS

10.1.2. Assessing the benefits of JMS

10.1.3. Setting up ActiveMQ in Spring

10.2. Using JMS with Spring

10.2.1. Tackling runaway JMS code

10.2.2. Working with JMS templates

10.2.3. Converting messages

10.2.4. Using Spring’s gateway support classes for JMS

10.3. Creating message-driven POJOs

10.3.1. Creating a message listener

10.3.2. Writing pure-POJO MDPs

10.4. Using message-based RPC

10.4.1. Introducing Lingo

10.4.2. Exporting the service

10.4.3. Proxying JMS

10.5. Summary

11. Spring and Enterprise JavaBeans

11.1. Wiring EJBs in Spring

11.1.1. Proxying session beans (EJB 2.x)

11.1.2. Wiring EJBs into Spring beans

11.2. Developing Spring-enabled EJBs (EJB 2.x)

11.3. Spring and EJB3

11.3.1. Introducing Pitchfork

11.3.2. Getting started with Pitchfork

11.3.3. Injecting resources by annotation

11.3.4. Declaring interceptors using annotations

11.4. Summary

12. Accessing enterprise services

12.1. Wiring objects from JNDI

12.1.1. Working with conventional JNDI

12.1.2. Injecting JNDI objects

12.1.3. Wiring JNDI objects in Spring 2

12.2. Sending email

12.2.1. Configuring a mail sender

12.2.2. Constructing the email

12.3. Scheduling tasks

12.3.1. Scheduling with Java’s Timer

12.3.2. Using the Quartz scheduler

12.3.3. Invoking methods on a schedule

12.4. Managing Spring beans with JMX

12.4.1. Exporting Spring beans as MBeans

12.4.2. Remoting MBeans

12.4.3. Handling notifications

12.5. Summary

Part 3 Client-side Spring

13. Handling web requests

13.1. Getting started with Spring MVC

13.1.1. A day in the life of a request

13.1.2. Configuring DispatcherServlet

13.1.3. Spring MVC in a nutshell

13.2. Mapping requests to controllers

13.2.1. Using SimpleUrlHandlerMapping

13.2.2. Using ControllerClassNameHandlerMapping

13.2.3. Using metadata to map controllers

13.2.4. Working with multiple handler mappings

13.3. Handling requests with controllers

13.3.1. Processing commands

13.3.2. Processing form submissions

13.3.3. Processing complex forms with wizards

13.3.4. Working with throwaway controllers

13.4. Handling exceptions

13.5. Summary

14. Rendering web views

14.1. Resolving views

14.1.1. Using template views

14.1.2. Resolving view beans

14.1.3. Choosing a view resolver

14.2. Using JSP templates

14.2.1. Binding form data

14.2.2. Rendering externalized messages

14.2.3. Displaying errors

14.3. Laying out pages with Tiles

14.3.1. Tile views

14.3.2. Creating Tile controllers

14.4. Working with JSP alternatives

14.4.1. Using Velocity templates

14.4.2. Working with FreeMarker

14.5. Generating non-HTML output

14.5.1. Producing Excel spreadsheets

14.5.2. Generating PDF documents

14.5.3. Developing custom views

14.6. Summary

15. Using Spring Web Flow

15.1. Getting started with Spring Web Flow

15.1.1. Installing Spring Web Flow

15.1.2. Spring Web Flow essentials

15.1.3. Creating a flow

15.2. Laying the flow groundwork

15.2.1. Flow variables

15.2.2. Start and end states

15.2.3. Gathering customer information

15.2.4. Building a pizza order

15.2.5. Completing the order

15.2.6. A few finishing touches

15.3. Advanced web flow techniques

15.3.1. Using decision states

15.3.2. Extracting subflows and using substates

15.4. Integrating Spring Web Flow with other frameworks

15.4.1. Jakarta Struts

15.4.2. JavaServer Faces

15.5. Summary

16. Integrating with other web frameworks

16.1. Using Spring with Struts

16.1.1. Registering the Spring plug-in with Struts

16.1.2. Writing Spring-aware Struts actions

16.1.3. Delegating to Spring-configured actions

16.1.4. What about Struts 2?

16.2. Working Spring into WebWork 2/Struts 2

16.3. Integrating Spring with Tapestry

16.3.1. Integrating Spring with Tapestry 3

16.3.2. Integrating Spring with Tapestry 4

16.4. Putting a face on Spring with JSF

16.4.1. Resolving JSF-managed properties

16.4.2. Resolving Spring beans

16.4.3. Using Spring beans in JSF pages

16.4.4. Exposing the application context in JSF

16.5. Ajax-enabling applications in Spring with DWR

16.5.1. Direct web remoting

16.5.2. Accessing Spring-managed beans DWR

16.6. Summary


Appendix A: Setting up Spring

A.1. Downloading Spring

A.2. Adding Spring as a Maven 2 dependency

A.3. Spring and Ant

A.4. Spring and Log4j

Appendix B: Testing with (and without) Spring

B.1. Introduction to testing

B.2. Unit-testing Spring MVC controllers

B.3. Integration testing with Spring

B.4. Summary


web content

Web chapter: Building portlet applications

P.1 Thinking inside the box

P.1.1 Why portlets need MVC

P.1.2 Introducing Spring Portlet MVC

P.2 Getting started with Spring Portlet MVC

P.2.1 Configuring DispatcherPortlet

P.2.2 Creating your first portlet controller

P.3 Mapping requests to controllers

P.3.1 Mapping portlet parameters to controllers

P.3.2 Mapping portlet modes to controllers

P.3.3 Mapping both modes and parameters to controllers

P.4 Handling portlet requests with controllers

P.4.1 Displaying mode-specific pages

P.4.2 Processing portlet commands

P.4.3 Processing form submissions

P.5 Handling portlet exceptions

P.6 Summary

Appendix C: Spring XML configuration reference

C.1. Core bean-wiring elements

C.2. AOP elements

C.3. Java Enterprise Edition elements

C.4. Script language elements

C.5. Transaction declaration elements

C.6. Utility elements

C.7. Spring Web Flow configuration elements

C.8. DWR configuration elements

Appendix D: Spring JSP tag library reference

D.1. Legacy Spring tag library

D.2. Form binding tags

D.3. Acegi’s authorization tag library

Appendix E: Spring Web Flow definition reference

Appendix F: Customizing Spring configuration

F.1. Defining a namespace

F.2. Creating namespace handlers

F.2.1. Writing a simple bean definition parser

F.3. Packaging custom configuration elements

F.3.1. Packaging the custom namespace

F.3.2. Using the custom namespace

About the Technology

Spring is a fresh breeze blowing over the Java landscape. Based on the principles of dependency injection, interface-oriented design, and aspect-oriented programming, Spring combines enterprise application power with the simplicity of plain-old Java objects (POJOs).

About the authors

Craig Walls is a software developer with over 12 years' experience and coauthor of XDoclet in Action. He is a zealous promoter of the Spring Framework, speaking frequently at local user groups and conferences and writing about Spring on his blog. When he's not slinging code, Craig spends as much time as he can with his wife, two daughters, 7 birds, 4 dogs, 2 cats, and an ever-fluctuating number of tropical fish. Craig lives in Denton, Texas.

An avid supporter of open source Java technologies, Ryan Breidenbach has developed Java web applications for the past seven years. He lives in Coppell, Texas.