Spring in Action, Second Edition

Book description

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.



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 Book


What's Inside


About the Reader


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.



Quotes
"5/5 stars...great instructive book."
- Pedot Nicola, Java User Group Trento

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.
- Review, Springframework.org

Spring in Action is encyclopedic and eminently readable. Five stars all around!
- Review, JavaLobby.org

Superbly organized and fluently written.
- Review, Internet Bookwatch

Table of contents

  1. Copyright
    1. Dedication
  2. Praise for the First Edition
  3. Preface
  4. Preface to the First Edition
  5. Acknowledgments
  6. About this Book
    1. Roadmap
    2. Additional web content
    3. Who should read this book
    4. Code conventions
    5. About the author
    6. Author Online
  7. About the Title
  8. About the Cover Illustration
  9. 1. Core Spring
    1. 1. Springing into action
      1. 1.1. What is Spring?
        1. 1.1.1. Spring modules
          1. The core container
          2. Application context module
          3. Spring’s AOP module
          4. JDBC abstraction and the DAO module
          5. Object-relational mapping (ORM) integration module
          6. Java Management Extensions (JMX)
          7. Java EE Connector API (JCA)
          8. The Spring MVC framework
          9. Spring Portlet MVC
          10. Spring’s web module
          11. Remoting
          12. Java Message Service (JMS)
      2. 1.2. A Spring jump start
      3. 1.3. Understanding dependency injection
        1. 1.3.1. Injecting dependencies
        2. 1.3.2. Dependency injection in action
          1. Knightly testing
          2. Who’s calling whom?
          3. Decoupling with interfaces
          4. Giving and taking
          5. Assigning a quest to a knight
          6. Seeing it work
        3. 1.3.3. Dependency injection in enterprise applications
      4. 1.4. Applying aspect-oriented programming
        1. 1.4.1. Introducing AOP
        2. 1.4.2. AOP in action
          1. Weaving the aspect
      5. 1.5. Summary
    2. 2. Basic bean wiring
      1. 2.1. Containing your beans
        1. 2.1.1. Introducing the BeanFactory
        2. 2.1.2. Working with an application context
        3. 2.1.3. A bean’s life
      2. 2.2. Creating beans
        1. 2.2.1. Declaring a simple bean
        2. 2.2.2. Injecting through constructors
          1. Injecting object references with constructors
      3. 2.3. Injecting into bean properties
        1. 2.3.1. Injecting simple values
        2. 2.3.2. Referencing other beans
          1. Injecting inner beans
        3. 2.3.3. Wiring collections
          1. Lists and arrays
          2. Sets
          3. Maps
          4. Properties
        4. 2.3.4. Wiring nothing (null)
      4. 2.4. Autowiring
        1. 2.4.1. The four types of autowiring
          1. Autowiring by name
          2. Autowiring by type
          3. Using constructor autowiring
          4. Autodetect autowiring
          5. Autowiring by default
        2. 2.4.2. Mixing auto with explicit wiring
        3. 2.4.3. To autowire or not to autowire
      5. 2.5. Controlling bean creation
        1. 2.5.1. Bean scoping
        2. 2.5.2. Creating beans from factory methods
        3. 2.5.3. Initializing and destroying beans
          1. Defaulting init-method and destroy-method
          2. InitializingBean and DisposableBean
      6. 2.6. Summary
    3. 3. Advanced bean wiring
      1. 3.1. Declaring parent and child beans
        1. 3.1.1. Abstracting a base bean type
          1. Overriding inherited properties
        2. 3.1.2. Abstracting common properties
      2. 3.2. Applying method injection
        1. 3.2.1. Basic method replacement
        2. 3.2.2. Using getter injection
      3. 3.3. Injecting non-Spring beans
      4. 3.4. Registering custom property editors
      5. 3.5. Working with Spring’s special beans
        1. 3.5.1. Postprocessing beans
          1. Writing a bean postprocessor
          2. Registering bean postprocessors
          3. Spring’s own bean postprocessors
        2. 3.5.2. Postprocessing the bean factory
        3. 3.5.3. Externalizing configuration properties
        4. 3.5.4. Resolving text messages
        5. 3.5.5. Decoupling with application events
          1. Publishing events
          2. Listening for events
        6. 3.5.6. Making beans aware
          1. Knowing who you are
          2. Knowing where you live
      6. 3.6. Scripting beans
        1. 3.6.1. Putting the lime in the coconut
        2. 3.6.2. Scripting a bean
          1. Scripting the Lime in Ruby
          2. Scripting a Groovy Lime
          3. Writing the Lime in BeanShell
        3. 3.6.3. Injecting properties of scripted beans
        4. 3.6.4. Refreshing scripted beans
        5. 3.6.5. Writing scripted beans inline
      7. 3.7. Summary
    4. 4. Advising beans
      1. 4.1. Introducing AOP
        1. 4.1.1. Defining AOP terminology
          1. Advice
          2. Joinpoint
          3. Pointcut
          4. Aspect
          5. Introduction
          6. Target
          7. Proxy
          8. Weaving
        2. 4.1.2. Spring’s AOP support
          1. Spring advice is written in Java
          2. Spring advises objects at runtime
          3. Spring only supports method joinpoints
      2. 4.2. Creating classic Spring aspects
        1. 4.2.1. Creating advice
          1. Before advice
          2. After returning advice
          3. After throwing advice
          4. Around advice
        2. 4.2.2. Defining pointcuts and advisors
          1. Declaring a regular expression pointcut
          2. Combining a pointcut with an advisor
          3. Defining AspectJ pointcuts
        3. 4.2.3. Using ProxyFactoryBean
          1. Abstracting ProxyFactoryBean
      3. 4.3. Autoproxying
        1. 4.3.1. Creating autoproxies for Spring aspects
        2. 4.3.2. Autoproxying @AspectJ aspects
          1. Annotating around advice
      4. 4.4. Declaring pure-POJO aspects
      5. 4.5. Injecting AspectJ aspects
      6. 4.6. Summary
  10. 2. Enterprise Spring
    1. 5. Hitting the database
      1. 5.1. Learning Spring’s data access philosophy
        1. 5.1.1. Getting to know Spring’s data access exception hierarchy
          1. Spring’s persistence platform agnostic exceptions
          2. Look, Ma! No catch blocks!
        2. 5.1.2. Templating data access
        3. 5.1.3. Using DAO support classes
      2. 5.2. Configuring a data source
        1. 5.2.1. Using JNDI data sources
          1. JNDI data sources in Spring 2.0
        2. 5.2.2. Using a pooled data source
        3. 5.2.3. JDBC driver-based data source
      3. 5.3. Using JDBC with Spring
        1. 5.3.1. Tackling runaway JDBC code
        2. 5.3.2. Working with JDBC templates
          1. Accessing data using JdbcTemplate
          2. Using named parameters
          3. Simplifying JDBC In Java 5
        3. 5.3.3. Using Spring’s DAO support classes for JDBC
          1. DAO support for named parameters
          2. Simplified Java 5 DAOs
      4. 5.4. Integrating Hibernate with Spring
        1. 5.4.1. Choosing a version of Hibernate
        2. 5.4.2. Using Hibernate templates
          1. Using classic Hibernate mapping files
          2. Working with annotated domain objects
          3. Accessing data through the Hibernate template
        3. 5.4.3. Building Hibernate-backed DAOs
        4. 5.4.4. Using Hibernate 3 contextual sessions
      5. 5.5. Spring and the Java Persistence API
        1. 5.5.1. Using JPA templates
          1. Accessing data through the JPA template
        2. 5.5.2. Configuring an entity manager factory
          1. Configuring application-managed JPA
          2. Configuring container-managed JPA
        3. 5.5.3. Building a JPA-backed DAO
      6. 5.6. Spring and iBATIS
        1. 5.6.1. Configuring an iBATIS client template
          1. Configuring an SqlMapClientTemplate
          2. Defining iBATIS SQL maps
          3. Using the template in a DAO
        2. 5.6.2. Building an iBATIS-backed DAO
      7. 5.7. Caching
        1. 5.7.1. Configuring a caching solution
          1. Configuring EHCache
        2. 5.7.2. Proxying beans for caching
          1. Flushing the cache
          2. Declaring a proxied inner bean
        3. 5.7.3. Annotation-driven caching
      8. 5.8. Summary
    2. 6. Managing transactions
      1. 6.1. Understanding transactions
        1. 6.1.1. Explaining transactions in only four words
        2. 6.1.2. Understanding Spring’s transaction management support
      2. 6.2. Choosing a transaction manager
        1. 6.2.1. JDBC transactions
        2. 6.2.2. Hibernate transactions
        3. 6.2.3. Java Persistence API transactions
        4. 6.2.4. Java Data Objects transactions
        5. 6.2.5. Java Transaction API transactions
      3. 6.3. Programming transactions in Spring
      4. 6.4. Declaring transactions
        1. 6.4.1. Defining transaction attributes
          1. Propagation behavior
          2. Isolation levels
          3. Read-only
          4. Transaction timeout
          5. Rollback rules
        2. 6.4.2. Proxying transactions
          1. Creating a transaction proxy template
        3. 6.4.3. Declaring transactions in Spring 2.0
        4. 6.4.4. Defining annotation-driven transactions
      5. 6.5. Summary
    3. 7. Securing Spring
      1. 7.1. Introducing Spring Security
        1. What’s in a name?
          1. Security interceptors
          2. Authentication managers
          3. Access decisions managers
          4. Run-as managers
          5. After-invocation managers
      2. 7.2. Authenticating users
        1. 7.2.1. Configuring a provider manager
        2. 7.2.2. Authenticating against a database
          1. Using an in-memory DAO
          2. Declaring a JDBC DAO
          3. Working with encrypted passwords
          4. Caching user information
        3. 7.2.3. Authenticating against an LDAP repository
          1. Authenticating with LDAP binding
          2. Authenticating by comparing passwords
          3. Declaring the populator strategy bean
      3. 7.3. Controlling access
        1. 7.3.1. Voting access decisions
        2. 7.3.2. Casting an access decision vote
        3. 7.3.3. Handling voter abstinence
      4. 7.4. Securing web applications
        1. 7.4.1. Proxying Spring Security’s filters
          1. Proxying servlet filters
          2. Proxying multiple filters
          3. Configuring proxies for Spring Security
        2. 7.4.2. Handling the security context
        3. 7.4.3. Prompting the user to log in
          1. Basic authentication
          2. Form-based authentication
        4. 7.4.4. Handling security exceptions
          1. Handling authorization exceptions
        5. 7.4.5. Enforcing web security
        6. 7.4.6. Ensuring a secure channel
          1. Managing channel decisions
      5. 7.5. View-layer security
        1. 7.5.1. Conditionally rendering content
        2. 7.5.2. Displaying user authentication information
      6. 7.6. Securing method invocations
        1. 7.6.1. Creating a security aspect
        2. 7.6.2. Securing methods using metadata
      7. 7.7. Summary
    4. 8. Spring and POJO-based remote services
      1. 8.1. An overview of Spring remoting
      2. 8.2. Working with RMI
        1. 8.2.1. Wiring RMI services
        2. 8.2.2. Exporting RMI services
          1. Configuring an RMI service in Spring
      3. 8.3. Remoting with Hessian and Burlap
        1. 8.3.1. Accessing Hessian/Burlap services
        2. 8.3.2. Exposing bean functionality with Hessian/Burlap
          1. Exporting a Hessian service
          2. Configuring the Hessian controller
          3. Exporting a Burlap service
      4. 8.4. Using Spring’s HttpInvoker
        1. 8.4.1. Accessing services via HTTP
        2. 8.4.2. Exposing beans as HTTP Services
      5. 8.5. Spring and web services
        1. 8.5.1. Exporting beans as web services using XFire
          1. Configuring XFireExporter
          2. Configuring DispatcherServlet
          3. Mapping requests to XFireExporter
        2. 8.5.2. Declaring web services with JSR-181 annotations
          1. Mapping requests to JSR-181 annotated beans
        3. 8.5.3. Consuming web services
          1. Wiring JaxRpcPortProxyFactoryBean
          2. Mapping complex types
          3. Mapping arrays
        4. 8.5.4. Proxying web services with an XFire client
      6. 8.6. Summary
    5. 9. Building contract-first web services in Spring
      1. 9.1. Introducing Spring-WS
      2. 9.2. Defining the contract (first!)
        1. 9.2.1. Creating sample XML messages
          1. Forging the data contract
      3. 9.3. Handling messages with service endpoints
        1. 9.3.1. Building a JDOM-based message endpoint
        2. 9.3.2. Marshaling message payloads
      4. 9.4. Wiring it all together
        1. 9.4.1. Spring-WS: The big picture
        2. 9.4.2. Mapping messages to endpoints
        3. 9.4.3. Wiring the service endpoint
        4. 9.4.4. Configuring a message marshaler
        5. 9.4.5. Handling endpoint exceptions
        6. 9.4.6. Serving WSDL files
          1. Using predefined WSDL
        7. 9.4.7. Deploying the service
      5. 9.5. Consuming Spring-WS web services
        1. 9.5.1. Working with web service templates
          1. Sending a message
          2. Using marshalers on the client side
        2. 9.5.2. Using web service gateway support
      6. 9.6. Summary
    6. 10. Spring messaging
      1. 10.1. A brief introduction to JMS
        1. 10.1.1. Architecting JMS
          1. Point-to-point messaging model
          2. Publish-subscribe messaging model
        2. 10.1.2. Assessing the benefits of JMS
          1. No waiting
          2. Message-oriented
          3. Location independence
          4. Guaranteed delivery
        3. 10.1.3. Setting up ActiveMQ in Spring
          1. Creating a connection factory
          2. Declaring an ActiveMQ message destination
      2. 10.2. Using JMS with Spring
        1. 10.2.1. Tackling runaway JMS code
        2. 10.2.2. Working with JMS templates
          1. Wiring JmsTemplate
          2. Sending messages
          3. Setting a default destination
          4. Consuming messages
        3. 10.2.3. Converting messages
          1. Sending and receiving converted messages
          2. Wiring a message converter
        4. 10.2.4. Using Spring’s gateway support classes for JMS
      3. 10.3. Creating message-driven POJOs
        1. 10.3.1. Creating a message listener
          1. Containing message listeners
          2. Working with transactional MDPs
        2. 10.3.2. Writing pure-POJO MDPs
          1. Converting MDP messages
      4. 10.4. Using message-based RPC
        1. 10.4.1. Introducing Lingo
        2. 10.4.2. Exporting the service
        3. 10.4.3. Proxying JMS
          1. Wiring JmsProxyFactoryBean
          2. Making the call
      5. 10.5. Summary
    7. 11. Spring and Enterprise JavaBeans
      1. 11.1. Wiring EJBs in Spring
        1. 11.1.1. Proxying session beans (EJB 2.x)
          1. Declaring EJB proxies with Spring’s JEE namespace
          2. Declaring EJB 3 session beans in Spring
        2. 11.1.2. Wiring EJBs into Spring beans
      2. 11.2. Developing Spring-enabled EJBs (EJB 2.x)
      3. 11.3. Spring and EJB3
        1. 11.3.1. Introducing Pitchfork
        2. 11.3.2. Getting started with Pitchfork
        3. 11.3.3. Injecting resources by annotation
        4. 11.3.4. Declaring interceptors using annotations
      4. 11.4. Summary
    8. 12. Accessing enterprise services
      1. 12.1. Wiring objects from JNDI
        1. 12.1.1. Working with conventional JNDI
        2. 12.1.2. Injecting JNDI objects
          1. Caching JNDI objects
          2. Lazily loading JNDI objects
          3. Fallback objects
        3. 12.1.3. Wiring JNDI objects in Spring 2
      2. 12.2. Sending email
        1. 12.2.1. Configuring a mail sender
          1. Using a JNDI mail session
          2. Wiring the mail sender into a service bean
        2. 12.2.2. Constructing the email
      3. 12.3. Scheduling tasks
        1. 12.3.1. Scheduling with Java’s Timer
          1. Creating a timer task
          2. Scheduling the timer task
          3. Starting the timer
          4. Delaying the start of the timer
        2. 12.3.2. Using the Quartz scheduler
          1. Creating a Quartz job
          2. Scheduling the job
          3. Scheduling a cron job
          4. Starting the job
        3. 12.3.3. Invoking methods on a schedule
      4. 12.4. Managing Spring beans with JMX
        1. 12.4.1. Exporting Spring beans as MBeans
          1. Exposing methods by name
          2. Using interfaces to define MBean operations and attributes
          3. Working with metadata-driven MBeans
          4. Handling MBean object name collisions
        2. 12.4.2. Remoting MBeans
          1. Exposing remote MBeans
          2. Accessing remote MBeans
          3. Proxying MBeans
        3. 12.4.3. Handling notifications
          1. Listening for notifications
      5. 12.5. Summary
  11. 3. Client-side Spring
    1. 13. Handling web requests
      1. 13.1. Getting started with Spring MVC
        1. 13.1.1. A day in the life of a request
        2. 13.1.2. Configuring DispatcherServlet
          1. Breaking up the application context
          2. Configuring a context loader
        3. 13.1.3. Spring MVC in a nutshell
          1. Building the controller
          2. Introducing ModelAndView
          3. Configuring the controller bean
          4. Declaring a view resolver
          5. Creating the JSP
          6. Putting it all together
      2. 13.2. Mapping requests to controllers
        1. 13.2.1. Using SimpleUrlHandlerMapping
        2. 13.2.2. Using ControllerClassNameHandlerMapping
        3. 13.2.3. Using metadata to map controllers
        4. 13.2.4. Working with multiple handler mappings
      3. 13.3. Handling requests with controllers
        1. 13.3.1. Processing commands
        2. 13.3.2. Processing form submissions
          1. Validating form input
          2. Validating with Commons Validator
        3. 13.3.3. Processing complex forms with wizards
          1. Building a basic wizard controller
          2. Stepping through form pages
          3. Finishing the wizard
          4. Canceling the wizard
          5. Validating a wizard form a page at a time
        4. 13.3.4. Working with throwaway controllers
      4. 13.4. Handling exceptions
      5. 13.5. Summary
    2. 14. Rendering web views
      1. 14.1. Resolving views
        1. 14.1.1. Using template views
        2. 14.1.2. Resolving view beans
          1. Declaring view beans in a separate XML file
          2. Resolving views from resource bundles
        3. 14.1.3. Choosing a view resolver
          1. Using multiple view resolvers
      2. 14.2. Using JSP templates
        1. 14.2.1. Binding form data
        2. 14.2.2. Rendering externalized messages
        3. 14.2.3. Displaying errors
      3. 14.3. Laying out pages with Tiles
        1. 14.3.1. Tile views
          1. Configuring Tiles
          2. Resolving Tiles views
        2. 14.3.2. Creating Tile controllers
      4. 14.4. Working with JSP alternatives
        1. 14.4.1. Using Velocity templates
          1. Defining the Velocity view
          2. Configuring the Velocity engine
          3. Resolving Velocity views
          4. Formatting dates and numbers
          5. Exposing request and session attributes
          6. Binding form fields in Velocity
        2. 14.4.2. Working with FreeMarker
          1. Constructing a FreeMarker view
          2. Configuring the FreeMarker engine
          3. Resolving FreeMarker views
          4. Exposing request and session attributes
          5. Binding form fields in FreeMarker
      5. 14.5. Generating non-HTML output
        1. 14.5.1. Producing Excel spreadsheets
        2. 14.5.2. Generating PDF documents
        3. 14.5.3. Developing custom views
      6. 14.6. Summary
    3. 15. Using Spring Web Flow
      1. 15.1. Getting started with Spring Web Flow
        1. 15.1.1. Installing Spring Web Flow
          1. Setting up the FlowController
          2. Configuring a flow executor
          3. Registering flow definitions
        2. 15.1.2. Spring Web Flow essentials
        3. 15.1.3. Creating a flow
      2. 15.2. Laying the flow groundwork
        1. 15.2.1. Flow variables
        2. 15.2.2. Start and end states
        3. 15.2.3. Gathering customer information
          1. Asking the customer for their phone number
          2. Looking up customer data
          3. Adding a new customer
        4. 15.2.4. Building a pizza order
          1. Displaying the order
          2. Adding a pizza to the order
        5. 15.2.5. Completing the order
          1. Taking payment
          2. Submitting the order
        6. 15.2.6. A few finishing touches
      3. 15.3. Advanced web flow techniques
        1. 15.3.1. Using decision states
        2. 15.3.2. Extracting subflows and using substates
          1. Creating a customer data flow
          2. Using a subflow
      4. 15.4. Integrating Spring Web Flow with other frameworks
        1. 15.4.1. Jakarta Struts
        2. 15.4.2. JavaServer Faces
      5. 15.5. Summary
    4. 16. Integrating with other web frameworks
      1. 16.1. Using Spring with Struts
        1. 16.1.1. Registering the Spring plug-in with Struts
        2. 16.1.2. Writing Spring-aware Struts actions
        3. 16.1.3. Delegating to Spring-configured actions
          1. Wiring actions in Spring
          2. Implementing the Spring-configured Struts action
        4. 16.1.4. What about Struts 2?
      2. 16.2. Working Spring into WebWork 2/Struts 2
      3. 16.3. Integrating Spring with Tapestry
        1. 16.3.1. Integrating Spring with Tapestry 3
          1. Loading Spring beans into Tapestry pages
        2. 16.3.2. Integrating Spring with Tapestry 4
      4. 16.4. Putting a face on Spring with JSF
        1. 16.4.1. Resolving JSF-managed properties
        2. 16.4.2. Resolving Spring beans
        3. 16.4.3. Using Spring beans in JSF pages
        4. 16.4.4. Exposing the application context in JSF
      5. 16.5. Ajax-enabling applications in Spring with DWR
        1. 16.5.1. Direct web remoting
          1. Basic DWR configuration
          2. Defining the remote objects
          3. Exporting the remote objects to JavaScript
          4. Calling remote methods from JavaScript
          5. Displaying the results
        2. 16.5.2. Accessing Spring-managed beans DWR
          1. Using Spring creators
          2. Using DWR’s Spring configuration namespace
      6. 16.6. Summary
  12. A. Setting up Spring
    1. A.1. Downloading Spring
      1. A.1.1. Exploring the Spring distribution
      2. A.1.2. Building your classpath
    2. A.2. Adding Spring as a Maven 2 dependency
    3. A.3. Spring and Ant
    4. A.4. Spring and Log4j
  13. B. Testing with (and without) Spring
    1. B.1. Introduction to testing
      1. B.1.1. Understanding different types of testing
      2. B.1.2. Using JUnit
        1. Setting up tests
        2. Tearing down tests
      3. B.1.3. Spring’s role in testing
    2. B.2. Unit-testing Spring MVC controllers
      1. B.2.1. Mocking objects
        1. Mocking interfaces with EasyMock
      2. B.2.2. Asserting the contents of ModelAndView
    3. B.3. Integration testing with Spring
      1. B.3.1. Testing wired objects
        1. Forcing a context reload
        2. Setting up and tearing down dependency injected tests
      2. B.3.2. Integration-testing transactional objects
        1. Committing the test’s changes
        2. Setting up and tearing down transactional tests
      3. B.3.3. Testing against the database
        1. Cleaning up
      4. B.3.4. Testing in JUnit 4 with Gienah Testing
    4. B.4. Summary

Product information

  • Title: Spring in Action, Second Edition
  • Author(s): Craig Walls, Ryan Breidenbach
  • Release date: August 2007
  • Publisher(s): Manning Publications
  • ISBN: 9781933988139