You are previewing Pro Spring, Fourth Edition.
O'Reilly logo
Pro Spring, Fourth Edition

Book Description

Pro Spring updates the perennial bestseller with the latest that the Spring Framework 4 has to offer. Now in its fourth edition, this popular book is by far the most comprehensive and definitive treatment of Spring available.

With Pro Spring, you'll learn Spring basics and core topics, and share the authors' insights and real-world experiences with remoting, Hibernate, and EJB. Beyond the basics, you'll learn how to leverage the Spring Framework to build the various tiers or parts of an enterprise Java application: transactions, web and presentation tiers, deployment, and much more. A full sample application allows you to apply many of the technologies and techniques covered in this book and see how they work together.

The agile, lightweight, opensource Spring Framework continues to be the de facto leading enterprise Java application development framework for today's Java programmers and developers. It works with other leading opensource, agile, and lightweight Java technologies such as Hibernate, Groovy, MyBatis, and more. Spring now works with Java EE and JPA 2 as well.

After reading this definitive book, you'll be armed with the power of Spring to build complex Spring applications, top to bottom.

Table of Contents

  1. Cover
  2. Title
  3. Copyright
  4. Dedication
  5. Contents at a Glance
  6. Contents
  7. About the Authors
  8. About the Technical Reviewer
  9. Introduction
  10. Chapter 1: Introducing Spring
    1. What Is Spring?
      1. Evolution of the Spring Framework
      2. Inverting Control or Injecting Dependencies?
      3. Evolution of Dependency Injection
      4. Beyond Dependency Injection
    2. The Spring Project
      1. Origins of Spring
      2. The Spring Community
      3. The Spring Tool Suite
      4. The Spring Security Project
      5. Spring Batch and Integration
      6. Many Other Projects
    3. Alternatives to Spring
      1. JBoss Seam Framework
      2. Google Guice
      3. PicoContainer
      4. JEE 7 Container
    4. Summary
  11. Chapter 2: Getting Started
    1. Obtaining the Spring Framework
      1. Quick Start
      2. Checking Spring Out of GitHub
    2. Understanding Spring Packaging
      1. Understanding Spring Modules
      2. Choosing Modules for Your Application
      3. Accessing Spring Modules on the Maven Repository
    3. Using Spring Documentation
    4. Putting a Spring into “Hello World!”
      1. Building the Sample “Hello World!” Application
      2. Refactoring with Spring
    5. Summary
  12. Chapter 3: Introducing IoC and DI in Spring
    1. Inversion of Control and Dependency Injection
    2. Types of Inversion of Control
      1. Dependency Pull
      2. Contextualized Dependency Lookup
      3. Constructor Dependency Injection
      4. Setter Dependency Injection
      5. Injection vs. Lookup
      6. Setter Injection vs. Constructor Injection
    3. Inversion of Control in Spring
    4. Dependency Injection in Spring
      1. Beans and BeanFactories
      2. BeanFactory Implementations
      3. ApplicationContext
    5. Configuring ApplicationContext
      1. Setting Spring Configuration Options
      2. Basic Configuration Overview
      3. Declaring Spring Components
      4. Using Setter Injection
      5. Using Constructor Injection
      6. Using Injection Parameters
      7. Using Method Injection
      8. Understanding Bean Naming
      9. Understanding Bean Instantiation Mode
    6. Resolving Dependencies
    7. Autowiring Your Bean
      1. Modes of Autowiring
      2. When to Use Autowiring
    8. Setting Bean Inheritance
    9. Summary
  13. Chapter 4: Spring Configuration in Detail
    1. Spring’s Impact on Application Portability
    2. Bean Life-Cycle Management
      1. Hooking into Bean Creation
      2. Hooking into Bean Destruction
    3. Making Your Beans “Spring Aware”
      1. Using the BeanNameAware Interface
      2. Using the ApplicationContextAware Interface
    4. Use of FactoryBeans
      1. FactoryBean Example: The MessageDigestFactoryBean
      2. Accessing a FactoryBean Directly
      3. Using the factory-bean and factory-method Attributes
    5. JavaBeans PropertyEditors
      1. Using the Built-in PropertyEditors
      2. Creating a Custom PropertyEditor
    6. More Spring ApplicationContext Configuration
      1. Internationalization with the MessageSource
      2. Using MessageSource in Stand-Alone Applications
      3. The MessageSourceResolvable Interface
      4. Application Events
      5. Accessing Resources
    7. Configuration Using Java Classes
      1. ApplicationContext Configuration in Java
      2. Java or XML Configuration?
    8. Profiles
      1. An Example of Using the Spring Profiles Feature
      2. Considerations for Using Profiles
    9. Environment and PropertySource Abstraction
    10. Configuration Using JSR-330 Annotations
    11. Configuration Using Groovy
    12. Summary
  14. Chapter 5: Introducing Spring AOP
    1. AOP Concepts
    2. Types of AOP
      1. Using Static AOP
      2. Using Dynamic AOP
      3. Choosing an AOP Type
    3. AOP in Spring
      1. The AOP Alliance
      2. “Hello World!” in AOP
      3. Spring AOP Architecture
      4. About the ProxyFactory Class
      5. Creating Advice in Spring
    4. Advisors and Pointcuts in Spring
      1. The Pointcut Interface
    5. Understanding Proxies
      1. Using JDK Dynamic Proxies
      2. Using CGLIB Proxies
      3. Comparing Proxy Performance
      4. Choosing a Proxy to Use
    6. Advanced Use of Pointcuts
      1. Using Control Flow Pointcuts
      2. Using a Composable Pointcut
      3. Composition and the Pointcut Interface
      4. Pointcut Summary
    7. Getting Started with Introductions
      1. Introduction Basics
      2. Object Modification Detection with Introductions
      3. Introduction Summary
    8. Framework Services for AOP
      1. Configuring AOP Declaratively
      2. Using ProxyFactoryBean
      3. Using the aop Namespace
      4. Using @AspectJ-Style Annotations
      5. Considerations for Declarative Spring AOP Configuration
    9. AspectJ Integration
      1. About AspectJ
      2. Using Singleton Aspects
    10. Summary
  15. Chapter 6: Spring JDBC Support
    1. Introducing Lambda Expressions
    2. Sample Data Model for Example Code
    3. Exploring the JDBC Infrastructure
    4. Spring JDBC Infrastructure
      1. Overview and Used Packages
      2. Database Connections and DataSources
      3. Embedded Database Support
    5. Using DataSources in DAO Classes
    6. Exception Handling
    7. The JdbcTemplate Class
      1. Initializing JdbcTemplate in a DAO Class
      2. Retrieving a Single-Value by Using JdbcTemplate
      3. Using Named Parameters with NamedParameterJdbcTemplate
      4. Retrieving Domain Objects with RowMapper<T>
      5. Retrieving Nested Domain Objects with ResultSetExtractor
    8. Spring Classes That Model JDBC Operations
      1. Querying Data by Using MappingSqlQuery<T>
      2. Updating Data by Using SqlUpdate
      3. Inserting Data and Retrieving the Generated Key
      4. Batching Operations with BatchSqlUpdate
      5. Calling Stored Functions by Using SqlFunction
    9. Spring Data Project: JDBC Extensions
    10. Considerations for Using JDBC
    11. Summary
  16. Chapter 7: Using Hibernate in Spring
    1. Sample Data Model for Example Code
    2. Configuring Hibernate SessionFactory
    3. ORM Mapping Using Hibernate Annotations
      1. Simple Mappings
      2. One-to-Many Mappings
      3. Many-to-Many Mappings
    4. The Hibernate Session Interface
    5. Database Operations with Hibernate
      1. Querying Data by Using Hibernate Query Language
      2. Inserting Data
      3. Updating Data
      4. Deleting Data
    6. Considerations When Using Hibernate
    7. Summary
  17. Chapter 8: Data Access in Spring with JPA2
    1. Introducing JPA 2.1
      1. Using the Sample Data Model for Example Code
      2. Configuring JPA EntityManagerFactory
      3. Using JPA Annotations for ORM Mapping
    2. Performing Database Operations with JPA
      1. Using the Java Persistence Query Language to Query Data
      2. Inserting Data
      3. Updating Data
      4. Deleting Data
      5. Using a Native Query
      6. Using the JPA 2 Criteria API for a Criteria Query
    3. Introducing Spring Data JPA
      1. Adding Spring Data JPA Library Dependencies
      2. Using Spring Data JPA Repository Abstraction for Database Operations
      3. Keeping Track of Changes on the Entity Class
    4. Keeping Entity Versions by Using Hibernate Envers
      1. Adding Tables for Entity Versioning
      2. Configuring EntityManagerFactory for Entity Versioning
      3. Enabling Entity Versioning and History Retrieval
      4. Testing Entity Versioning
    5. Considerations When Using JPA
    6. Summary
  18. Chapter 9: Transaction Management
    1. Exploring the Spring Transaction Abstraction Layer
      1. Transaction Types
      2. Implementations of the PlatformTransactionManager
    2. Analyzing Transaction Properties
      1. The TransactionDefinition Interface
      2. The TransactionStatus Interface
    3. Sample Data Model and Infrastructure for Example Code
      1. Creating a Simple Spring JPA Project with Dependencies
      2. Sample Data Model and Common Classes
    4. Declarative and Programmatic Transactions with Spring
      1. Using Annotations for Transaction Management
      2. Using XML Configuration for Transaction Management
      3. Using Programmatic Transactions
      4. Considerations on Transaction Management
    5. Global Transactions with Spring
      1. Infrastructure for Implementing the JTA Sample
      2. Implementing Global Transactions with JTA
      3. Considerations on Using JTA Transaction Manager
    6. Summary
  19. Chapter 10: Validation with Type Conversion and Formatting
    1. Dependencies
    2. Spring Type Conversion System
      1. Conversion from a String Using PropertyEditors
      2. Introducing Spring Type Conversion
    3. Field Formatting in Spring
      1. Implementing a Custom Formatter
      2. Configuring ConversionServiceFactoryBean
    4. Validation in Spring
      1. Using the Spring Validator Interface
      2. Using JSR-349 Bean Validation
      3. Deciding Which Validation API to Use
    5. Summary
  20. Chapter 11: Task Scheduling in Spring
    1. Dependencies for the Task Scheduling Samples
    2. Task Scheduling in Spring
      1. Introducing the Spring TaskScheduler Abstraction
      2. Exploring a Sample Task
      3. Using task-namespace for Task Scheduling
      4. Using Annotations for Task Scheduling
    3. Asynchronous Task Execution in Spring
    4. Task Execution in Spring
    5. Summary
  21. Chapter 12: Using Spring Remoting
    1. Adding Required Dependencies for the JPA Back End
      1. Using a Data Model for Samples
      2. Implementing and Configuring ContactService
    2. Using the Spring HTTP Invoker
      1. Exposing the Service
      2. Invoking the Service
    3. Using JMS in Spring
      1. Setting Up ActiveMQ
      2. Implementing a JMS Listener in Spring
      3. Sending JMS Messages in Spring
      4. Working with JMS 2.0
    4. Using RESTful-WS in Spring
      1. Introducing RESTful Web Services
      2. Adding Required Dependencies for Samples
      3. Designing the Contact RESTful Web Service
      4. Using Spring MVC to Expose RESTful Web Services
      5. Using curl to Test RESTful-WS
      6. Using RestTemplate to Access RESTful-WS
      7. Securing RESTful-WS with Spring Security
    5. Using AMQP in Spring
    6. Summary
  22. Chapter 13: Spring Testing
    1. Introducing an Enterprise Testing Framework
    2. Using Spring Test Annotations
    3. Implementing Logic Unit Tests
      1. Adding Required Dependencies
      2. Unit Testing Spring MVC Controllers
    4. Implementing an Integration Test
      1. Adding Required Dependencies
      2. Configuring the Profile for Service-Layer Testing
      3. Implementing the Infrastructure Classes
      4. Unit Testing the Service Layer
    5. Implementing a Front-End Unit Test
      1. Introducing Selenium
    6. Summary
  23. Chapter 14: Scripting Support in Spring
    1. Working with Scripting Support in Java
    2. Introducing Groovy
      1. Dynamic Typing
      2. Simplified Syntax
      3. Closure
    3. Using Groovy with Spring
      1. Adding Required Dependencies
      2. Developing the Contact Domain
      3. Implementing the Rule Engine
      4. Implementing the Rule Factory as a Spring Refreshable Bean
      5. Testing the Age Category Rule
    4. Inlining Dynamic Language Code
    5. Summary
  24. Chapter 15: Spring Application Monitoring
    1. JMX Support in Spring
    2. Exporting a Spring Bean to JMX
    3. Setting Up VisualVM for JMX Monitoring
    4. Monitoring Hibernate Statistics
    5. Summary
  25. Chapter 16: Web Applications with Spring
    1. Implementing the Service Layer for Samples
      1. Using a Data Model for Samples
      2. Implementing and Configuring ContactService
    2. Introducing MVC and Spring MVC
      1. Introducing MVC
      2. Introducing Spring MVC
    3. Creating the First View in Spring MVC
      1. Configuring the DispatcherServlet
      2. Implementing the ContactController
      3. Implementing the Contact List View
      4. Testing the Contact List View
    4. Understanding Spring MVC Project Structure
    5. Enabling i18n (Internationalization)
      1. Configuring i18n in DispatcherServlet Configuration
      2. Modifying the Contact List View for i18n Support
    6. Using Theming and Templating
      1. Theming Support
      2. View Templating with Apache Tiles
    7. Implementing the Views for Contact Information
      1. Mapping URLs to the Views
      2. Implementing the Show Contact View
      3. Implementing the Edit Contact View
      4. Implementing the Add Contact View
      5. Enabling JSR-349 Bean Validation
    8. Using jQuery and jQuery UI
      1. Introducing jQuery and jQuery UI
      2. Enabling jQuery and jQuery UI in a View
      3. Rich-Text Editing with CKEditor
      4. Using jqGrid for a Data Grid with Pagination
    9. Handling File Upload
      1. Configuring File Upload Support
      2. Modifying Views for File Upload Support
      3. Modifying Controller for File Upload Support
    10. Securing a Web Application with Spring Security
      1. Configuring Spring Security
      2. Adding Login Functions to the Application
      3. Using Annotations to Secure Controller Methods
    11. Supporting Servlet 3 Code-Based Configuration
    12. Summary
  26. Chapter 17: WebSocket
    1. Introducing WebSocket
    2. Using WebSocket with Spring
      1. Using the WebSocket API
      2. Using SockJS
      3. Sending Messages with STOMP
    3. Summary
  27. Chapter 18: Spring Projects: Batch, Integration, XD, and Boot
    1. Spring Batch
      1. JSR-352
    2. Spring Integration
    3. Spring XD
    4. Spring Boot
    5. Summary
  28. Index