+ All Categories
Home > Documents > Pro Spring 2.5

Pro Spring 2.5

Date post: 08-Dec-2016
Category:
Upload: jan-machacek
View: 280 times
Download: 2 times
Share this document with a friend
920
THE EXPERT’S VOICE ® IN OPEN SOURCE Pro Spring 2.5 Jan Machacek, Aleksa Vukotic, Anirvan Chakraborty, and Jessica Ditt Foreword by Rob Harrop, Principal Software Engineer and Lead Engineer of SpringSource Application Platform Core skills for Spring 2, Spring 2.5 and forward Core skills for Spring 2, Spring 2.5 and forward The Spring Framework 2.5 release reflects the state of the art in both the Spring Framework and in enterprise Java frameworks as a whole. A guidebook to this critical tool is necessary reading for any conscientious Java developer. —Rob Harrop, author Pro Spring
Transcript
  • this print for content onlysize & color not accurate spine = 1.724" 920 page count

    Books for professionals By professionals

    Pro Spring 2.5Dear Readers,

    When Rob Harrop and I wrote Pro Spring in 2005, we could see a distinct move from so-called heavyweight architectures, such as Enterprise JavaBeans, toward light-weight frameworks, like Spring. Since then, this trend has not stopped; in fact, its picked up pace. The Spring Framework remains the leader in this move and pro-vides a platform on which you can build your own applications and services.

    This book covers the new features of Spring 2.5, but moreover, we focus on the best practices and core standards of contemporary Spring development. As members of the Spring development team at Cake Solutions, we bring extensive practical experience gained from working with Spring since version 1.0 and deliv-ering successful systems on top of it. In this book, we show you the approaches that really matter in a professional, enterprise-level environment, so you can apply them to your projects today, safe in the knowledge that they just work.

    We show you how to use Springs Inversion of Control (IoC) core to express the dependencies between your applications components without having to write any Java code, as well as Spring's excellent aspect-oriented program-ming (AOP) support, including coverage of Spring 2.5s new @AspectJ feature. Other topics new to this edition are how to use Springs dynamic scripting lan-guage features, Spring design patterns, and performance tuning. At each stage, we explain what works in real-world Spring development, giving you the skills you need to use Springs core to the best effect in your own applications.

    We also step beyond the IoC and AOP core and describe Springs support for the JDBC framework, Hibernate, the Quartz enterprise scheduler, declarative transaction management, and much more. Finally, we cover Springs well-designed MVC framework and adding AJAX to your Spring web applications, so that you will be able to create flexible, efficient, and manageable applications using the best techniques available.

    Jan Machacek, Aleksa Vukotic, Anirvan Chakraborty, and Jessica Ditt

    US $49.99

    Shelve in Java Programming

    User level: IntermediateAdvanced

    Machacek,Vukotic,

    Chakraborty,Ditt

    Spring 2.5

    The eXperTs Voice in open source

    ProSpring 2.5

    cyan MaGenTa

    yelloW Black panTone 123 c

    Jan Machacek, author ofPro Visual Studio .NETOracle Application Server 10g: J2EE Deployment and AdministrationPro Spring

    Jan Machacek, Aleksa Vukotic, Anirvan Chakraborty, and Jessica DittForeword by Rob Harrop, Principal Software Engineer and Lead Engineer of SpringSource Application Platform

    Companion eBook Available

    THE APRESS ROADMAPExpert Spring MVC

    and Web Flows

    Pro Spring 2.5Beginning Spring 2 Pro JavaTM

    EESpring Patterns

    The Definitive Guideto Terracotta

    www.apress.comSOURCECODEONLINE

    Companion eBook

    See last page for details

    on $10 eBook version

    ISBN-13: 978-1-59059-921-1ISBN-10: 1-59059-921-7

    9 781590 599211

    54999

    Core skills for

    Spring 2, Spring 2.5

    and forward

    Core skills for

    Spring 2, Spring 2.5

    and forward

    Pro

    Anirvan Chakraborty

    Aleksa Vukotic

    Jessica Ditt

    The Spring Framework 2.5 release reflects the state of the art in both the Spring

    Framework and in enterprise Java frameworks as a whole. A guidebook to this

    critical tool is necessary reading for any conscientious Java developer.

    Rob Harrop, author Pro Spring

  • Jan Machacek, Aleksa Vukotic,Anirvan Chakraborty, and Jessica Ditt

    Pro Spring 2.5

    9217ch00FM.qxd 7/25/08 5:58 PM Page i

  • Pro Spring 2.5

    Copyright 2008 by Jan Machacek, Aleksa Vukotic, Anirvan Chakraborty, and Jessica Ditt

    All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means,electronic or mechanical, including photocopying, recording, or by any information storage or retrievalsystem, without the prior written permission of the copyright owner and the publisher.

    ISBN-13 (pbk): 978-1-59059-921-1

    ISBN-10 (pbk): 1-59059-921-7

    ISBN-13 (electronic): 978-1-4302-0506-7

    Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1

    Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrenceof a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademarkowner, with no intention of infringement of the trademark.

    SpringSource is the company behind Spring, the de facto standard in enterprise Java. SpringSource isa leading provider of enterprise Java infrastructure software, and delivers enterprise class software, supportand services to help organizations utilize Spring. The open sourcebased Spring Portfolio is a comprehen-sive enterprise application framework designed on long-standing themes of simplicity and power. Withmore than five million downloads to date, Spring has become an integral part of the enterprise applicationinfrastructure at organizations worldwide. For more information, visit www.springsource.com.

    Lead Editors: Steve Anglin and Tom WelshTechnical Reviewer: Rick EvansEditorial Board: Clay Andres, Steve Anglin, Ewan Buckingham, Tony Campbell, Gary Cornell, Jonathan

    Gennick, Kevin Goff, Matthew Moodie, Joseph Ottinger, Jeffrey Pepper, Frank Pohlmann, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh

    Project Manager: Sofia MarchantCopy Editors: Heather Lang, Damon LarsonAssociate Production Director: Kari Brooks-CoponyProduction Editor: Kelly WinquistCompositor: Kinetic Publishing ServicesProofreader: April EddyIndexer: Broccoli Information ManagementCover Designer: Kurt KramesManufacturing Director: Tom Debolski

    Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor,New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail [email protected], orvisit http://www.springeronline.com.

    For information on translations, please contact Apress directly at 2855 Telegraph Avenue, Suite 600, Berkeley,CA 94705. Phone 510-549-5930, fax 510-549-5939, e-mail [email protected], or visit http://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 SpecialBulk SaleseBook Licensing web page at http://www.apress.com/info/bulksales.

    The information in this book is distributed on an as is basis, without warranty. Although every precautionhas been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability toany person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectlyby the information contained in this work.

    The source code for this book is available to readers at http://www.apress.com. You will need to answerquestions pertaining to this book in order to successfully download the code.

    9217ch00FM.qxd 7/30/08 3:54 PM Page ii

  • To Marc, who still thinks that beans grow on treesJan

    To my parents, Andja and Slobodan, who have guided me through life and encouraged meto follow my own path

    Aleksa

    To my parents, Sujata and Kalyan, for their unconditional love and supportAnirvan

    To Ioannis, for the vital nudges when I needed them most (and so much more), and to myfamily, just for being my family

    Jessica

    9217ch00FM.qxd 7/25/08 5:58 PM Page iii

  • Contents at a Glance

    Foreword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi

    About the Authors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii

    About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiv

    Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv

    Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxvii

    PART 1 Getting Started with SpringCHAPTER 1 Introducing Spring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3CHAPTER 2 Getting Started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13CHAPTER 3 Introducing Inversion of Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31CHAPTER 4 Beyond the Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73CHAPTER 5 Introducing Spring AOP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147CHAPTER 6 Advanced AOP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199CHAPTER 7 Spring Schemas and Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259CHAPTER 8 Spring Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279

    PART 2 Data AccessCHAPTER 9 JDBC Support. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319CHAPTER 10 iBATIS Integration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373CHAPTER 11 Hibernate Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 399

    PART 3 Enterprise Application ComponentsCHAPTER 12 Job Scheduling with Spring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445CHAPTER 13 Mail Support in Spring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 477CHAPTER 14 Dynamic Languages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 511

    iv

    9217ch00FM.qxd 7/25/08 5:58 PM Page iv

  • PART 4 Java EE 5CHAPTER 15 Using Spring Remoting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 533CHAPTER 16 Transaction Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 575CHAPTER 17 Web Applications with Spring MVC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 611CHAPTER 18 Spring Web Flow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 711CHAPTER 19 Spring and AJAX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 757CHAPTER 20 JMX with Spring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 771CHAPTER 21 Testing with Spring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 793CHAPTER 22 Spring Performance Tuning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 829

    INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 857

    v

    9217ch00FM.qxd 7/25/08 5:58 PM Page v

  • 9217ch00FM.qxd 7/25/08 5:58 PM Page vi

  • Contents

    Foreword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi

    About the Authors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii

    About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiv

    Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv

    Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxvii

    PART 1 Getting Started with SpringCHAPTER 1 Introducing Spring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

    What Is Spring? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3Beyond Dependency Injection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6Aspect-Oriented Programming with Spring . . . . . . . . . . . . . . . . . . . . . . . . . . 6Accessing Data in Spring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6Simplifying and Integrating with Java EE . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7Job Scheduling Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7Mail Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7Dynamic Languages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8Remoting Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8Managing Transactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8The Spring MVC Framework . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8Spring Web Flow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9AJAX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9Internationalization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9Simplified Exception Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

    The Spring Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10Origins of Spring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10The Spring Community . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10Spring for Microsoft .NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10The Spring IDE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11The Spring Security (Formerly Acegi) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11Alternatives to Spring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

    The Sample Code. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12

    vii

    9217ch00FM.qxd 7/25/08 5:58 PM Page vii

  • CHAPTER 2 Getting Started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13

    Obtaining the Spring Framework . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13Checking Out Spring from CVS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13Building Spring from Source Code. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14Verifying Your Spring Distribution. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15Spring Packaging. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15Spring Dependencies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17Spring Sample Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20Setting Up Spring and Your IDE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21

    Hello, World. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24Putting Spring Into Hello, World . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27

    Dependency Injection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28The Impact of Spring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30

    Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30

    CHAPTER 3 Introducing Inversion of Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31

    IoC and DI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31Types of IoC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32

    Contextualized Dependency Lookup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33Constructor DI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35Setter DI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36Injection vs. Lookup. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37Setter Injection vs. Constructor Injection. . . . . . . . . . . . . . . . . . . . . . . . . . . . 38

    IoC in Spring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39DI with Spring. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39

    Beans and BeanFactories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40BeanFactory Implementations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40XML Bean Definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41Using Constructor Injection. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43Injection Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46Understanding Bean Naming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56Bean Instantiation Modes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59Resolving Dependencies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62Automatically Wiring Your Beans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65Checking Dependencies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68Bean Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70

    Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72

    CONTENTSviii

    9217ch00FM.qxd 7/25/08 5:58 PM Page viii

  • CHAPTER 4 Beyond the Basics. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73

    Springs Impact on Application Portability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74Bean Life Cycle Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74

    Hooking into Bean Creation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75Hooking into Bean Destruction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82

    Making Your Beans Spring Aware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87Using the BeanNameAware Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87Using the BeanFactoryAware Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89

    Using Method Injection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91Lookup Method Injection. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91Method Replacement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96

    Using FactoryBean . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100The MessageDigestFactoryBean . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100Accessing a FactoryBean Directly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102

    The BeanFactoryPostProcessor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103Implementing a BeanFactoryPostProcessor . . . . . . . . . . . . . . . . . . . . . . . . 106

    JavaBeans PropertyEditor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110The Built-in PropertyEditors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110Creating a Custom PropertyEditor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113

    The BeanPostProcessor. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118Implementing a BeanPostProcessor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120When to Use BeanPostProcessor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124

    The Spring ApplicationContext . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125Implementations of ApplicationContext . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126Using ApplicationContextAware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126Controlling Bean Initialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127Using Annotation-Based Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129Internationalization with MessageSource . . . . . . . . . . . . . . . . . . . . . . . . . . 133Using MessageSource in Stand-Alone Applications. . . . . . . . . . . . . . . . . . 141The MessageSourceResolvable Interface . . . . . . . . . . . . . . . . . . . . . . . . . . 141Using Application Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141Considerations for Event Usage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143Accessing Resources. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144

    Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145

    CHAPTER 5 Introducing Spring AOP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147

    AOP Concepts. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148Types of AOP. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149

    Static AOP. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149Dynamic AOP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149Choosing an AOP Type. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149

    CONTENTS ix

    9217ch00FM.qxd 7/25/08 5:58 PM Page ix

  • AOP in Spring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149The AOP Alliance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150Hello, World in AOP. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150Spring AOP Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152The ProxyFactory Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153Creating Advice in Spring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153

    Advisors and Pointcuts in Spring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169The Pointcut Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170Using ComposablePointcut . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187Pointcutting Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191

    All About Proxies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191Understanding Proxies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192Using JDK Dynamic Proxies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192Using CGLIB Proxies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192Comparing Proxy Performance. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193Which Proxy to Use? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196

    Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196

    CHAPTER 6 Advanced AOP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199

    @AspectJ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199@AspectJ Aspects in Detail . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203

    Pointcuts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203Pointcut Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207Exploring the Pointcut Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208Using the @Pointcuts in XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211Types of Advice . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212Argument Binding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220Introductions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221The Aspect Life Cycle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227

    Framework Services for AOP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228Creating Our First Aspect Using the aop Namespace . . . . . . . . . . . . . . . . 229Pointcuts in the aop Namespace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230Creating Advice Using the aop Namespace . . . . . . . . . . . . . . . . . . . . . . . . 231Introductions in the aop Namespace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237

    Which Style Should You Use? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240Working with Spring AOP Proxies. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241

    Impact of Proxies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242AspectJ Integration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246

    Creating Your First AspectJ Aspect. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247Compiling the Sample Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249AspectJs Aspect Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251

    CONTENTSx

    9217ch00FM.qxd 7/25/08 5:58 PM Page x

  • Load-Time Weaving . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251Your First Load-Time Weaving Example . . . . . . . . . . . . . . . . . . . . . . . . . . . 252LoadTimeWeaver Lookup Strategies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254

    Practical Uses of AOP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254Performance and Health Monitoring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255

    Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257

    CHAPTER 7 Spring Schemas and Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . 259

    Why the New Configuration? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259Schemas Included in Spring 2.5. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262

    The beans Schema . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262The context Schema . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263The util Schema . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263The tx Schema . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266The aop Schema . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266The jee Schema . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267The lang Schema . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267

    Behind the Schema Scenes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268Custom Schemas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270IDE Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277

    CHAPTER 8 Spring Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279

    Directory Structure. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279Simple Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279Complex Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281Packaging and Naming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281

    Design Patterns Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282Programming Against Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282Creational Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283Structural Patterns. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287Behavioral Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290Template Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 292

    Spring Application Patterns. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293Layered Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 294High-Performance Paging. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295Multiple Error Reporting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298User Interface Transactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301Background Processes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 304E-mail Notifications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309Error Collecting and Logging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311

    Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315

    CONTENTS xi

    9217ch00FM.qxd 7/25/08 5:58 PM Page xi

  • PART 2 Data AccessCHAPTER 9 JDBC Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319

    Key JDBC Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319Using the DriverManager and Connections. . . . . . . . . . . . . . . . . . . . . . . . . 322Using PreparedStatements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323Using CallableStatements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 326Other JDBC Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327

    Concepts in Spring Data Access Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327JDBC Data Access Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 328Using the JdbcTemplate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329

    JdbcTemplate.execute . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 330JdbcTemplate.query and Friends . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 334JdbcTemplate.update . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339JdbcTemplate.batchUpdate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 340

    RdbmsOperation Subclasses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 342SqlUpdate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343BatchSqlUpdate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 348SqlCall and StoredProcedure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349SqlQuery and Its Subclasses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351JdbcTemplate or RdbmsOperation?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 358

    Large Binary Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 358JdbcDaoSupport. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 361Simple Spring JDBC. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 364

    SimpleJdbcTemplate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 365SimpleJdbcCall . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 367SimpleJdbcInsert . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 369SimpleJdbcDaoSupport. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 370

    Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371

    CHAPTER 10 iBATIS Integration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373

    What Is iBATIS? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373iBATIS Versions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 374Infrastructure and Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 374

    Mapping Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 375sqlMap Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 376Configuring iBATIS and Spring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 378

    CONTENTSxii

    9217ch00FM.qxd 7/25/08 5:58 PM Page xii

  • Selecting Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 380Simple Selects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 380One-to-One Selects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 384One-to-Many Selects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 388Many-to-Many Selects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 390

    Updating Data. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 391Deleting Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393Inserting Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 394Whats Missing from iBATIS? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 397Overall Performance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 397Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 398

    CHAPTER 11 Hibernate Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 399

    Hibernate Primer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 399Packaging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 400Introduction to Hibernate Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 401

    Using Hibernate Sessions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 403Using HibernateDaoSupport . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 407Deciding Between HibernateTemplate and Session. . . . . . . . . . . . . . . . . . 409

    Using Hibernate in Enterprise Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 413Preventing Stale Data Updates. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 413Object Equality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 416Transactional Behavior . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 419Lazy Loading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 424Dealing with Large Data Sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 435Handling Large Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 437Combining Hibernate with Other DAO Code . . . . . . . . . . . . . . . . . . . . . . . . 441

    Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 441

    PART 3 Enterprise Application ComponentsCHAPTER 12 Job Scheduling with Spring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445

    Scheduling Jobs Using JDK Timer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 446Trigger Types with Timer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 446Creating a Simple Job . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 447Spring Support for JDK Timer Scheduling. . . . . . . . . . . . . . . . . . . . . . . . . . 449

    Scheduling Jobs Using OpenSymphony Quartz . . . . . . . . . . . . . . . . . . . . . . . . . . 455Introducing Quartz . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 455Quartz Support in Spring. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 464

    CONTENTS xiii

    9217ch00FM.qxd 7/25/08 5:58 PM Page xiii

  • Job Scheduling Considerations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 470Choosing a Scheduler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 470Packaging Job Logic Separately from the Job Class . . . . . . . . . . . . . . . . . 470Task Execution and Thread Pooling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 471

    Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 475

    CHAPTER 13 Mail Support in Spring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 477

    The Spring Mail API Structure. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 479Configuring Mail Settings Using Spring . . . . . . . . . . . . . . . . . . . . . . . . . . . . 479

    Sending Simple E-mails . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 479Constructing and Sending E-mail Programmatically . . . . . . . . . . . . . . . . . 479Constructing E-mail Declaratively . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 482Constructing and Sending MIME Messages . . . . . . . . . . . . . . . . . . . . . . . . 485Insight into Enterprise-Level E-mail Handling. . . . . . . . . . . . . . . . . . . . . . . 500

    Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 509

    CHAPTER 14 Dynamic Languages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 511

    Supported Languages Primer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 511BeanShell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 511Groovy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 513JRuby . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 514

    Using Dynamic Languages As Spring Beans. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 515Behind the Scenes of Dynamic Language Support . . . . . . . . . . . . . . . . . . 517Proxying Dynamic Language Beans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 518Performance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 519

    Refreshable Beans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 521BeanShell Beans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 523JRuby Beans. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 525Groovy Beans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 526Typical Usage for Dynamic Languages in Spring Applications . . . . . . . . . . . . . . 526Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 530

    PART 4 Java EE 5CHAPTER 15 Using Spring Remoting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 533

    Spring Remoting Architecture. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 535Remote Method Invocation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 535

    Exposing Arbitrary Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 536Accessing an RMI Service Using Proxies . . . . . . . . . . . . . . . . . . . . . . . . . . 538Exposing CORBA Services. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 540Accessing a CORBA Service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 543

    CONTENTSxiv

    9217ch00FM.qxd 7/25/08 5:58 PM Page xiv

  • Web Services with JAX-RPC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 544Introducing Apache Axis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 545Creating a Web Service with ServletEndpointSupport . . . . . . . . . . . . . . . . 545Accessing RPC-Style Web Services using Proxies . . . . . . . . . . . . . . . . . . . 549Working with JavaBeans in Axis Services. . . . . . . . . . . . . . . . . . . . . . . . . . 552

    Using JAX-WS Web Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 556Exposing Web Services Using SimpleJaxWsServiceExporter . . . . . . . . . . 556Exposing a Web Service Using XFire . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 557Accessing JAX-WS Web Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 559Accessing Java Web Services from Other Clients . . . . . . . . . . . . . . . . . . . 560

    Creating Web Services with HTTP Invoker . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 563Exposing Simple Services. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 564Accessing an HTTP Invoker Service Using Proxies . . . . . . . . . . . . . . . . . . 567Using Arbitrary Objects in HTTP Invoker Services . . . . . . . . . . . . . . . . . . . 567Using HTTP Basic Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 569

    Choosing a Remoting Architecture. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 573Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 574

    CHAPTER 16 Transaction Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 575

    Exploring the Spring Transaction Abstraction Layer . . . . . . . . . . . . . . . . . . . . . . . 575Analyzing Transaction Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 576

    Exploring the TransactionDefinition Interface . . . . . . . . . . . . . . . . . . . . . . . 576Using the TransactionStatus Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 578Implementations of the PlatformTransactionManager . . . . . . . . . . . . . . . . 579

    Exploring a Transaction Management Sample . . . . . . . . . . . . . . . . . . . . . . . . . . . 579Nontransactional Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 580

    Programmatic Transaction Management. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 588Using the TransactionTemplate Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 590Programmatic Transaction Management Summary. . . . . . . . . . . . . . . . . . 591

    Declarative Transaction Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 591Using the TransactionProxyFactoryBean . . . . . . . . . . . . . . . . . . . . . . . . . . . 591Implications of Using Proxies in Transaction Management . . . . . . . . . . . . 593

    AOP Transaction Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 594Using Annotation-Based AOP Transaction Management . . . . . . . . . . . . . . 594Using XML AOP Transaction Management . . . . . . . . . . . . . . . . . . . . . . . . . 596

    Working with Transactions Over Multiple Transactional Resources . . . . . . . . . . 599Implementing Your Own Transaction Synchronization . . . . . . . . . . . . . . . . . . . . . 601Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 610

    CONTENTS xv

    9217ch00FM.qxd 7/25/08 5:58 PM Page xv

  • CHAPTER 17 Web Applications with Spring MVC . . . . . . . . . . . . . . . . . . . . . . . . . . 611

    MVC Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 611Spring MVC. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 613

    MVC Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 613Using Handler Mappings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 614Spring Controllers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 616

    AbstractController . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 616ParameterizableViewController . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 617MultiActionController . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 618

    Interceptors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 621Views, Locales, and Themes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 622

    Using Views Programmatically . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 622Using View Resolvers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 625Using Localized Messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 629Using Locales . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 630Using Themes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 630

    Command Controllers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 633Using Form Controllers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 633Exploring the AbstractWizardFormController . . . . . . . . . . . . . . . . . . . . . . . 640File Upload . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 644

    Handling Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 647Spring and Other Web Technologies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 650

    Using JSP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 651Using Velocity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 669FreeMarker . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 674Using XSLT Views. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 678Using PDF Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 680Using Excel Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 682Using Tiles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 684JasperReports . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 696

    Spring Conventions Over Configuration. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 701Controller Conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 701MultiActionController Conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 702Model Conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 703View Conventions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 704

    Using Annotations for Controller Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . 705@Controller. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 705@RequestMapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 706@RequestParam . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 707@ModelAttribute . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 707Using Annotations with the Command Controller . . . . . . . . . . . . . . . . . . . . 708

    Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 709

    CONTENTSxvi

    9217ch00FM.qxd 7/25/08 5:58 PM Page xvi

  • CHAPTER 18 Spring Web Flow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 711

    Introducing Spring Web Flow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 712Core Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 713Obtaining Spring Web Flow. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 716Spring Web Flow Dependencies. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 718

    Hello, Web Flow!. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 719Exploring States . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 723

    View State. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 724Decision State . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 724End State . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 725

    Working with Transitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 725Advanced Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 727

    Expression Languages and Scopes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 727Implementing Actions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 731Model Data Binding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 732Partial Rendering of Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 736Mapping Flow Input and Output Parameters . . . . . . . . . . . . . . . . . . . . . . . 736Using Subflows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 737

    Spring Web Flow Behind the Scenes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 738Flow Execution Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 739Flow Executor. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 741Flow Definition Registry . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 742Flow Execution Repository . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 745

    Integration with Spring MVC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 746Flow Handling. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 746View Resolving . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 747

    Securing Flows with Spring Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 747Step 1: Adding the SecurityFlowExecutionListener . . . . . . . . . . . . . . . . . . 748Step 2: Basic Authentication and Authorization Handling . . . . . . . . . . . . . 748Step 3: Defining Security Rules in Flow Definitions . . . . . . . . . . . . . . . . . . 750

    Problem Solver . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 752Stateful Navigational Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 753Browser Navigation Bar Support and Double Submit . . . . . . . . . . . . . . . . 753

    Testing Flow Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 753Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 754

    CONTENTS xvii

    9217ch00FM.qxd 7/25/08 5:58 PM Page xvii

  • CHAPTER 19 Spring and AJAX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 757

    DWR. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 758Installing DWR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 758Spring Configuration for DWR. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 758About the Complete Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 760Testing the DWR Configuration. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 764Running the Complete Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 765DWR Scripting Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 766

    Using Simple Callback Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 766Calling Metadata Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 767

    engine.js . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 767Call Batching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 767Call Ordering. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 768Handling Errors and Warnings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 768

    util.js . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 768Security in DWR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 768Advantages and Disadvantages of DWR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 770Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 770

    CHAPTER 20 JMX with Spring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 771

    JMX Refresher . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 771Exposing Your Beans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 772

    MBeanExporter. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 772MBeanServerFactoryBean . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 774Exposing Your Beans in an Existing MBean Server . . . . . . . . . . . . . . . . . . 775Bean Registration Behavior . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 776

    Controlling Object Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 776Controlling the Management Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 777

    MBeanInfoAssembler Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 777MethodNameBasedMBeanInfoAssembler Interface . . . . . . . . . . . . . . . . . . 777Using Java Interfaces to Control the Management Interface . . . . . . . . . . 780Using Source-Level Metadata . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 782

    Remoting with Spring JMX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 785Exposing Remote MBeans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 785Accessing Remote MBeans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 786Proxying MBeans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 786

    Notifications in Spring JMX. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 787Notification Listeners. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 787Publishing Notifications. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 788

    Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 792

    CONTENTSxviii

    9217ch00FM.qxd 7/25/08 5:58 PM Page xviii

  • CHAPTER 21 Testing with Spring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 793

    Unit Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 793Unit Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 795

    Unit Testing the Web Layer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 798Integration Tests. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 800

    Using AbstractSpringContextTests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 807Using AbstractDependencyInjectionSpringContextTests . . . . . . . . . . . . . . 809Using AbstractTransactionalSpringContextTests. . . . . . . . . . . . . . . . . . . . . 811Using AbstractAnnotationAwareTransactionalTests . . . . . . . . . . . . . . . . . . 813JNDI. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 817

    Spring TestContext Framework. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 819Application Context and DI with the TestContext Framework . . . . . . . . . . 820Transactions in the TestContext Framework . . . . . . . . . . . . . . . . . . . . . . . . 822Support Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 824

    Test Coverage. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 826Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 827

    CHAPTER 22 Spring Performance Tuning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 829

    Performance vs. Responsiveness. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 829Exploring Enterprise Application Performance Issues . . . . . . . . . . . . . . . . . . . . . 830Measuring Java EE Application Performance . . . . . . . . . . . . . . . . . . . . . . . . . . . . 830

    Finding Out What to Measure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 830Determining the Necessary Data Sets. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 837Improving the Data Access Tier . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 837Improving Transaction Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 847Controlling the Performance of Remote Calls . . . . . . . . . . . . . . . . . . . . . . . 849Understanding View Performance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 849

    Using Caching. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 849Performance Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 851Monitoring Application Health and Performance . . . . . . . . . . . . . . . . . . . . . . . . . 853More Resources on Performance Tuning. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 855Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 855

    INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 857

    CONTENTS xix

    9217ch00FM.qxd 7/25/08 5:58 PM Page xix

  • 9217ch00FM.qxd 7/25/08 5:58 PM Page xx

  • Foreword

    It was with a heavy heart that I made the decision not to participate in writing Pro Spring 2.5. I amdeeply thankful that Jan was around to pick up this book and run with it. Pro Spring has been a bigpart of my life for over three years, and I didnt relinquish the reins lightly. When Juergen and I setout working on Spring Framework 2.0, I knew that I wouldnt have the time to participate in thewriting process and write the software at the same time. Fortunately, Jan was there to step into thebreach.

    Jan and Apress had additionally planned to release Pro Spring 2.0, but Juergen and I inadver-tently made it impossible for them to keep up by making many changes to the Spring Framework.I vividly remember cringing when updating all the JSP form tags, knowing that I was creating yetmore work for Jan.

    With the 2.5 release just on the horizon, Jan made the sensible choice to forego a 2.0 editionand head straight for 2.5. This was a wise move. The Spring Framework 2.5 release reflects the stateof the art in both the Spring Framework and in enterprise Java frameworks as a whole. A guide bookto this critical tool is necessary reading for any conscientious Java developer.

    I recall, back in the early days of running Cake Solutions, when we decided we needed to hireanother programmer. We were very inexperienced at hiring in general, and hiring programmers isfraught with problems. We knew that we wanted to get a graduate, but we never imagined that wewould get someone as accomplished as Jan.

    I remember, in his first week, he wrote a complete desktop mailing package from scratchandit worked. Over the last five years, Jan has been at the center of most of the projects run at Cake, manyof which are large-scale Java products based on the Spring Framework. His knowledge of Spring comesfrom an immense amount of practical experience: he has been in the trenches with Spring sinceversion 1.0 and has delivered successful systems on top of it.

    To his credit, Jan realized that writing Pro Spring 2.5 was too big a job for just one man, so heroped in the rest of the Cake Solutions team to help him. This prospect excited me greatly a teamof real programmers, with real experience in Spring, passing along that knowledge. There is no doubtthat many will find this book to be an indispensable reference.

    And so, although I am disappointed at being unable to work on this book myself, I am glad thatJan was there to deliver what so many people have been asking for, an updated version of Pro Spring.Enjoy,

    Rob HarropPrincipal Software Engineer and Lead Engineer

    of the SpringSource Application Platform

    xxi

    9217ch00FM.qxd 7/25/08 5:58 PM Page xxi

  • 9217ch00FM.qxd 7/25/08 5:58 PM Page xxii

  • About the Authors

    JAN MACHACEK is the chief software architect at Cake Solutions, whichplaces him at the center of all architectural decisions in all projects. Apartfrom the architecture, Jan is often called on to help with some of the mostcomplex and challenging areas of the implementation. Since joining Cake,Jan has proven his expertise in Java not only by taking on a wide variety ofcomplex projects but also through his collection of published works. In hisfree time, Jan is a keen cyclist and a member of the Manchester WheelersClub. He tries his best in various time trials and road races.

    Jan authored Chapters 14, 6, 9, 11, 14, 16, and 22.

    ALEKSA VUKOTIC is a software architect at Cake Solutions. He oversees thearchitecture as well as the implementation of most of Cakes projects. He hasextensive experience with most Java EE technologies, particularly Spring MVCand Security. He also has the knack, which means he can solve virtually anytechnical problem. He is an excellent tutor and is in charge of directing a teamof Cake Solutions developers, helping the team in solving the most complexproblems. As well as his interest in Java and .NET platforms, Aleksa enjoyssports, music, and nights out. Aleksa works with Jan on all of the major proj-ects at Cake Solutions.

    Aleksa authored Chapters 5, 8, 10, 15, 17, and 21.

    ANIRVAN CHAKRABORTY is a senior developer at Cake Solutions. His exten-sive experience with the Spring Framework and attention to detail puts himin charge of the implementation of some of the challenging aspects of CakeSolutionss projects. Anirvan takes great pride in his code and always makessure the code can be used as an example to others. When he does not havehis head buried in Java EE and Linux, he enjoys good food and drink withhis friends. He is also an ardent follower of the sport of cricket and enjoysreading detective novels.

    Anirvan authored Chapters 7, 13, 19, and 20.

    JESSICA DITT has been a developer at London-based Avenue A | Razorfishsince early 2008. Prior to that, she was part of the Cake Solutions team for2.5 years. She has been working on numerous enterprise-level projects,all of which were written using the Spring Framework and Spring WebFlow. Jessica has acquired significant expertise in efficient indexing usingLucene and has efficiently addressed Java EE application scalability issuesusing Gigaspaces. Out of the office, Jessica is a keen volleyball player andenjoys spending time in the gym.

    Jessica authored Chapters 12 and 18.

    xxiii

    9217ch00FM.qxd 7/25/08 5:58 PM Page xxiii

  • About the Technical Reviewer

    RICK EVANS is an independent contractor based in the UK with many yearsof experience working in the health, financial, and retail sectors. Over theyears, Rick has committed on a number of open source projects, includingSpring and Spring.NET. A polished teacher and mentor, Rick often speaksprofessionally about technology and delivers training on a wide range ofenterprise technologies and disciplines.

    xxiv

    9217ch00FM.qxd 7/25/08 5:58 PM Page xxiv

  • Acknowledgments

    When writing a book, a substantial amount of work goes on behind the scenes, and authors arebacked by an excellent team of editors, proofreaders, and technical reviewers. This book was noexception, and we would like to thank everyone who worked on the book. Our thanks goes to Rick,the technical reviewer, who has done enormous amounts of work to ensure that this book is of thehighest quality. The great editorial team at Apress also deserves our thanks: most importantly, SofiaMarchant; our editor, Tom Welsh; Heather Lang; Kelly Winquist; and many others. Without theirhelp, we would not have been able to complete this book. I would also like to thank Rob Harrop foragreeing to write the Foreword. Finally, we all wish to thank the managing director of Cake Solutions,Guy Remond; he gave some of Cake Solutionss time to us to work on the book.

    xxv

    9217ch00FM.qxd 7/25/08 5:58 PM Page xxv

  • 9217ch00FM.qxd 7/25/08 5:58 PM Page xxvi

  • Introduction

    Recently, the Java world has witnessed a dramatic shift away from so-called heavyweight archi-tectures such as Enterprise JavaBeans (EJB) toward lighter weight frameworks such as Spring. Complexand container-dependent services, such as CMP, and transaction management systems have beenreplaced with simpler alternatives such as Hibernate and aspect-oriented programming (AOP). Atthe core, Spring provides a comprehensive, lightweight container based on the principle of Inversionof Control (IoC), on which you can build your own applications. On top of this container, Springprovides a myriad of useful services, bringing together a large range of highly competent opensource projects into a single cohesive framework.

    The quality of the Spring Framework has seen it replacing traditional Java EE architectures inmany cases; as a result, more and more developers see the need for comprehensive Spring skills.Despite Spring having quite an extensive suite of documentation and examples, we feel that manydevelopers are still struggling to understand how to use Spring and, more importantly, how to use iteffectively. Because of this, we decided to write a new edition of Pro Spring.

    At first, we thought we would just update a few chapters and call it Pro Spring 2.5. However, wequickly realized that Spring 2.5 brought so many new features and improvements that, although wekept the old Pro Spring name, this is a completely new book.

    Through this book, you will learn how to use Spring to build better web and stand-alone appli-cations and how to sift through the many choices available to you through the framework. Our aimis to provide you with all the knowledge you need to use Spring effectively in your own applicationsand to give you insight into what is happening behind the scenes in Spring.

    For example, you will

    Learn the fundamentals of IoC in the context of AOP.

    Become aware of the seamlessness and power of Spring by referencing the easy-to-understandsample applications we provide.

    Learn how to replace common EJB features with Spring alternatives, including Springscomprehensive AOP-based transaction management framework.

    Effectively manage your Spring components and applications using Springs built-in JMXengine.

    Learn how to add scheduling to your Spring application with Quartz.

    After reading this book, you will be equipped with all the knowledge you need to build applicationseffectively using Spring and its related open source projects.

    xxvii

    9217ch00FM.qxd 7/25/08 5:58 PM Page xxvii

  • 9217ch00FM.qxd 7/25/08 5:58 PM Page xxviii

  • Getting Started with Spring

    In this part of the book, we will introduce the Spring Framework, starting with the steps needed to

    download the binary and source distributions. We will next explain the Inversion of Control (IoC)

    pattern and aspect-oriented programming (AOP). IoC and AOP lie at the heart of the Spring Frame-

    work. Toward the end of Part 1, we will show you, in full detail, various ways to configure

    Spring-managed beans. Finally, we will round off Part 1 with a look at some of the most important

    Spring design patterns.

    P A R T 1

    9217ch01FINAL.qxd 7/24/08 10:20 AM Page 1

  • 9217ch01FINAL.qxd 7/24/08 10:20 AM Page 2

  • Introducing Spring

    When we think of the community of Java developers, we are reminded of the hordes of gold rushprospectors of the late 1840s, frantically panning the rivers of North America looking for fragmentsof gold. As Java developers, our rivers run rife with open source projects, but, like the prospectors,finding a useful project can be time consuming and arduous. And yet, more and more developersare turning to open source tools and code. Open source brings innovative code and few restrictionson its usage, allowing developers to focus on the core of the applications they build.

    A common gripe about many open source Java projects is that they exist merely to fill a gap inthe implementation of the latest buzzword-heavy technology or pattern. Another problem is thatsome projects lose all their momentum: code that looked very promising in version 0.1 never reachesversion 0.2, much less 1.0. Having said that, many high-quality, user-friendly projects meet and addressa real need for real applications. In the course of this book, you will meet a carefully chosen subsetof these projects. You will get to know one in particular rather wellSpring.

    Spring has come a long way since the early code written by Rod Johnson in his book ExpertOne-to-One J2EE Design and Development (Wrox, October 2002). It has seen contributions from themost respected Java developers in the world and reached version 2.5.

    Throughout this book, you will see many applications of different open source technologies, allof which are unified under the Spring Framework. When working with Spring, an application devel-oper can use a large variety of open source tools, without needing to write reams of infrastructurecode and without coupling his application too closely to any particular tool. This chapter is a gentleintroduction to the Spring Framework. If you are already familiar with Spring, you might want to skipthis chapter and go straight to Chapter 2, which deals with the setup and introduces the Hello, Worldapplication in Spring.

    Our main aims in this book are to provide as comprehensive a reference to the Spring Frameworkas we can and, at the same time, give plenty of practical, application-focused advice without it seem-ing like a clone of the documentation. To help with this, we build a full application using Springthroughout the book to illustrate how to use Spring technologies.

    What Is Spring?The first thing we need to explain is the name Spring. We will use Spring in most of the text of thisbook, but we may not always mean the same thing. Sometimes, we will mean the Spring Frameworkand sometimes the Spring project. We believe the distinction will always be clear and that you willnot have any trouble understanding our meaning.

    The core of the Spring Framework is based on the principle of Inversion of Control (IoC). Appli-cations that follow the IoC principle use configuration that describes the dependencies between itscomponents. It is then up to the IoC framework to satisfy the configured dependencies. The inversionmeans that the application does not control its structure; it is up to the IoC framework to do that.

    3

    C H A P T E R 1

    9217ch01FINAL.qxd 7/24/08 10:20 AM Page 3

  • Consider an example where an instance of class Foo depends on an instance of class Bar to performsome kind of processing. Traditionally, Foo creates an instance of Bar using the new operator or obtainsone from some kind of factory class. Using the IoC technique, an instance of Bar (or a subclass) isprovided to Foo at runtime by some external process. This injection of dependencies at runtime hassometimes led to IoC being given the much more descriptive name dependency injection (DI). Theprecise nature of the dependencies managed by DI is discussed in Chapter 3.

    Springs DI implementation puts focus on loose coupling: the components of your applicationshould assume as little as possible about other components. The easiest way to achieve loose cou-pling in Java is to code to interfaces. Imagine your applications code as a system of components: ina web application, you will have components that handle the HTTP requests and then use the com-ponents that contain the business logic of the application. The business logic components, in turn,use the data access objects (DAOs) to persist the data to a database. The important concept is thateach component does not know what concrete implementation it is using; it only sees an interface.Because each component of the application is aware only of the interfaces of the other components,we can switch the implementation of the components (or entire groups or layers of components) with-out affecting the components that use the changed components. Springs DI core uses the informationfrom your applications configuration files to satisfy the dependencies between its components. Theeasiest way to allow Spring to set the dependencies is to follow the JavaBean naming conventionsin your components, but it is not a strict requirement (for a quick introduction to JavaBeans, go toChapter 3).

    When you use DI, you allow dependency configuration to be externalized from your code.JavaBeans provide a standard mechanism for creating Java resources that are configurable in a stan-dard way. In Chapter 3, you will see how Spring uses the JavaBean specification to form the core ofits DI configuration model; in fact, any Spring-managed resource is referred to as a bean. If you areunfamiliar with JavaBeans, take a look at the quick primer at the beginning of Chapter 3.

    Interfaces and DI are mutually beneficial. We are sure that everyone reading this book willagree that designing and coding an application to interfaces makes for a flexible application that ismuch more amenable to unit testing. But the complexity of writing code that manages the depend-encies between the components of an application designed using interfaces is quite high and placesan additional coding burden on developers. By using DI, you reduce the amount of extra code youneed for an interface-based design to almost zero. Likewise, by using interfaces, you can get the mostout of DI because your beans can utilize any interface implementation to satisfy their dependency.

    In the context of DI, Spring acts more like a container than a frameworkproviding instancesof your application classes with all the dependencies they needbut it does so in a much less intru-sive way than, say, the EJB container that allows you to create persistent entity beans. Most importantly,Spring will manage the dependencies between the components of your application automatically.All you have to do is create a configuration file that describes the dependencies; Spring will takecare of the rest. Using Spring for DI requires nothing more than following the JavaBeans namingconventions within your classes (a requirement that, as you will see in Chapter 4, you can bypassusing Springs method injection support)there are no special classes from which to inherit or pro-prietary naming schemes to follow. If anything, the only change you make in an application that usesDI is to expose more properties on your JavaBeans, thus allowing more dependencies to be injectedat runtime.

    Note A container builds the environment in which all other software components live. Spring is a container,because it creates the components of your application and the components are children of the container.

    A framework is a collection of components that you can use to build your applications. Spring is a framework,because it provides components to build common parts of applications, such as data access support, MVC support,and many others.

    CHAPTER 1 INTRODUCING SPRING4

    9217ch01FINAL.qxd 7/24/08 10:20 AM Page 4

  • Although we leave a full discussion of DI until Chapter 3, it is worth taking a look at the benefitsof using DI rather than a more traditional approach:

    Reduce glue code: One of the biggest plus points of DI is its ability to reduce dramatically theamount of code you have to write to glue the different components of your application together.Often, this code is trivialwhere creating a dependency involves simply creating a new instanceof a class. However, the glue code can get quite complex when you need to look up dependenciesin a JNDI repository or when the dependencies cannot be invoked directly, as is the case withremote resources. In these cases, DI can really simplify the glue code by providing automaticJNDI lookup and automatic proxying of remote resources.

    Externalize dependencies: You can externalize the configuration of dependencies, which allowsyou to reconfigure easily without needing to recompile your application. This gives you twointeresting benefits. First, as you will see in Chapter 4, DI in Spring gives you the ideal mecha-nism for externalizing all the configuration options of your application for free. Second, thisexternalization of dependencies makes it much simpler to swap one implementation ofa dependency for another. Consider the case where you have a DAO component that performsdata operations against a PostgreSQL database and you want to upgrade to Oracle. Using DI,you can simply reconfigure the appropriate dependency on your business objects to use theOracle implementation rather than the PostgreSQL one.

    Manage dependencies in a single place: In the traditional approach to dependency management,you create instances of your dependencies where they are neededwithin the dependentclass. Even worse, in typical large applications, you usually use a factory or locator to find thedependent components. That means that your code depends on the factory or locator as wellas the actual dependency. In all but the most trivial of applications, you will have dependenciesspread across the classes in your application, and changing them can prove problematic.When you use DI, all the information about dependencies is the responsibility of a single com-ponent (the Spring IoC container), making the management of dependencies much simplerand less error prone.

    Improve testability: When you design your classes for DI, you make it possible to replace depend-encies easily. This comes in especially handy when you are testing your application. Considera business object that performs some complex processing; for part of this, it uses a DAO objectto access data stored in a relational database. You are not interested in testing the DAO; yousimply want to test the business object with various sets of data. In a traditional approach, wherethe service object is responsible for obtaining an instance of the DAO itself, you have a hard timetesting this, because you are unable to replace the DAO implementation easily with a dummyimplementation that returns your test data. Instead, you need to make sure that your test data-base contains the correct data and uses the full DAO implementation for your tests. Using DI,you can create a mock implementation of the DAO that returns the test data, and then you canpass this to your service object for testing. This mechanism can be extended for testing any tierof your application and is especially useful for testing web components, where you can createfake implementations of HttpServletRequest and HttpServletResponse.

    Foster good application design: Designing for DI means, in general, designing against interfaces.A typical injection-oriented application is designed so that all major components are definedas interfaces, and then concrete implementations of these interfaces are created and wiredtogether using the DI container. This kind of design was possible in Java before the advent ofDI and DI-based containers such as Spring, but by using Spring, you get a whole host of DI fea-tures for free, and you can concentrate on building your application logic, not a framework tosupport it.

    CHAPTER 1 INTRODUCING SPRING 5

    9217ch01FINAL.qxd 7/24/08 10:20 AM Page 5

  • As you can see from this list, DI provides a lot of benefits, but it is not without its drawbackstoo. In particular, DI can make seeing just what implementation of a particular dependency is beinghooked into which objects difficult, especially for someone not intimately familiar with the code.Typically, this is only a problem when developers are inexperienced with DI; after becoming moreexperienced, developers find that the centralized view of an application given by Spring DI lets themsee the whole picture. For the most part, the massive benefits far outweigh this small drawback, butyou should consider this when planning your application.

    Beyond Dependency Injection The Spring core alone, with its advanced DI capabilities, is a worthy tool, but where Spring reallyexcels is in its myriad of additional features, all elegantly designed and built using the principlesof DI. Spring provides tools to help build every layer of an application, from helper application pro-gramming interfaces (APIs) for data access right through to advanced model-view-controller (MVC)capabilities. What is great about these features is that, althou


Recommended