ebook img

Pro Spring PDF

713 Pages·2014·5.18 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Pro Spring

BOOKS FOR PROFESSIONALS BY PROFESSIONALS® Schaefer Ho Harrop Pro Spring RELATED 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. Learn how to: • Get started with the Spring Framework and its latest features • Use inversion of control (IoC) and dependency injection (DI) • Apply aspect-oriented programming (AOP) techniques with Spring, and why they’re important • Access and persist data using Spring and Hibernate, MyBatis, JPA 2 and more • Build transaction engines for your enterprise application and take advantage of other middle-tier features in Spring • Create Spring-based web applications using Spring MVC and more • Design and build Spring-based front ends • Work with scripting languages such as Groovy to provide enhanced functionality for your applications After reading this definitive book, you’ll be armed with the power of Spring to build complex Spring applications, top to bottom. US $54.99 Shelve in ISBN 978-1-4302-6151-3 Programming Languages/Java 55499 User level: FOURTH Intermediate–Advanced EDITION SOURCE CODE ONLINE 9781430261513 www.apress.com Pro Spring Fourth Edition Chris Schaefer Clarence Ho Rob Harrop Pro Spring Copyright © 2014 by Chris Schaefer, Clarence Ho, Rob Harrop This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed. Exempted from this legal reservation are brief excerpts in connection with reviews or scholarly analysis or material supplied specifically for the purpose of being entered and executed on a computer system, for exclusive use by the purchaser of the work. Duplication of this publication or parts thereof is permitted only under the provisions of the Copyright Law of the Publisher’s location, in its current version, and permission for use must always be obtained from Springer. Permissions for use may be obtained through RightsLink at the Copyright Clearance Center. Violations are liable to prosecution under the respective Copyright Law. ISBN-13 (pbk): 978-1-4302-6151-3 ISBN-13 (electronic): 978-1-4302-6152-0 Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image, we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights. While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material contained herein. Publisher: Heinz Weinheimer Lead Editor: Steve Anglin Development Editor: Matthew Moodie Technical Reviewer: Manuel Jordan Editorial Board: Steve Anglin, Mark Beckner, Ewan Buckingham, Gary Cornell, Louise Corrigan, James T. DeWolf, Jonathan Gennick, Robert Hutchinson, Michelle Lowman, James Markham, Matthew Moodie, Jeff Olson, Jeffrey Pepper, Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft, Gwenan Spearing, Matt Wade, Steve Weiss Coordinating Editor: Anamika Panchoo Copy Editor: Sharon Wilkey Compositor: SPi Global Indexer: SPi Global Artist: SPi Global Cover Designer: Anna Ishchenko Distributed to the book trade worldwide by Springer Science+Business Media New York, 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail [email protected], or visit www.springeronline.com. Apress Media, LLC is a California LLC and the sole member (owner) is Springer Science+Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation. For information on translations, please e-mail [email protected], or visit www.apress.com. Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Special Bulk Sales–eBook Licensing web page at www.apress.com/bulk-sales. Any source code or other supplementary materials referenced by the author in this text is available to readers at www.apress.com. For detailed information about how to locate your book’s source code, go to www.apress.com/source-code/. I dedicate this book to my wife, son, family, friends, and of course my cat Sally, who shares my home office during our waking hours, keeping me sane. — Chris Schaefer Contents About the Authors ��������������������������������������������������������������������������������������������������������������xxi About the Technical Reviewer �����������������������������������������������������������������������������������������xxiii Introduction ����������������������������������������������������������������������������������������������������������������������xxv ■ Chapter 1: Introducing Spring �������������������������������������������������������������������������������������������1 What Is Spring? �����������������������������������������������������������������������������������������������������������������������������1 Evolution of the Spring Framework �����������������������������������������������������������������������������������������������������������������������2 Inverting Control or Injecting Dependencies? ��������������������������������������������������������������������������������������������������������5 Evolution of Dependency Injection ������������������������������������������������������������������������������������������������������������������������6 Beyond Dependency Injection �������������������������������������������������������������������������������������������������������������������������������7 The Spring Project �����������������������������������������������������������������������������������������������������������������������12 Origins of Spring ��������������������������������������������������������������������������������������������������������������������������������������������������12 The Spring Community ����������������������������������������������������������������������������������������������������������������������������������������12 The Spring Tool Suite �������������������������������������������������������������������������������������������������������������������������������������������13 The Spring Security Project ���������������������������������������������������������������������������������������������������������������������������������13 Spring Batch and Integration �������������������������������������������������������������������������������������������������������������������������������13 Many Other Projects ��������������������������������������������������������������������������������������������������������������������������������������������13 Alternatives to Spring ������������������������������������������������������������������������������������������������������������������14 JBoss Seam Framework ��������������������������������������������������������������������������������������������������������������������������������������14 Google Guice ��������������������������������������������������������������������������������������������������������������������������������������������������������14 PicoContainer ������������������������������������������������������������������������������������������������������������������������������������������������������14 JEE 7 Container ���������������������������������������������������������������������������������������������������������������������������������������������������14 Summary �������������������������������������������������������������������������������������������������������������������������������������14 vii ■ Contents ■ Chapter 2: Getting Started �����������������������������������������������������������������������������������������������15 Obtaining the Spring Framework ������������������������������������������������������������������������������������������������16 Quick Start �����������������������������������������������������������������������������������������������������������������������������������������������������������16 Checking Spring Out of GitHub ����������������������������������������������������������������������������������������������������������������������������16 Understanding Spring Packaging ������������������������������������������������������������������������������������������������16 Understanding Spring Modules ���������������������������������������������������������������������������������������������������������������������������16 Choosing Modules for Your Application ���������������������������������������������������������������������������������������������������������������18 Accessing Spring Modules on the Maven Repository ������������������������������������������������������������������������������������������19 Using Spring Documentation �������������������������������������������������������������������������������������������������������19 Putting a Spring into “Hello World!” ��������������������������������������������������������������������������������������������19 Building the Sample “Hello World!” Application ��������������������������������������������������������������������������������������������������19 Refactoring with Spring ���������������������������������������������������������������������������������������������������������������������������������������24 Summary �������������������������������������������������������������������������������������������������������������������������������������26 ■ Chapter 3: Introducing IoC and DI in Spring ��������������������������������������������������������������������27 Inversion of Control and Dependency Injection ���������������������������������������������������������������������������27 Types of Inversion of Control �������������������������������������������������������������������������������������������������������28 Dependency Pull ��������������������������������������������������������������������������������������������������������������������������������������������������28 Contextualized Dependency Lookup ��������������������������������������������������������������������������������������������������������������������29 Constructor Dependency Injection �����������������������������������������������������������������������������������������������������������������������30 Setter Dependency Injection ��������������������������������������������������������������������������������������������������������������������������������31 Injection vs� Lookup ���������������������������������������������������������������������������������������������������������������������������������������������31 Setter Injection vs� Constructor Injection ������������������������������������������������������������������������������������������������������������32 Inversion of Control in Spring ������������������������������������������������������������������������������������������������������35 Dependency Injection in Spring ���������������������������������������������������������������������������������������������������35 Beans and BeanFactories ������������������������������������������������������������������������������������������������������������������������������������35 BeanFactory Implementations �����������������������������������������������������������������������������������������������������������������������������36 ApplicationContext �����������������������������������������������������������������������������������������������������������������������������������������������38 Configuring ApplicationContext ���������������������������������������������������������������������������������������������������38 Setting Spring Configuration Options ������������������������������������������������������������������������������������������������������������������38 Basic Configuration Overview������������������������������������������������������������������������������������������������������������������������������39 viii ■ Contents Declaring Spring Components �����������������������������������������������������������������������������������������������������������������������������40 Using Setter Injection ������������������������������������������������������������������������������������������������������������������������������������������44 Using Constructor Injection ���������������������������������������������������������������������������������������������������������������������������������46 Using Injection Parameters����������������������������������������������������������������������������������������������������������������������������������53 Using Method Injection ����������������������������������������������������������������������������������������������������������������������������������������72 Understanding Bean Naming �������������������������������������������������������������������������������������������������������������������������������80 Understanding Bean Instantiation Mode �������������������������������������������������������������������������������������������������������������82 Resolving Dependencies �������������������������������������������������������������������������������������������������������������86 Autowiring Your Bean ������������������������������������������������������������������������������������������������������������������86 Modes of Autowiring ��������������������������������������������������������������������������������������������������������������������������������������������87 When to Use Autowiring ��������������������������������������������������������������������������������������������������������������������������������������89 Setting Bean Inheritance �������������������������������������������������������������������������������������������������������������90 Summary �������������������������������������������������������������������������������������������������������������������������������������91 ■ Chapter 4: Spring Configuration in Detail ������������������������������������������������������������������������93 Spring’s Impact on Application Portability�����������������������������������������������������������������������������������94 Bean Life-Cycle Management �����������������������������������������������������������������������������������������������������94 Hooking into Bean Creation ���������������������������������������������������������������������������������������������������������������������������������96 Hooking into Bean Destruction ��������������������������������������������������������������������������������������������������������������������������104 Making Your Beans “Spring Aware” ������������������������������������������������������������������������������������������112 Using the BeanNameAware Interface ����������������������������������������������������������������������������������������������������������������113 Using the ApplicationContextAware Interface����������������������������������������������������������������������������������������������������114 Use of FactoryBeans �����������������������������������������������������������������������������������������������������������������117 FactoryBean Example: The MessageDigestFactoryBean �����������������������������������������������������������������������������������117 Accessing a FactoryBean Directly ��������������������������������������������������������������������������������������������������������������������121 Using the factory-bean and factory-method Attributes ������������������������������������������������������������������������������������121 JavaBeans PropertyEditors �������������������������������������������������������������������������������������������������������123 Using the Built-in PropertyEditors ���������������������������������������������������������������������������������������������������������������������123 Creating a Custom PropertyEditor ���������������������������������������������������������������������������������������������������������������������128 ix ■ Contents More Spring ApplicationContext Configuration �������������������������������������������������������������������������131 Internationalization with the MessageSource ���������������������������������������������������������������������������������������������������132 Using MessageSource in Stand-Alone Applications ������������������������������������������������������������������������������������������135 The MessageSourceResolvable Interface ����������������������������������������������������������������������������������������������������������135 Application Events ���������������������������������������������������������������������������������������������������������������������������������������������135 Accessing Resources �����������������������������������������������������������������������������������������������������������������������������������������138 Configuration Using Java Classes ���������������������������������������������������������������������������������������������140 ApplicationContext Configuration in Java ����������������������������������������������������������������������������������������������������������140 Java or XML Configuration? ������������������������������������������������������������������������������������������������������������������������������145 Profiles ��������������������������������������������������������������������������������������������������������������������������������������145 An Example of Using the Spring Profiles Feature ����������������������������������������������������������������������������������������������146 Considerations for Using Profiles ����������������������������������������������������������������������������������������������������������������������149 Environment and PropertySource Abstraction ��������������������������������������������������������������������������150 Configuration Using JSR-330 Annotations ��������������������������������������������������������������������������������154 Configuration Using Groovy �������������������������������������������������������������������������������������������������������158 Summary �����������������������������������������������������������������������������������������������������������������������������������160 ■ Chapter 5: Introducing Spring AOP ��������������������������������������������������������������������������������161 AOP Concepts ����������������������������������������������������������������������������������������������������������������������������162 Types of AOP ������������������������������������������������������������������������������������������������������������������������������163 Using Static AOP ������������������������������������������������������������������������������������������������������������������������������������������������163 Using Dynamic AOP �������������������������������������������������������������������������������������������������������������������������������������������163 Choosing an AOP Type ���������������������������������������������������������������������������������������������������������������������������������������163 AOP in Spring ����������������������������������������������������������������������������������������������������������������������������164 The AOP Alliance ������������������������������������������������������������������������������������������������������������������������������������������������164 “Hello World!” in AOP �����������������������������������������������������������������������������������������������������������������������������������������164 Spring AOP Architecture ������������������������������������������������������������������������������������������������������������������������������������166 About the ProxyFactory Class ����������������������������������������������������������������������������������������������������������������������������167 Creating Advice in Spring ����������������������������������������������������������������������������������������������������������������������������������167 x ■ Contents Advisors and Pointcuts in Spring ����������������������������������������������������������������������������������������������183 The Pointcut Interface ���������������������������������������������������������������������������������������������������������������������������������������184 Understanding Proxies ��������������������������������������������������������������������������������������������������������������200 Using JDK Dynamic Proxies �������������������������������������������������������������������������������������������������������������������������������201 Using CGLIB Proxies ������������������������������������������������������������������������������������������������������������������������������������������201 Comparing Proxy Performance ��������������������������������������������������������������������������������������������������������������������������201 Choosing a Proxy to Use ������������������������������������������������������������������������������������������������������������������������������������206 Advanced Use of Pointcuts ��������������������������������������������������������������������������������������������������������206 Using Control Flow Pointcuts ����������������������������������������������������������������������������������������������������������������������������206 Using a Composable Pointcut ����������������������������������������������������������������������������������������������������������������������������209 Composition and the Pointcut Interface ������������������������������������������������������������������������������������������������������������212 Pointcut Summary ���������������������������������������������������������������������������������������������������������������������������������������������212 Getting Started with Introductions ��������������������������������������������������������������������������������������������212 Introduction Basics ��������������������������������������������������������������������������������������������������������������������������������������������212 Object Modification Detection with Introductions ���������������������������������������������������������������������������������������������215 Introduction Summary ���������������������������������������������������������������������������������������������������������������������������������������220 Framework Services for AOP �����������������������������������������������������������������������������������������������������220 Configuring AOP Declaratively ���������������������������������������������������������������������������������������������������������������������������220 Using ProxyFactoryBean ������������������������������������������������������������������������������������������������������������������������������������221 Using the aop Namespace ���������������������������������������������������������������������������������������������������������������������������������226 Using @AspectJ-Style Annotations �������������������������������������������������������������������������������������������������������������������231 Considerations for Declarative Spring AOP Configuration ���������������������������������������������������������������������������������235 AspectJ Integration �������������������������������������������������������������������������������������������������������������������236 About AspectJ ����������������������������������������������������������������������������������������������������������������������������������������������������236 Using Singleton Aspects ������������������������������������������������������������������������������������������������������������������������������������236 Summary �����������������������������������������������������������������������������������������������������������������������������������239 ■ Chapter 6: Spring JDBC Support �����������������������������������������������������������������������������������241 Introducing Lambda Expressions ����������������������������������������������������������������������������������������������242 Sample Data Model for Example Code ��������������������������������������������������������������������������������������242 Exploring the JDBC Infrastructure ���������������������������������������������������������������������������������������������246 xi ■ Contents Spring JDBC Infrastructure �������������������������������������������������������������������������������������������������������252 Overview and Used Packages ���������������������������������������������������������������������������������������������������������������������������252 Database Connections and DataSources �����������������������������������������������������������������������������������������������������������253 Embedded Database Support ����������������������������������������������������������������������������������������������������������������������������256 Using DataSources in DAO Classes �������������������������������������������������������������������������������������������257 Exception Handling ��������������������������������������������������������������������������������������������������������������������259 The JdbcTemplate Class������������������������������������������������������������������������������������������������������������260 Initializing JdbcTemplate in a DAO Class �����������������������������������������������������������������������������������������������������������261 Retrieving a Single-Value by Using JdbcTemplate ��������������������������������������������������������������������������������������������261 Using Named Parameters with NamedParameterJdbcTemplate �����������������������������������������������������������������������263 Retrieving Domain Objects with RowMapper<T> ���������������������������������������������������������������������������������������������265 Retrieving Nested Domain Objects with ResultSetExtractor �����������������������������������������������������������������������������267 Spring Classes That Model JDBC Operations ����������������������������������������������������������������������������272 Querying Data by Using MappingSqlQuery<T> �������������������������������������������������������������������������������������������������274 Updating Data by Using SqlUpdate ��������������������������������������������������������������������������������������������������������������������280 Inserting Data and Retrieving the Generated Key ����������������������������������������������������������������������������������������������283 Batching Operations with BatchSqlUpdate ��������������������������������������������������������������������������������������������������������287 Calling Stored Functions by Using SqlFunction �������������������������������������������������������������������������������������������������294 Spring Data Project: JDBC Extensions ���������������������������������������������������������������������������������������301 Considerations for Using JDBC ��������������������������������������������������������������������������������������������������301 Summary �����������������������������������������������������������������������������������������������������������������������������������302 ■ Chapter 7: Using Hibernate in Spring ����������������������������������������������������������������������������303 Sample Data Model for Example Code ��������������������������������������������������������������������������������������304 Configuring Hibernate SessionFactory ��������������������������������������������������������������������������������������306 ORM Mapping Using Hibernate Annotations �����������������������������������������������������������������������������308 Simple Mappings �����������������������������������������������������������������������������������������������������������������������������������������������309 One-to-Many Mappings �������������������������������������������������������������������������������������������������������������������������������������314 Many-to-Many Mappings ����������������������������������������������������������������������������������������������������������������������������������318 xii

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 th
See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.