Introducing Spring
What Is Spring?
Inverting Control or Injecting Dependencies?
Evolution of Dependency Injection
Beyond Dependency Injection
The Spring Project
Origins of Spring
The Spring Community
Spring for Microsoft .NET
The SpringSource Tool Suite/Spring IDE
The Spring Security Project
Spring Batch and Integration
Many Other Projects
Alternatives to Spring
JBoss Seam Framework
Google Guice
PicoContainer
JEE 6 Container
Summary
Getting Started
Obtaining the Spring Framework
Downloading a Standard Distribution
Checking Spring Out of GitHub
Understanding Spring Packaging
Spring Modules
Choosing Modules for Your Application
Spring Modules on the Maven Repository
Analyzing Spring Dependencies
The Sample Applications
Obtaining Spring Samples Source Code
The Petclinic Application
The Petclinic Groovy Application
The jPetStore Application
Spring Configuration Basic Application
Spring Task and Scheduling Application
The Spring MVC Showcase Application
The Spring MVC Basic and Ajax Application
The Spring Petcare Application
Spring Webflow Sample Applications
Spring Documentation
Putting a Spring into 'Hello World!'
Building the Sample 'Hello World!'Application
Create Spring Project in STS
Summary
The Sample Application
Requirements of the SpringBlog Application
Security and Authentication
Viewing Blog Entries
Posting Blog Entries
Commenting on a Blog Entry
Filtering Out Obscenities
Attaching Files to a Blog Entry or Comment
Auditing Blog Actions
RSS Feed
Upload Blog from an XML File
Implementing SpringBlog
Development Tool and Dependency Management
Application Design
Application Configuration Management
SpringBlog's Layered Application Architecture
Implementing the Persistence Layer
Implementing the Service Layer
Using AOP for Obscenity Filtering
Summary
Introducing IoC and DI in Spring
Inversion of Control and Dependency Injection
Types of Inversion of Control
Dependency Pull
Contextualized Dependency Lookup
Constructor Dependency Injection
Setter Dependency Injection
Injection vs. Lookup
Setter Injection vs. Constructor Injection
Inversion of Control in Spring
Dependency Injection with Spring
Beans and BeanFactories
BeanFactory Implementations
ApplicationContext
Configuring ApplicationContext
Spring Configuration Options (XML and Java Annotations)
Declare Spring Components
Using Setter Injection
Using Constructor Injection
Injection Parameters
Using Method Injection
Understanding Bean Naming
Bean Instantiation Mode
Resolving Dependencies
Autowiring Your Bean
Different Modes of Autowiring
When to Use Autowiring
Bean Inheritance
Summary
Spring Configuration in Detail
Spring's Impact on Application Portability
Bean Life-Cycle Management
Hooking into Bean Creation
Hooking into Bean Destruction
Making Your Beans 'Spring Aware'
Using the BeanNameAware Interface
Using the ApplicationContextAware Interface
Use FactoryBeans
Custom FactoryBean Example: The MessageDigestFactoryBean
Accessing a FactoryBean Directly
Using the factory-bean and factory-method Attributes
JavaBeans PropertyEditors
The Built-in PropertyEditors
Creating a Custom PropertyEditor
More Spring ApplicationContext Configuration
Internationalization with MessageSource
Using MessageSource in Stand-Alone Applications
The MessageSourceResolvable Interface
Application Events
Accessing Resources
Configuration Using Java Classes
ApplicationContext Configuration in Java
Java or XML Configuration?
Profiles
An Example of Using the Spring Profiles Feature
Considerations for Using Profiles
Environment and PropertySource Abstraction
Configuration Using JSR-330 Annotations
Summary
Introducing Spring AOP
AOP Concepts
Types of AOP
Static AOP
Dynamic AOP
Choosing an AOP Type
AOP in Spring
The AOP Alliance
'Hello World!' in AOP
Spring AOP Architecture
About the ProxyFactory Class
Creating Advice in Spring
Advisors and Pointcuts in Spring
The Pointcut Interface
Understanding Proxies
Using JDK Dynamic Proxies
Using CGLIB Proxies
Comparing Proxy Performance
Which Proxy to Use?
Summary
More Spring AOP and Annotations
Advanced Use of Pointcuts
Use Control Flow Pointcuts
Using Composable Pointcut
Composition and the Pointcut Interface
Pointcutting Summary
Getting Started with Introductions
Introduction Basics
Object Modification Detection with Introductions
Introduction Summary
Framework Services for AOP
Configuring AOP Declaratively
Using ProxyFactoryBean
Using the aop Namespace
Using @AspectJ-Style Annotations
Considerations for Declarative Spring AOP Configuration
AspectJ Integration
About AspectJ
Using Singleton Aspects
AOP in the Sample Application
Filtering Obscenities in SpringBlog
Summary
Spring JDBC Support
Sample Data Model for Example Code
Exploring the JDBC Infrastructure
Spring JDBC Infrastructure
Overview and Used Packages
Database Connections and DataSources
Embedded Database Support
Using DataSources in DAO Classes
Exception Handling
The JdbcTemplate Class
Initializing JdbcTemplate in a DAO Class
Retrieving Single-Value-Use JdbcTemplate Class
Using Named Parameters with NamedParameterJdbcTemplate
Retrieving Domain Objects with RowMapper
Retrieving Nested Domain Objects with ResultSetExtractor
Spring Classes That Model JDBC Operations
Setting Up JDBC DAO Using Annotations
Querying Data Using MappingSqlQuery
Updating Data Using SqlUpdate
Inserting Data and Retrieving the Generated Key
Batching Operations with BatchSqlUpdate
Calling Stored Functions Using SqlFunction
Using the Java Configuration
Spring Data Project: JDBC Extensions
Considerations for Using JDBC
Summary
Using Hibernate in Spring
Create a Hibernate Utility Project in STS
Sample Data Model for Example Code
Configuring Hibernate SessionFactory
ORM Mapping Using Hibernate Annotations
Simple Mappings
One-to-Many Mappings
Many-to-Many Mappings
The Hibernate Session Interface
Database Operations with Hibernate
Query Data Using Hibernate Query Language
Inserting Data
Updating Data
Deleting Data
Considerations of Using Hibernate
Summary
Data Access in Spring with JPA2
Introducing JPA 2
Creating a Simple Spring JPA Utility Project in STS
Sample Data Model for Example Code
Configuring JPA EntityManagerFactory
ORM Mapping Using JPA Annotations
Eliminating the DAO Layer
Injecting EntityManager into Service Layer Classes
Database Operations with JPA
Query Data Using the Java Persistence Query Language
Inserting Data
Updating Data
Deleting Data
Native Query
Criteria Query Using the JPA 2 Criteria API
Introducing Spring Data JPA
Adding Spring Data JPA Library Dependencies
Database Operations Using Spring Data JPA Repository Abstraction
Keeping Track of Changes on the Entity Class
Keeping Entity Versions by Using Hibernate Envers
Adding Hibernate Envers Dependencies
Adding Tables for Entity Versioning
Configuring EntityManagerFactory for Entity Versioning
Coding Changes for Entity Versioning and History Retrieval
Testing Entity Versioning
Considerations When Using JPA
Using JPA in the Sample Application
Database Backend
Using JPA for Persistence Layer Implementation
Auditing and Entity Versioning
Summary
Using MyBatis in Spring
Getting Started with MyBatis in Spring
Introducing MyBatis
Creating a Simple Utility Project with MyBatis Support in STS
Sample Data Model for Example Code
Configuring MyBatis SqlSessionFactory and MapperScannerConfigurer
SQL Mapping in MyBatis
Mapper Interfaces and SQL Mapping Files
SQL Mapping XML Configuration
Database Operations with MyBatis
Querying Data
Inserting Data
Updating Data
Deleting Data
Considerations When Using MyBatis
Using MyBatis in the Sample Application
Database Backend
Using MyBatis for Persistence Layer Implementation
Auditing and Entity Versioning
Summary
Designing and Implementing Spring-Based Applications
Designing to Interfaces
Why Design to Interfaces
The Factory Pattern
Impact of Spring on Interface-Based Design
Building a Domain Object Model
Spring and the Domain Object Model
The DOM Is Not the Same As a Value Object
Why Create a Domain Object Model
Modeling Domain Objects
To Encapsulate Behavior or Not?
The SpringBlog Domain Object Model
Domain Object Model Summary
Designing and Building the Data Access Layer
Practical Design Considerations
Data Access Layer Summary
Designing the Service Layer
Why Have a Service Layer
Designing Business Interfaces
Service Layer Summary
Summary
Transaction Management
Exploring the Spring Transaction Abstraction Layer
Transaction Types
Implementations of the PlatformTransactionManager
Analyzing Transaction Properties
The TransactionDefinition Interface
The TransactionStatus Interface
Sample Data Model and Infrastructure for Example Code
Creating a Simple Spring JPA Utility Project with Dependencies
Sample Data Model and Common Classes
Declarative and Programmatic Transactions with Spring
Using Annotations for Transaction Management
Using XML Configuration for Transaction Management
Using Programmatic Transactions
Considerations on Transaction Management
Global Transactions with Spring
Infrastructure for Implementing the JTA Sample
Implementing Global Transactions with JTA
Considerations on Using JTA Transaction Manager
Summary
Validation with Type Conversion and Formatting
Creating a Project in STS for Samples
Spring Type Conversion System
Conversion from a String Using PropertyEditors
Introducing Spring 3 Type Conversion
Field Formatting in Spring 3
Implementing a Custom Formatter
Configuring ConversionServiceFactoryBean
Validation in Spring
Using Spring Validator Interface
Using JSR-303 Bean Validation
Which Validation API to Use?
Validation with Type Conversion and Formatting in the Sample Application
Summary
Task Scheduling in Spring
Create a Project in STS for the Sample Projects
Task Scheduling in Spring
Introducing Spring TaskScheduler Abstraction
Sample Task
Task Scheduling Using task-namespace
Task Scheduling Using Annotation
Asynchronous Task Execution in Spring
Task Scheduling in the Sample Application
Summary
Using Spring Remoting
Creating the Project in STS for the Samples
Implementing the Service Layer for the Samples
Adding Required Dependencies for the JPA Backend
Verifying the Project
Data Model for Samples
Implementing and Configuring ContactService
Using the Spring HTTP Invoker
Exposing the Service
Invoking the Service
Using JMS in Spring
Setting Up ActiveMQ
Implementing a JMS Listener in Spring
Sending JMS Messages in Spring
Using RESTful-WS in Spring
Introducing RESTful Web Services
Adding Required Dependencies for Samples
The Contact RESTful Web Service
Using Spring MVC to Expose RESTful Web Services
Using curl to Test RESTful-WS
Using RestTemplate to Access RESTful-WS
Securing RESTful-WS with Spring Security
Using JSR-303 with RESTful Web Services
Remoting in the Sample Application
Summary
Web Applications with Spring
Create Project in STS for Samples
Implement the Service Layer for Samples
Data Model for Samples
Implementing and Configuring ContactService
Introducing MVC and Spring MVC
Introducing MVC
Introducing Spring MVC
Create the First View in Spring MVC
Configure the DispatcherServlet
Implement the ContactController
Implement the Contact List View
Testing the Contact List View
Spring MVC Project Structure Overview
i18n (Internationalization)
Configure i18n in DispatcherServlet Configuration
Modify the Contact List View for i18n Support
Theming and Templating
Theming Support
View Templating with Apache Tiles
Implement the Views for Contact Information
Mapping of URLs to the Views
Implementing the Show Contact View
Implementing the Edit Contact View
Implementing the Add Contact View
Enable JSR-303 Bean Validation
Using jQuery and jQuery UI
Introducing jQuery and jQuery UI
Enable jQuery and jQuery UI in a View
Rich-Text Editing with CKEditor
Data Grid with Pagination using jqGrid
File Upload Handling
Configuring File Upload Support
Modify Views for File Upload Support
Modify Controller for File Upload Support
Securing a Web Application with Spring Security
Configuring Spring Security
Adding Login Functions to the Application
Using Annotations to Secure Controller Methods
Support for Servlet 3 Code-Based Configuration
Spring MVC in the Sample Application
MVC Implementation for SpringBlog
Rich User Interface and Ajax
Security Support
Servlet 3.0 Support
Summary
Spring Web Flow and JSF
Project for Sample Backend
The Sample Backend Service Layer
Import the Sample Backend in STS
Introducing Spring Web Flow
Spring Web Flow Modules
Spring Web Flow Features
Introducing JSF
View
Model Interaction
Navigation
Application Life Cycle
The Sample Spring Web Flow Application
Design of the Sample Flow
Project Structure
Spring Web Flow and JSF Configuration
Adding Required Dependencies
Configuring JSF
Configuring Web Deployment Descriptor
Configuring Spring Web Flow and Spring MVC
Implementing the Sample Flow
Define the Flow Definition
Implementing the Template Page
Implementing a Custom Converter
Implementing the Controller and Backing Bean
Implementing the Show Contact View
Implement the Add Contact Flow
Step 1: Enter Basic Information
Step 2: Select Hobbies
Step 3: Review Information
Step 4: Add Contact Complete
Summary
Spring Testing
Project for Sample Web Application
Import the Sample Backend in STS
Introducing an Enterprise Testing Framework
Implementing Logic Unit Test
Adding Required Dependencies
Unit Testing Spring MVC Controller
Implementing an Integration Unit Test
Adding Required Dependencies
Configuring the Profile for Service Layer Testing
Implementing the Infrastructure Classes
Unit Testing Service Layer
Implementing a Frontend Unit Test
Adding Required Dependencies
Introducing Selenium
Implementing a Test Case for a Frontend UI
Verifying Test Case Code Coverage
Summary
Spring Projects: Batch, Integration, and Roo
Project for Chapter Samples
Import the Sample Backend in STS
Introducing Spring Batch
Batch Job Flow and Process
Spring Batch Infrastructure Components
Spring Batch Metadata
Job Execution Policies
Implementing a Batch Job
Adding Required Dependencies
Spring Batch Infrastructure Configuration
Implementing the Import Contact Job
Using Spring Batch with Spring Integration
Introducing Spring Integration
Adding Required Dependencies
Implementing the File Polling Mechanism
Introducing Spring Roo
Configure Spring Roo in STS
Create a Spring Roo Project
Set Up the Persistence Layer and Entity Class
Set Up the Service Layer
Set Up the Presentation Layer
Spring Roo Add-on
Conclusion on Spring Roo
Spring Batch and Spring Integration in the Sample Application
Summary
Sample Application in Detail
Setting Up the Sample Application
Project Setup
Switching Between the MySQL and H2 Databases
Switching Between the JPA and MyBatis Implementations
Application Design
The Data Model
Domain Object Model
The UML Model
Configuration Details
Implementation Details
Service Layer Implementation
Obscenity Filter Using AOP
Scheduling the Job for Purging Audit Data
Presentation Layer
Summary
Scripting Support in Spring
Project for Chapter Samples
Create a Simple Spring Utility Project
Installing the Groovy Plug-in for Eclipse
Scripting Support in Java
Introducing Groovy
Dynamic Typing
Simplified Syntax
Closure
Using Groovy with Spring
Adding Required Dependencies
The Contact Domain
Implementing the Rule Engine
Implement the Rule Factory as a Spring Refreshable Bean
Testing the Age Category Rule
Summary
Spring Application Monitoring
Project for Chapter Samples
JMX Support in Spring
Exporting a Spring Bean to JMX
Setting Up VisualVM for JMX Monitoring
Monitoring Logged-In Users
Monitoring Hibernate Statistics
Monitoring Spring Batch Jobs
Monitoring an Application with Spring Insight
Introducing Spring Insight
Configuring Spring Insight
Using Spring Insight
Summary
SpringSource Tool Suite
Introducing STS
STS Installation
Installing the Stand-Alone Version of STS
Installing STS to an Existing Eclipse Environment
Project Setup and Dependency Management
Create a Simple Spring Utility Project
Dependency Management for a Project
Using STS
Installing STS Extensions
Configuring VMware tc Server in STS
Summary