You are previewing Beginning Spring Framework 2.
O'Reilly logo
Beginning Spring Framework 2

Book Description

The Spring Framework is designed from the ground up to make it easier than ever to develop server-side applications with Java Enterprise Edition. With this book as your guide, you'll quickly learn how to use the latest features of Spring 2 and other open-source tools that can be downloaded for free on the web. With each subsequent chapter, you'll explore an area of Spring application design and development as you walk through the steps involved in building a larg production-scale example.

Table of Contents

  1. Copyright
  2. About the Authors
  3. Credits
  4. Acknowledgments
  5. Introduction
    1. How to Use This Book
    2. Conventions
      1. Downloads for the Book
    3. Errata
    4. p2p.wrox.com
  6. 1. Jump Start Spring 2
    1. 1.1. All About Spring
    2. 1.2. Focus on Simplicity
    3. 1.3. Applying Spring
      1. 1.3.1. Creating a Modularized Application
        1. 1.3.1.1. Decoupling at the Interface
        2. 1.3.1.2. Creating the Result Writer Components
        3. 1.3.1.3. Putting the Application Together
        4. 1.3.1.4. Mixing and Matching Components
      2. 1.3.2. Using Spring to Configure a Modularized Application
        1. 1.3.2.1. ApplicationContext is a BeanFactory
        2. 1.3.2.2. Providing the Spring Container with Wiring Instructions
        3. 1.3.2.3. Creating and Wiring Java Beans
        4. 1.3.2.4. Adding a Logging Configuration File
      3. 1.3.3. Wiring Beans Automatically by Type
      4. 1.3.4. Understanding Spring's Inversion of Control (IoC) Container
        1. 1.3.4.1. Dependency Injection
    4. 1.4. Adding Aspect-Oriented Programming to the Mix
      1. 1.4.1. Adding a Logging Aspect
        1. 1.4.1.1. Wiring in AOP Proxies
        2. 1.4.1.2. Matching Join Points via AspectJ Pointcut Expressions
    5. 1.5. Beyond Plumbing — Spring API Libraries
      1. 1.5.1. Using Spring APIs To Facilitate Application Creation
    6. 1.6. Summary
  7. 2. Designing Spring Applications
    1. 2.1. Overview of the PIX Album-Management System
      1. 2.1.1. The PIX System Requirements
        1. 2.1.1.1. PixUser Requirements
        2. 2.1.1.2. Album Viewer Requirements
        3. 2.1.1.3. Affiliate Requirements
        4. 2.1.1.4. External Interfacing Requirements
    2. 2.2. Discovering the Domain Model
      1. 2.2.1. The PixUser POJO
      2. 2.2.2. The Affiliate POJO
      3. 2.2.3. The Picture Object
      4. 2.2.4. The Album POJO
      5. 2.2.5. The Comment POJO
      6. 2.2.6. The POJO Relationships
        1. 2.2.6.1. Adding POJO Operations to Support Relationships
        2. 2.2.6.2. Establishing POJO Identity
          1. 2.2.6.2.1. Why the Java Platform Default Implementation Is Not Adequate
        3. 2.2.6.3. The Appeal of a Nonintrusive Framework
    3. 2.3. The Importance of Unit Testing
      1. 2.3.1. POJO-Based Design and Containerless Unit Testing
      2. 2.3.2. Regression Testing
      3. 2.3.3. Working with a Unit-Testing Framework
        1. 2.3.3.1. Running Test Cases
    4. 2.4. Summary
  8. 3. Spring Persistence Using JPA
    1. 3.1. Java Persistence
      1. 3.1.1. JDBC Architecture
      2. 3.1.2. Traditional JDBC Approach
    2. 3.2. DAO — Unifying Data Access
      1. 3.2.1. Spring DAO Support
        1. 3.2.1.1. Spring Template Classes
          1. 3.2.1.1.1. Using the JDBC Template
          2. 3.2.1.1.2. About DataSource Injection
      2. 3.2.2. Spring Exception Translation
    3. 3.3. Spring and JPA
      1. 3.3.1. Entities
        1. 3.3.1.1. Entity Metadata
        2. 3.3.1.2. Entity Manager
        3. 3.3.1.3. Container-Managed Entity Managers
        4. 3.3.1.4. Application-Managed Entity Managers
        5. 3.3.1.5. Entity States
        6. 3.3.1.6. Entity Relationships
        7. 3.3.1.7. One-to-One Entity Relationship
        8. 3.3.1.8. One-to-Many Entity Relationship
        9. 3.3.1.9. Many-to-One Entity Relationship
        10. 3.3.1.10. Many-to-Many Relationship
        11. 3.3.1.11. Entity Relationship Direction
        12. 3.3.1.12. Entity Inheritance Mapping
        13. 3.3.1.13. Persisting Entities
        14. 3.3.1.14. Removing Entity Instances
        15. 3.3.1.15. Creating Database Queries
        16. 3.3.1.16. Persistence Unit
        17. 3.3.1.17. Persistence Context
      2. 3.3.2. Spring as a JPA Container
      3. 3.3.3. About JPA APIs
      4. 3.3.4. JPA the Easy Way — Using Annotations
        1. 3.3.4.1. JPA Entity Annotation
      5. 3.3.5. SPRING JPA Exception Translation
      6. 3.3.6. SPRING JPA DAOs
        1. 3.3.6.1. JpaDaoSupport and JpaTemplate Classes
      7. 3.3.7. Spring JPA Configuration
        1. 3.3.7.1. Wiring a DataSource to EntityManagerFactory
        2. 3.3.7.2. Configuring JPA Provider
        3. 3.3.7.3. Changing JPA Providers
    4. 3.4. Persistence and the PIX Domain Model
      1. 3.4.1. Persisting The PixUser POJO
      2. 3.4.2. Implementing PixUser Repository DAO
      3. 3.4.3. Implementing the Album Repository DAO
    5. 3.5. Testing the Persistence Layer
      1. 3.5.1. Spring Test Support
      2. 3.5.2. Executing the Persistence Test Suite
      3. 3.5.3. Testing the PIX Repositories
    6. 3.6. Summary
  9. 4. Using Spring MVC to Build Web Pages
    1. 4.1. The MVC Architectural Pattern
      1. 4.1.1. The Sample Application's Architecture
    2. 4.2. Spring MVC Development
      1. 4.2.1. Processing Web Requests with Controllers
        1. 4.2.1.1. Dispatching Requests to Controller Classes
        2. 4.2.1.2. Mapping URLs to Controllers
        3. 4.2.1.3. Creating a Basic Controller to Retrieve Albums
        4. 4.2.1.4. Ensuring Controllers Behave as Expected with Unit Tests
        5. 4.2.1.5. Using the Spring Mock Package for Unit Testing
      2. 4.2.2. Presenting the Model with a View
        1. 4.2.2.1. Resolving View Names
    3. 4.3. Getting Data from the User with Forms
      1. 4.3.1. A Basic Form-Submission Workflow
        1. 4.3.1.1. Understanding Form Submissions in Spring MVC
      2. 4.3.2. Using the Form View
        1. 4.3.2.1. Passing Reference Data to the Form View
        2. 4.3.2.2. Populating the Model
        3. 4.3.2.3. Implementing Form Validation
    4. 4.4. When Things Go Wrong
    5. 4.5. Summary
  10. 5. Advanced Spring MVC
    1. 5.1. Submitting a Form Across Multiple Pages
      1. 5.1.1. Adding Pictures to an Album
      2. 5.1.2. Developing Wizard Form Pages
      3. 5.1.3. Implementing Wizard Form Actions
      4. 5.1.4. Validating Data Submitted Through a Wizard
    2. 5.2. Uploading Files
    3. 5.3. Getting More Stuff Done with the Same Controller
    4. 5.4. Creating a Different View
      1. 5.4.1. Your First View
      2. 5.4.2. Saving an Album to PDF
      3. 5.4.3. Generating an RSS Feed
    5. 5.5. Personalization
      1. 5.5.1. Retrieving Text Labels from a Message Source
      2. 5.5.2. Displaying Application Labels in a Different Language
      3. 5.5.3. Changing the Application's Language Settings
      4. 5.5.4. Allowing the User to Personalize the Application
    6. 5.6. Summary
  11. 6. Spring Web Flow
    1. 6.1. Examining a Sample Work Flow for Loan Applications
    2. 6.2. Introducing Spring Web Flow
      1. 6.2.1. How SWF Works with Spring MVC
        1. 6.2.1.1. Spring Web Flow Configuration
      2. 6.2.2. Launching Flows
    3. 6.3. Implementing SWF in the PixWeb Application
      1. 6.3.1. The Login Flow
        1. 6.3.1.1. The Login Flow Beans XML
          1. 6.3.1.1.1. The Registration Flow File
          2. 6.3.1.1.2. The Registration Flow Beans XML
          3. 6.3.1.1.3. The register.jsp File
          4. 6.3.1.1.4. The labels_en.properties File
          5. 6.3.1.1.5. The RegistrationAction.java File
          6. 6.3.1.1.6. Deploying the Changes
      2. 6.3.2. The Album Creation Flow
    4. 6.4. Implementing Actions
    5. 6.5. Implementing Views
    6. 6.6. Testing Flows
    7. 6.7. Architectural Overview
    8. 6.8. Advanced Topics
      1. 6.8.1. REST-Style URLs
      2. 6.8.2. Flow Execution Repositories
      3. 6.8.3. Flow Execution Repository Implementations
      4. 6.8.4. Subflows and Attribute Mappers
      5. 6.8.5. Flow Execution Listeners
      6. 6.8.6. Exception Handlers
    9. 6.9. Summary
  12. 7. Ajax and Spring: Direct Web Remoting Integration
    1. 7.1. Web 2.0: The World of Ajax
    2. 7.2. Ajax Basics
      1. 7.2.1. Client-Side Ajax Development with JavaScript
      2. 7.2.2. The XMLHttpRequest Object
        1. 7.2.2.1.
          1. 7.2.2.1.1. Working with XMLHttpRequest Methods and Properties
        2. 7.2.2.2. Monitoring XMLHttpRequest readyState
        3. 7.2.2.3. Cross-Browser Differences and the XMLHttpRequest Object
    3. 7.3. Introducing Direct Web Remoting 2
      1. 7.3.1. Downloading DWR 2
      2. 7.3.2. Working with DWR 2
        1. 7.3.2.1.
          1. 7.3.2.1.1. Writing Client-Side JavaScript Code with DWR 2
        2. 7.3.2.2. Working with the util.js Library
        3. 7.3.2.3. The addOptions() Utility Function
        4. 7.3.2.4. Ajax Versus Spring MVC and Webflow
      3. 7.3.3. Integrating Spring and DWR 2
        1. 7.3.3.1. The PIX Ajax Album Viewer
    4. 7.4. Setting up the PIX System for the Ajax Album Viewer
    5. 7.5. Remoting Requires EAGER Fetching of Contained Objects
      1. 7.5.1.
        1. 7.5.1.1. Confirming DWR is Working
    6. 7.6. Summary
  13. 8. Spring and JMS — Message-Driven POJOs
    1. 8.1. JMS Concepts
    2. 8.2. JMS Messaging Domains
      1. 8.2.1. Point-To-Point Messaging
      2. 8.2.2. Publish/Subscribe Messaging
      3. 8.2.3. Persistence versus Durability
    3. 8.3. The JMS Message
      1. 8.3.1. Message Header
      2. 8.3.2. Message Properties
      3. 8.3.3. Message Selectors
      4. 8.3.4. Message Body
    4. 8.4. Producing JMS Messages
    5. 8.5. Consuming JMS Messages
      1. 8.5.1. Synchronous Message Consumption
      2. 8.5.2. Asynchronous Message Consumption
        1. 8.5.2.1. Message-Driven Beans
        2. 8.5.2.2. Message-Driven POJOs
    6. 8.6. The Spring JMS Framework
      1. 8.6.1. The Spring JMS Packages
      2. 8.6.2. The JmsTemplate Class
      3. 8.6.3. Message Listener Containers
      4. 8.6.4. Destinations
      5. 8.6.5. Transactions
    7. 8.7. Configuring Message-Driven POJOS
    8. 8.8. Realizing the JMS Use Case
      1. 8.8.1. Modeling Message-Driven POJOs
      2. 8.8.2. The PIX Web POJOS
      3. 8.8.3. Changing the PIX Web POJOs Into Message-Driven POJOs
    9. 8.9. A JMS Provider — Apache ActiveMQ
    10. 8.10. The JMS Template in the PIX Web Application
    11. 8.11. Summary
  14. 9. Spring Web Services and Remoting
    1. 9.1. Web Service Benefits
    2. 9.2. Introducing Web Services
    3. 9.3. Web Services Architecture
      1. 9.3.1. The Network Layer
      2. 9.3.2. XML
      3. 9.3.3. SOAP
      4. 9.3.4. WSDL
        1. 9.3.4.1. WSDL Document
        2. 9.3.4.2. WSDL Bindings
      5. 9.3.5. UDDI
    4. 9.4. Web Services Interactions
    5. 9.5. Web Services Interoperability
      1. 9.5.1. Java Web Service Technologies
      2. 9.5.2. Java Web Application Web Services
        1. 9.5.2.1. JSR 181–Compliant Java
        2. 9.5.2.2. JSR 181 Annotations
    6. 9.6. Spring Remoting
    7. 9.7. SOAP Frameworks
      1. 9.7.1. Java-to-XML Bindings
      2. 9.7.2. XFire
        1. 9.7.2.1. Aegis Binding
    8. 9.8. Spring Web Services with XFire
    9. 9.9. Realizing the PIX AffiliateManagement Use Case
    10. 9.10. Invoking Web Services
    11. 9.11. SOAP Handlers
      1. 9.11.1. Testing SOAP Handlers with XFire
    12. 9.12. Summary
  15. 10. Web Service Consumer and Interoperation with .NET
    1. 10.1. Creating Web Service Consumers — Overview
    2. 10.2. Describing Web Services with WSDL
    3. 10.3. Creating a Web Service Consumer with XFire
      1. 10.3.1. The XFire Maven Plugin
      2. 10.3.2. Invoking Web Service Methods via XFire-Generated Stubs
    4. 10.4. Understanding the E-Mail-Validation Web Service Consumer in PIX
      1. 10.4.1. Examining a WSDL Document
      2. 10.4.2. WSDL Description of a Web Service Endpoint
    5. 10.5. Generating Web Service Stubs from WSDL Using XFire
      1. 10.5.1. The XFire WsGen Tool
      2. 10.5.2. Generated Interface for Invocation of Web Service
    6. 10.6. Creating a Web Service Consumer with XFire-Generated Stubs
    7. 10.7. Add a Web Service Consumer to PIX
    8. 10.8. Web Service Interoperability
      1. 10.8.1. WS-I and Web Service Interoperability
      2. 10.8.2. Expose PIX Service for .NET Web Service Consumers
    9. 10.9. Summary
  16. 11. Rapid Spring Development with Spring IDE
    1. 11.1. Brief Feature Overview
    2. 11.2. Installing and Setting Up Your Eclipse Environment
      1. 11.2.1. Installing Spring IDE
      2. 11.2.2. Preparing an Eclipse Project
        1. 11.2.2.1. Converting a Project to a Spring Project
        2. 11.2.2.2. Adding Bean Definition Files
    3. 11.3. Support for Spring Beans Configuration Files
      1. 11.3.1. Viewing Spring Bean Definitions
        1. 11.3.1.1. Using the Tree View of Spring Bean definitions
        2. 11.3.1.2. Using the Dependency Graph for Bean Definitions
      2. 11.3.2. Validating Spring Bean Definition Files
        1. 11.3.2.1. Understanding Config Sets in the Context of Validation
        2. 11.3.2.2. Starting Validation
      3. 11.3.3. XML Editing
        1. 11.3.3.1. Content Assist
        2. 11.3.3.2. Hyperlink
        3. 11.3.3.3. Contextual Hover Information
      4. 11.3.4. Searching for and Navigating to Bean Definitions
    4. 11.4. Visual Support for Spring AOP Configurations
      1. 11.4.1. Enabling AOP Support for Spring Projects
      2. 11.4.2. Working with Spring IDE's AOP Support
      3. 11.4.3. Integration with the AspectJ Development Tools
    5. 11.5. Web Flow Development with Spring IDE
      1. 11.5.1. Setting Up Your Spring Web Flow Project
      2. 11.5.2. Validating Spring Web Flow Definition Files
      3. 11.5.3. Editing Spring Web Flow Definition Files
      4. 11.5.4. Graphical Editor for Web Flow Definitions
        1. 11.5.4.1. Opening and Working the Graphical Web Flow Editor
        2. 11.5.4.2. Exporting and Printing Web Flow Definitions
    6. 11.6. Summary
  17. 12. Spring AOP and AspectJ
    1. 12.1. Comparing Aspect-Oriented Programming to Object-Oriented Programming
      1. 12.1.1. What Is AOP?
      2. 12.1.2. Crosscutting Concerns
        1. 12.1.2.1. AOP Concepts and Terminology
        2. 12.1.2.2. What Does This All Mean?
        3. 12.1.2.3. Refactoring fetchdata()
    2. 12.2. AOP in Spring
    3. 12.3. XML Schema-Based Support
      1. 12.3.1. The AOP Namespace Explored
        1. 12.3.1.1. Defining Aspects
        2. 12.3.1.2. Defining Pointcuts
        3. 12.3.1.3. Defining Advice
          1. 12.3.1.3.1. Before Advice
          2. 12.3.1.3.2. After Returning Advice
          3. 12.3.1.3.3. After Throwing Advice
          4. 12.3.1.3.4. After Advice
          5. 12.3.1.3.5. Around Advice
      2. 12.3.2. Advice Parameters
    4. 12.4. AspectJ Support
      1. 12.4.1. @AspectJ Explored
        1. 12.4.1.1. @AspectJ Pointcuts
        2. 12.4.1.2. Enabling @AspectJ Style of Configuration
      2. 12.4.2. @AspectJ-Style Advice
        1. 12.4.2.1. Before Advice
        2. 12.4.2.2. After Returning Advice
        3. 12.4.2.3. After Throwing Advice
        4. 12.4.2.4. After (Finally) Advice
        5. 12.4.2.5. Around Advice
        6. 12.4.2.6. Advice Parameters
    5. 12.5. Performance Monitoring with AOP and JETM
      1. 12.5.1.
        1. 12.5.1.1. Programmatic Monitoring With JETM
        2. 12.5.1.2. Declarative Monitoring With JETM
    6. 12.6. Summary
    7. 12.7. References
  18. 13. More AOP: Transactions
    1. 13.1. Understanding Transactions
      1. 13.1.1. Understanding Spring Transaction Management
        1. 13.1.1.1. Local Transactions Versus Global Transactions
        2. 13.1.1.2. Programmatic Transaction versus Declarative Transaction
      2. 13.1.2. Spring Transaction Abstraction
        1. 13.1.2.1. The PlatformTransactionManager Interface
        2. 13.1.2.2. Available Transactional Platforms Support
      3. 13.1.3. Applying AOP to Transactions
    2. 13.2. Adding Spring Transaction Support to PIX
      1. 13.2.1. Selecting the Transaction Manager
        1. 13.2.1.1. Coding Directly to the JPA Transaction Manager
        2. 13.2.1.2. Adding Transaction Support
        3. 13.2.1.3. Using Transaction Annotations
          1. 13.2.1.3.1. Propagation Property
          2. 13.2.1.3.2. Transaction Isolation Property
        4. 13.2.1.4. Read-Only Property
        5. 13.2.1.5. Rollback Properties
      2. 13.2.2. Coding Spring Transactions
        1. 13.2.2.1.
          1. 13.2.2.1.1. Using the rollbackFor Property
      3. 13.2.3. Global Transactions
    3. 13.3. Summary
  19. A. Maven 2 Basics
    1. A.1. Downloading and Installing Maven
      1. A.1.1. Downloading and Installing Maven
    2. A.2. Directory Structure
      1. A.2.1. Maven and the Project Build Life Cycle
      2. A.2.2. Maven Plug-Ins
      3. A.2.3. Plug-In Mojos
      4. A.2.4. The Project Object Model
      5. A.2.5. Maven Artifacts
      6. A.2.6. Dependency Management
        1. A.2.6.1. Running Only One Unit Test
        2. A.2.6.2. Using Wild Cards in Running Unit Tests
          1. A.2.6.2.1. Skipping Unit Tests
          2. A.2.6.2.2. Obtaining Information on a Specific Mojo of a Plug-In
    3. A.3. Summary
  20. B. Spring and Java EE
    1. B.1. A Heavyweight Application Server versus a Lightweight Framework
      1. B.1.1. The Server-Centric Architecture of Java EE
      2. B.1.2. The Lightweight Approach of Spring
    2. B.2. Complexity versus Simplicity
      1. B.2.1. Complexity Caused by Boilerplate Source Code Explosion
      2. B.2.2. Complexity in Code Interdependency
      3. B.2.3. Simplicity in POJOs
      4. B.2.4. A Convergence of Ideals
    3. B.3. Java SE Versus the Open-Source Community
      1. B.3.1. The Spring Open-Source Community
      2. B.3.2. Toward an Open-Source Java EE Application Server
    4. B.4. Java EE 5 and the Spring Framework
      1. B.4.1. Java EE Support Modules in Spring Framework 2
      2. B.4.2. EJB 3 Persistence Implementation — JPA
      3. B.4.3. Declarative Transaction Support
      4. B.4.4. POJOs for EJBs
      5. B.4.5. Dependency Injection and EJB 3
      6. B.4.6. Parallel Coexistence and Collaboration
    5. B.5. Summary
  21. C. Getting Ready for the Code Examples