pro spring 2 - download.e-bookshelf.de · springsource is the company behind spring, the de facto...

30
Jan Machacek, Aleksa Vukotic, Anirvan Chakraborty, and Jessica Ditt Pro Spring 2.5

Upload: others

Post on 17-Oct-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Pro Spring 2 - download.e-bookshelf.de · SpringSource is the company behind Spring, the de facto standard in enterprise Java. SpringSource is a leading provider of enterprise Java

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

Pro Spring 2.5

Page 2: Pro Spring 2 - download.e-bookshelf.de · SpringSource is the company behind Spring, the de facto standard in enterprise Java. SpringSource is a leading provider of enterprise Java

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 source–based 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 Sales–eBook 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.

Page 3: Pro Spring 2 - download.e-bookshelf.de · SpringSource is the company behind Spring, the de facto standard in enterprise Java. SpringSource is a leading provider of enterprise Java

To Marc, who still thinks that beans grow on trees—Jan

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 support—Anirvan

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

—Jessica

Page 4: Pro Spring 2 - download.e-bookshelf.de · SpringSource is the company behind Spring, the de facto standard in enterprise Java. SpringSource is a leading provider of enterprise Java

Contents at a Glance

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

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

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

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

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

PART 1 ■ ■ ■ Getting Started with Spring■CHAPTER 1 Introducing Spring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

■CHAPTER 2 Getting Started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13

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

■CHAPTER 4 Beyond the Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73

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

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

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

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

PART 2 ■ ■ ■ Data Access■CHAPTER 9 JDBC Support. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319

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

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

PART 3 ■ ■ ■ Enterprise Application Components■CHAPTER 12 Job Scheduling with Spring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445

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

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

iv

Page 5: Pro Spring 2 - download.e-bookshelf.de · SpringSource is the company behind Spring, the de facto standard in enterprise Java. SpringSource is a leading provider of enterprise Java

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

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

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

■CHAPTER 18 Spring Web Flow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 711

■CHAPTER 19 Spring and AJAX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 757

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

■CHAPTER 21 Testing with Spring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 793

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

■INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 857

v

Page 6: Pro Spring 2 - download.e-bookshelf.de · SpringSource is the company behind Spring, the de facto standard in enterprise Java. SpringSource is a leading provider of enterprise Java

Contents

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

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

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

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

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

PART 1 ■ ■ ■ Getting Started with Spring■CHAPTER 1 Introducing Spring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

What Is Spring? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

Beyond Dependency Injection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6

Aspect-Oriented Programming with Spring . . . . . . . . . . . . . . . . . . . . . . . . . . 6

Accessing Data in Spring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6

Simplifying and Integrating with Java EE . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7

Job Scheduling Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7

Mail Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7

Dynamic Languages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

Remoting Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8Managing Transactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

The Spring MVC Framework . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

Spring Web Flow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

AJAX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

Internationalization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

Simplified Exception Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9The Spring Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

Origins of Spring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

The Spring Community . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

Spring for Microsoft .NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

The Spring IDE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

The Spring Security (Formerly Acegi) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

Alternatives to Spring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

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

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12

vii

Page 7: Pro Spring 2 - download.e-bookshelf.de · SpringSource is the company behind Spring, the de facto standard in enterprise Java. SpringSource is a leading provider of enterprise Java

■CHAPTER 2 Getting Started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13

Obtaining the Spring Framework . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13

Checking Out Spring from CVS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13

Building Spring from Source Code. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14

Verifying Your Spring Distribution. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15

Spring Packaging. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15

Spring Dependencies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17

Spring Sample Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20

Setting Up Spring and Your IDE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21

Hello, World. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24

Putting Spring Into “Hello, World” . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27

Dependency Injection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28

The Impact of Spring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30

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

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

IoC and DI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31

Types of IoC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32

Contextualized Dependency Lookup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33

Constructor DI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35

Setter DI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36

Injection vs. Lookup. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37

Setter Injection vs. Constructor Injection. . . . . . . . . . . . . . . . . . . . . . . . . . . . 38

IoC in Spring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39

DI with Spring. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39

Beans and BeanFactories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40

BeanFactory Implementations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40

XML Bean Definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41

Using Constructor Injection. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43

Injection Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46

Understanding Bean Naming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56

Bean Instantiation Modes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59

Resolving Dependencies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62

Automatically Wiring Your Beans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65

Checking Dependencies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68

Bean Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70

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

■CONTENTSviii

Page 8: Pro Spring 2 - download.e-bookshelf.de · SpringSource is the company behind Spring, the de facto standard in enterprise Java. SpringSource is a leading provider of enterprise Java

■CHAPTER 4 Beyond the Basics. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73

Spring’s Impact on Application Portability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74

Bean Life Cycle Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74

Hooking into Bean Creation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75

Hooking into Bean Destruction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82

Making Your Beans Spring Aware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87

Using the BeanNameAware Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87

Using the BeanFactoryAware Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89

Using Method Injection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91

Lookup Method Injection. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91

Method Replacement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96

Using FactoryBean . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100

The MessageDigestFactoryBean . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100

Accessing a FactoryBean Directly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102

The BeanFactoryPostProcessor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103

Implementing a BeanFactoryPostProcessor . . . . . . . . . . . . . . . . . . . . . . . . 106

JavaBeans PropertyEditor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110

The Built-in PropertyEditors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110

Creating a Custom PropertyEditor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113

The BeanPostProcessor. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118

Implementing a BeanPostProcessor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120

When to Use BeanPostProcessor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124

The Spring ApplicationContext . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125

Implementations of ApplicationContext . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126

Using ApplicationContextAware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126

Controlling Bean Initialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127

Using Annotation-Based Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129

Internationalization with MessageSource . . . . . . . . . . . . . . . . . . . . . . . . . . 133

Using MessageSource in Stand-Alone Applications. . . . . . . . . . . . . . . . . . 141

The MessageSourceResolvable Interface . . . . . . . . . . . . . . . . . . . . . . . . . . 141

Using Application Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141

Considerations for Event Usage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143

Accessing Resources. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144

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

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

AOP Concepts. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148

Types of AOP. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149

Static AOP. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149

Dynamic AOP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149

Choosing an AOP Type. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149

■CONTENTS ix

Page 9: Pro Spring 2 - download.e-bookshelf.de · SpringSource is the company behind Spring, the de facto standard in enterprise Java. SpringSource is a leading provider of enterprise Java

AOP in Spring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149

The AOP Alliance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150

“Hello, World” in AOP. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150

Spring AOP Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152

The ProxyFactory Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153

Creating Advice in Spring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153

Advisors and Pointcuts in Spring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169

The Pointcut Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170

Using ComposablePointcut . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187

Pointcutting Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191

All About Proxies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191

Understanding Proxies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192

Using JDK Dynamic Proxies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192

Using CGLIB Proxies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192

Comparing Proxy Performance. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193

Which Proxy to Use? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196

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

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

@AspectJ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199

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

Pointcuts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203

Pointcut Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207

Exploring the Pointcut Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208

Using the @Pointcuts in XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211

Types of Advice . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212

Argument Binding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220

Introductions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221

The Aspect Life Cycle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227

Framework Services for AOP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228

Creating Our First Aspect Using the aop Namespace . . . . . . . . . . . . . . . . 229

Pointcuts in the aop Namespace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230

Creating Advice Using the aop Namespace . . . . . . . . . . . . . . . . . . . . . . . . 231

Introductions in the aop Namespace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237

Which Style Should You Use? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240

Working with Spring AOP Proxies. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241

Impact of Proxies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242

AspectJ Integration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246

Creating Your First AspectJ Aspect. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247

Compiling the Sample Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249

AspectJ’s Aspect Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251

■CONTENTSx

Page 10: Pro Spring 2 - download.e-bookshelf.de · SpringSource is the company behind Spring, the de facto standard in enterprise Java. SpringSource is a leading provider of enterprise Java

Load-Time Weaving . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251

Your First Load-Time Weaving Example . . . . . . . . . . . . . . . . . . . . . . . . . . . 252

LoadTimeWeaver Lookup Strategies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254

Practical Uses of AOP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254

Performance and Health Monitoring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255

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

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

Why the New Configuration? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259

Schemas Included in Spring 2.5. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262

The beans Schema . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262

The context Schema . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263

The util Schema . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263

The tx Schema . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266

The aop Schema . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266

The jee Schema . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267

The lang Schema . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267

Behind the Schema Scenes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268

Custom Schemas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270

IDE Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277

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

Directory Structure. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279

Simple Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279

Complex Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281

Packaging and Naming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281

Design Patterns Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282

Programming Against Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282

Creational Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283

Structural Patterns. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287

Behavioral Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290

Template Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 292

Spring Application Patterns. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293

Layered Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 294

High-Performance Paging. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295

Multiple Error Reporting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298

User Interface Transactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301

Background Processes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 304

E-mail Notifications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309

Error Collecting and Logging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311

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

■CONTENTS xi

Page 11: Pro Spring 2 - download.e-bookshelf.de · SpringSource is the company behind Spring, the de facto standard in enterprise Java. SpringSource is a leading provider of enterprise Java

PART 2 ■ ■ ■ Data Access■CHAPTER 9 JDBC Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319

Key JDBC Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319

Using the DriverManager and Connections. . . . . . . . . . . . . . . . . . . . . . . . . 322

Using PreparedStatements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323

Using CallableStatements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 326

Other JDBC Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327

Concepts in Spring Data Access Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327

JDBC Data Access Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 328

Using the JdbcTemplate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329

JdbcTemplate.execute . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 330

JdbcTemplate.query and Friends . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 334

JdbcTemplate.update . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339

JdbcTemplate.batchUpdate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 340

RdbmsOperation Subclasses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 342

SqlUpdate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343

BatchSqlUpdate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 348

SqlCall and StoredProcedure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349

SqlQuery and Its Subclasses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351

JdbcTemplate or RdbmsOperation?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 358

Large Binary Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 358

JdbcDaoSupport. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 361

Simple Spring JDBC. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 364

SimpleJdbcTemplate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 365

SimpleJdbcCall . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 367

SimpleJdbcInsert . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 369

SimpleJdbcDaoSupport. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 370

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

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

What Is iBATIS? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373

iBATIS Versions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 374

Infrastructure and Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 374

Mapping Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 375

sqlMap Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 376

Configuring iBATIS and Spring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 378

■CONTENTSxii

Page 12: Pro Spring 2 - download.e-bookshelf.de · SpringSource is the company behind Spring, the de facto standard in enterprise Java. SpringSource is a leading provider of enterprise Java

Selecting Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 380

Simple Selects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 380

One-to-One Selects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 384

One-to-Many Selects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 388

Many-to-Many Selects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 390

Updating Data. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 391

Deleting Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393

Inserting Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 394

What’s Missing from iBATIS? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 397

Overall Performance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 397

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 398

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

Hibernate Primer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 399

Packaging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 400

Introduction to Hibernate Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 401

Using Hibernate Sessions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 403

Using HibernateDaoSupport . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 407

Deciding Between HibernateTemplate and Session. . . . . . . . . . . . . . . . . . 409

Using Hibernate in Enterprise Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 413

Preventing Stale Data Updates. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 413

Object Equality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 416

Transactional Behavior . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 419

Lazy Loading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 424

Dealing with Large Data Sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 435

Handling Large Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 437

Combining Hibernate with Other DAO Code . . . . . . . . . . . . . . . . . . . . . . . . 441

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

PART 3 ■ ■ ■ Enterprise Application Components■CHAPTER 12 Job Scheduling with Spring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445

Scheduling Jobs Using JDK Timer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 446

Trigger Types with Timer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 446

Creating a Simple Job . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 447

Spring Support for JDK Timer Scheduling. . . . . . . . . . . . . . . . . . . . . . . . . . 449

Scheduling Jobs Using OpenSymphony Quartz . . . . . . . . . . . . . . . . . . . . . . . . . . 455

Introducing Quartz . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 455

Quartz Support in Spring. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 464

■CONTENTS xiii

Page 13: Pro Spring 2 - download.e-bookshelf.de · SpringSource is the company behind Spring, the de facto standard in enterprise Java. SpringSource is a leading provider of enterprise Java

Job Scheduling Considerations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 470

Choosing a Scheduler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 470

Packaging Job Logic Separately from the Job Class . . . . . . . . . . . . . . . . . 470

Task Execution and Thread Pooling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 471

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

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

The Spring Mail API Structure. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 479

Configuring Mail Settings Using Spring . . . . . . . . . . . . . . . . . . . . . . . . . . . . 479

Sending Simple E-mails . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 479

Constructing and Sending E-mail Programmatically . . . . . . . . . . . . . . . . . 479

Constructing E-mail Declaratively . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 482

Constructing and Sending MIME Messages . . . . . . . . . . . . . . . . . . . . . . . . 485

Insight into Enterprise-Level E-mail Handling. . . . . . . . . . . . . . . . . . . . . . . 500

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

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

Supported Languages Primer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 511

BeanShell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 511

Groovy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 513

JRuby . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 514

Using Dynamic Languages As Spring Beans. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 515

Behind the Scenes of Dynamic Language Support . . . . . . . . . . . . . . . . . . 517

Proxying Dynamic Language Beans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 518

Performance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 519

Refreshable Beans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 521

BeanShell Beans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 523

JRuby Beans. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 525

Groovy Beans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 526

Typical Usage for Dynamic Languages in Spring Applications . . . . . . . . . . . . . . 526

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 530

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

Spring Remoting Architecture. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 535

Remote Method Invocation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 535

Exposing Arbitrary Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 536

Accessing an RMI Service Using Proxies . . . . . . . . . . . . . . . . . . . . . . . . . . 538

Exposing CORBA Services. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 540

Accessing a CORBA Service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 543

■CONTENTSxiv

Page 14: Pro Spring 2 - download.e-bookshelf.de · SpringSource is the company behind Spring, the de facto standard in enterprise Java. SpringSource is a leading provider of enterprise Java

Web Services with JAX-RPC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 544

Introducing Apache Axis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 545

Creating a Web Service with ServletEndpointSupport . . . . . . . . . . . . . . . . 545

Accessing RPC-Style Web Services using Proxies . . . . . . . . . . . . . . . . . . . 549

Working with JavaBeans in Axis Services. . . . . . . . . . . . . . . . . . . . . . . . . . 552

Using JAX-WS Web Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 556

Exposing Web Services Using SimpleJaxWsServiceExporter . . . . . . . . . . 556

Exposing a Web Service Using XFire . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 557

Accessing JAX-WS Web Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 559

Accessing Java Web Services from Other Clients . . . . . . . . . . . . . . . . . . . 560

Creating Web Services with HTTP Invoker . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 563

Exposing Simple Services. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 564

Accessing an HTTP Invoker Service Using Proxies . . . . . . . . . . . . . . . . . . 567

Using Arbitrary Objects in HTTP Invoker Services . . . . . . . . . . . . . . . . . . . 567

Using HTTP Basic Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 569

Choosing a Remoting Architecture. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 573

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 574

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

Exploring the Spring Transaction Abstraction Layer . . . . . . . . . . . . . . . . . . . . . . . 575

Analyzing Transaction Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 576

Exploring the TransactionDefinition Interface . . . . . . . . . . . . . . . . . . . . . . . 576

Using the TransactionStatus Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 578

Implementations of the PlatformTransactionManager . . . . . . . . . . . . . . . . 579

Exploring a Transaction Management Sample . . . . . . . . . . . . . . . . . . . . . . . . . . . 579

Nontransactional Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 580

Programmatic Transaction Management. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 588

Using the TransactionTemplate Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 590

Programmatic Transaction Management Summary. . . . . . . . . . . . . . . . . . 591

Declarative Transaction Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 591

Using the TransactionProxyFactoryBean . . . . . . . . . . . . . . . . . . . . . . . . . . . 591

Implications of Using Proxies in Transaction Management . . . . . . . . . . . . 593

AOP Transaction Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 594

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

Using XML AOP Transaction Management . . . . . . . . . . . . . . . . . . . . . . . . . 596

Working with Transactions Over Multiple Transactional Resources . . . . . . . . . . 599

Implementing Your Own Transaction Synchronization . . . . . . . . . . . . . . . . . . . . . 601

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 610

■CONTENTS xv

Page 15: Pro Spring 2 - download.e-bookshelf.de · SpringSource is the company behind Spring, the de facto standard in enterprise Java. SpringSource is a leading provider of enterprise Java

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

MVC Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 611

Spring MVC. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 613

MVC Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 613

Using Handler Mappings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 614

Spring Controllers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 616

AbstractController . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 616

ParameterizableViewController . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 617

MultiActionController . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 618

Interceptors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 621

Views, Locales, and Themes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 622

Using Views Programmatically . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 622

Using View Resolvers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 625

Using Localized Messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 629

Using Locales . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 630

Using Themes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 630

Command Controllers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 633

Using Form Controllers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 633

Exploring the AbstractWizardFormController . . . . . . . . . . . . . . . . . . . . . . . 640

File Upload . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 644

Handling Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 647

Spring and Other Web Technologies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 650

Using JSP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 651

Using Velocity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 669

FreeMarker . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 674

Using XSLT Views. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 678

Using PDF Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 680

Using Excel Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 682

Using Tiles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 684

JasperReports . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 696

Spring Conventions Over Configuration. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 701

Controller Conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 701

MultiActionController Conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 702

Model Conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 703

View Conventions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 704

Using Annotations for Controller Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . 705

@Controller. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 705

@RequestMapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 706

@RequestParam . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 707

@ModelAttribute . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 707

Using Annotations with the Command Controller . . . . . . . . . . . . . . . . . . . . 708

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

■CONTENTSxvi

Page 16: Pro Spring 2 - download.e-bookshelf.de · SpringSource is the company behind Spring, the de facto standard in enterprise Java. SpringSource is a leading provider of enterprise Java

■CHAPTER 18 Spring Web Flow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 711

Introducing Spring Web Flow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 712

Core Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 713

Obtaining Spring Web Flow. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 716

Spring Web Flow Dependencies. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 718

Hello, Web Flow!. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 719

Exploring States . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 723

View State. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 724

Decision State . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 724

End State . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 725

Working with Transitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 725

Advanced Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 727

Expression Languages and Scopes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 727

Implementing Actions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 731

Model Data Binding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 732

Partial Rendering of Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 736

Mapping Flow Input and Output Parameters . . . . . . . . . . . . . . . . . . . . . . . 736

Using Subflows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 737

Spring Web Flow Behind the Scenes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 738

Flow Execution Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 739

Flow Executor. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 741

Flow Definition Registry . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 742

Flow Execution Repository . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 745

Integration with Spring MVC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 746

Flow Handling. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 746

View Resolving . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 747

Securing Flows with Spring Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 747

Step 1: Adding the SecurityFlowExecutionListener . . . . . . . . . . . . . . . . . . 748

Step 2: Basic Authentication and Authorization Handling . . . . . . . . . . . . . 748

Step 3: Defining Security Rules in Flow Definitions . . . . . . . . . . . . . . . . . . 750

Problem Solver . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 752

Stateful Navigational Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 753

Browser Navigation Bar Support and Double Submit . . . . . . . . . . . . . . . . 753

Testing Flow Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 753

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 754

■CONTENTS xvii

Page 17: Pro Spring 2 - download.e-bookshelf.de · SpringSource is the company behind Spring, the de facto standard in enterprise Java. SpringSource is a leading provider of enterprise Java

■CHAPTER 19 Spring and AJAX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 757

DWR. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 758

Installing DWR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 758

Spring Configuration for DWR. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 758

About the Complete Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 760

Testing the DWR Configuration. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 764

Running the Complete Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 765

DWR Scripting Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 766

Using Simple Callback Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 766

Calling Metadata Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 767

engine.js . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 767

Call Batching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 767

Call Ordering. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 768

Handling Errors and Warnings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 768

util.js . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 768

Security in DWR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 768

Advantages and Disadvantages of DWR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 770

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 770

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

JMX Refresher . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 771

Exposing Your Beans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 772

MBeanExporter. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 772

MBeanServerFactoryBean . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 774

Exposing Your Beans in an Existing MBean Server . . . . . . . . . . . . . . . . . . 775

Bean Registration Behavior . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 776

Controlling Object Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 776

Controlling the Management Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 777

MBeanInfoAssembler Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 777

MethodNameBasedMBeanInfoAssembler Interface . . . . . . . . . . . . . . . . . . 777

Using Java Interfaces to Control the Management Interface . . . . . . . . . . 780

Using Source-Level Metadata . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 782

Remoting with Spring JMX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 785

Exposing Remote MBeans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 785

Accessing Remote MBeans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 786

Proxying MBeans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 786

Notifications in Spring JMX. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 787

Notification Listeners. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 787

Publishing Notifications. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 788

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

■CONTENTSxviii

Page 18: Pro Spring 2 - download.e-bookshelf.de · SpringSource is the company behind Spring, the de facto standard in enterprise Java. SpringSource is a leading provider of enterprise Java

■CHAPTER 21 Testing with Spring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 793

Unit Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 793

Unit Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 795

Unit Testing the Web Layer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 798

Integration Tests. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 800

Using AbstractSpringContextTests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 807

Using AbstractDependencyInjectionSpringContextTests . . . . . . . . . . . . . . 809

Using AbstractTransactionalSpringContextTests. . . . . . . . . . . . . . . . . . . . . 811

Using AbstractAnnotationAwareTransactionalTests . . . . . . . . . . . . . . . . . . 813

JNDI. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 817

Spring TestContext Framework. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 819

Application Context and DI with the TestContext Framework . . . . . . . . . . 820

Transactions in the TestContext Framework . . . . . . . . . . . . . . . . . . . . . . . . 822

Support Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 824

Test Coverage. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 826

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 827

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

Performance vs. Responsiveness. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 829

Exploring Enterprise Application Performance Issues . . . . . . . . . . . . . . . . . . . . . 830

Measuring Java EE Application Performance . . . . . . . . . . . . . . . . . . . . . . . . . . . . 830

Finding Out What to Measure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 830

Determining the Necessary Data Sets. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 837

Improving the Data Access Tier . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 837

Improving Transaction Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 847

Controlling the Performance of Remote Calls . . . . . . . . . . . . . . . . . . . . . . . 849

Understanding View Performance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 849

Using Caching. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 849

Performance Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 851

Monitoring Application Health and Performance . . . . . . . . . . . . . . . . . . . . . . . . . 853

More Resources on Performance Tuning. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 855

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 855

■INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 857

■CONTENTS xix

Page 19: Pro Spring 2 - download.e-bookshelf.de · SpringSource is the company behind Spring, the de facto standard in enterprise Java. SpringSource is a leading provider of enterprise Java

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 didn’t relinquish the reins lightly. When Juergen and I setout working on Spring Framework 2.0, I knew that I wouldn’t 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 scratch—andit 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

Page 20: Pro Spring 2 - download.e-bookshelf.de · SpringSource is the company behind Spring, the de facto standard in enterprise Java. SpringSource is a leading provider of enterprise Java

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 Wheelers’Club. He tries his best in various time trials and road races.

Jan authored Chapters 1–4, 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 Cake’s 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 CakeSolutions’s 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

Page 21: Pro Spring 2 - download.e-bookshelf.de · SpringSource is the company behind Spring, the de facto standard in enterprise Java. SpringSource is a leading provider of enterprise Java

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

Page 22: Pro Spring 2 - download.e-bookshelf.de · SpringSource is the company behind Spring, the de facto standard in enterprise Java. SpringSource is a leading provider of enterprise Java

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 Solutions’s time to us to work on the book.

xxv

Page 23: Pro Spring 2 - download.e-bookshelf.de · SpringSource is the company behind Spring, the de facto standard in enterprise Java. SpringSource is a leading provider of enterprise Java

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 Spring’scomprehensive AOP-based transaction management framework.

• Effectively manage your Spring components and applications using Spring’s 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

Page 24: Pro Spring 2 - download.e-bookshelf.de · SpringSource is the company behind Spring, the de facto standard in enterprise Java. SpringSource is a leading provider of enterprise Java

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

Page 25: Pro Spring 2 - download.e-bookshelf.de · SpringSource is the company behind Spring, the de facto standard in enterprise Java. SpringSource is a leading provider of enterprise Java

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 well—Spring.

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, World”application 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 “inversion”means 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

Page 26: Pro Spring 2 - download.e-bookshelf.de · SpringSource is the company behind Spring, the de facto standard in enterprise Java. SpringSource is a leading provider of enterprise Java

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.

Spring’s 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 application’s 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. Spring’s DI core uses the informationfrom your application’s 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 framework—providing instancesof your application classes with all the dependencies they need—but 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 Spring’s 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

Page 27: Pro Spring 2 - download.e-bookshelf.de · SpringSource is the company behind Spring, the de facto standard in enterprise Java. SpringSource is a leading provider of enterprise Java

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 trivial—where 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 needed—within 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

Page 28: Pro Spring 2 - download.e-bookshelf.de · SpringSource is the company behind Spring, the de facto standard in enterprise Java. SpringSource is a leading provider of enterprise Java

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, although Spring often provides its own approach,you can easily integrate them with other tools, making them all first-class members of the Springfamily.

Aspect-Oriented Programming with Spring Aspect-oriented programming (AOP) is one of the technologies of the moment in the programmingspace. AOP lets you implement crosscutting logic—that is, logic that applies to many parts of yourapplication—in a single place, and then have that logic automatically applied right across the appli-cation. AOP is enjoying an immense amount of time in the limelight at the moment; however, behindall the hype is a truly useful technology that has a place in any Java developer’s toolbox.

There are two main kinds of AOP implementation. Static AOP, such as AspectJ (www.aspectj.org),provides a compile-time solution for building AOP-based logic and adding it to an application.Dynamic AOP, such as that in Spring, allows crosscutting logic to be applied arbitrarily to any othercode at runtime. Finally, in Spring 2.5, you can use load-time dynamic weaving, which applies thecrosscutting logic when the class loader loads the class. Both kinds of AOP have their places, andindeed, Spring provides features to integrate with AspectJ. This is covered in more detail in Chapters 5and 6.

There are many applications for AOP. The typical one given in many traditional examplesinvolves performing some kind of tracing, but AOP has found many more ambitious uses, evenwithin the Spring Framework itself, particularly in transaction management. Spring AOP is coveredin depth in Chapters 5–7, where we show you typical uses of AOP within the Spring Framework andyour own application. We also look into the issue of performance and consider some areas wheretraditional technologies can work better than AOP.

Accessing Data in Spring Data access and persistence seem to be the most often discussed topics in the Java world. It seemsthat you cannot visit a community site such as www.theserverside.com without being bombardedwith articles and blog entries describing the latest, greatest data access tool.

Spring provides excellent integration with a choice selection of these data access tools. More-over, Spring makes the use of JDBC, a viable option for many projects thanks to its simplifiedwrapper APIs around the standard JDBC API. As of Spring version 1.1, you have support for JDBC,Hibernate, iBATIS, and Java Data Objects (JDO).

The JDBC support in Spring makes building an application on top of JDBC realistic, even forcomplex applications. The support for Hibernate, iBATIS, and JDO makes their already simple APIseven simpler, thus easing the burden on developers. When using the Spring APIs to access data via

CHAPTER 1 ■ INTRODUCING SPRING6

Page 29: Pro Spring 2 - download.e-bookshelf.de · SpringSource is the company behind Spring, the de facto standard in enterprise Java. SpringSource is a leading provider of enterprise Java

any tool, you can take advantage of Spring’s excellent transaction support. A full discussion of thissupport can be found in Chapter 15.

One of Spring’s nicest features is the ability to mix and match data access technologies easilywithin an application. For instance, you may be running an application with Oracle, using Hibernatefor much of your data access logic. However, if you want to take advantage of Oracle-specific features,it is simple to implement that particular part of your data access tier using Spring’s JDBC APIs.

Simplifying and Integrating with Java EEThere has been a lot of discussion recently about the complexity of various Java EE APIs, especiallythose of EJB. It is evident from the EJB 3.0 specification that this discussion has been taken on boardby the expert group, and EJB 3.0 brought some simplifications and many new features. Even withthe simplifications over EJB 2.0, using Spring’s simplified support for many Java EE technologies is stillmore convenient. For instance, Spring provides a selection of classes for building and accessingEJB resources. These classes cut out a lot of the grunt work from both tasks and provide a moreDI-oriented API for EJBs.

For any resources stored in a JNDI-accessible location, Spring allows you to do away with thecomplex lookup code and have JNDI-managed resources injected as dependencies into otherobjects at runtime. As a side effect of this, your application code becomes decoupled from JNDI,giving you more scope for code reuse in the future.

As of version 1.0.2, Spring does not support JMS access. However, the CVS repository alreadycontains a large array of classes that are to be introduced in 1.1. Using these classes simplifies allinteraction with Java Message Service (JMS) destinations and should reduce a lot of the boilerplatecode you need to write in order to use JMS from your Spring applications.

Chapters 11–13 explain how Spring works with the most important Java EE application compo-nents; Chapter 14 addresses application integration issues; and Chapter 20 deals with managementof Java EE applications.

Job Scheduling Support Many advanced applications require some kind of scheduling capability. Whether for sendingupdates to customers or doing housekeeping tasks, the ability to schedule code to run at a prede-fined time is an invaluable tool for developers.

Spring supports two scheduling mechanisms: one uses the Timer class, which has been availablesince Java 1.3; and the other uses the Quartz scheduling engine. Scheduling based on the Timer classis quite primitive and is limited to fixed periods defined in milliseconds. With Quartz, on the otherhand, you can build complex schedules using the Unix cron format to define when tasks should be run.

Spring’s scheduling support is covered in full in Chapter 11.

Mail Support Sending e-mail is a typical requirement for many different kinds of applications and is given first-classtreatment within the Spring Framework. Spring provides a simplified API for sending e-mail messagesthat fits nicely with its DI capabilities. It supports pluggable implementations of the mail API andcomes complete with two implementations: one uses JavaMail, and the other uses Jason Hunter’sMailMessage class from the com.oreilly.servlet package available from http://servlets.com/cos.

Spring lets you create a prototype message in the DI container and use this as the base for allmessages sent from your application. This allows for easy customization of mail parameters such asthe subject and sender address. However, there is no support for customizing the message bodyoutside of the code. In Chapter 12, we look at Spring’s mail support in detail and discuss a solutionthat combines templating engines such as Velocity and FreeMarker and Spring, allowing mail con-tent to be externalized from the Java code.

CHAPTER 1 ■ INTRODUCING SPRING 7

Page 30: Pro Spring 2 - download.e-bookshelf.de · SpringSource is the company behind Spring, the de facto standard in enterprise Java. SpringSource is a leading provider of enterprise Java

In addition to simple mail-sending code, we will show how to use Spring events to implementfully asynchronous messaging infrastructure. We will make use of the Spring Java Management Exten-sions (JMX) features to show how to create an efficient management console for the mail queues.

Dynamic LanguagesDynamic languages in Spring allow you to implement components of your application in languagesother than Java (Spring 2.5 supports BeanShell, JRuby, and Groovy). This allows you to externalizepart of your application’s code so that it can be easily updated by administrators and power users.You could do this even without Spring, but the support built into Spring means that the rest of yourapplication will not be aware that a component is implemented in another language; it will appearto be an ordinary Spring bean.

Many large applications have to deal with complex business processes. This would not be too dif-ficult to handle in Java, but in most cases, these processes change over time, and users want to be ableto make the changes themselves. This is where a domain-specific language implementation comes in.

In Chapter 14, you will see how to use the dynamic language support in Spring 2.5 and how touse it to implement a simple domain-specific language.

Remoting Support Accessing or exposing remote components in Java has never been simple. Using Spring, you cantake advantage of extensive support for a wide range of remoting techniques that help you exposeand access remote services quickly.

Spring supports a variety of remote access mechanisms, including Java RMI, JAX-RPC, CauchoHessian, and Caucho Burlap. In addition to these remoting protocols, Spring 1.1 introduced its ownHTTP-based protocol that is based on standard Java serialization. By applying Spring’s dynamicproxying capabilities, you can have a proxy to a remote resource injected as a dependency into oneof your components, thus removing the need to couple your application to a specific remotingimplementation and also reducing the amount of code you need to write for your application.

As well as making it easy to access remote components, Spring provides excellent support forexposing a Spring-managed resource as a remote service. This lets you export your service usingany of the remoting mechanisms mentioned earlier, without needing any implementation-specificcode in your application.

Integrating applications written in different programming languages, and possibly running ondifferent platforms, is one of the most compelling reasons for using remote services. In Chapter 14,we will show how to use remoting between Java applications and a C# Windows rich client applica-tion making full use of a Spring service running on a Unix system.

Managing TransactionsSpring provides an excellent abstraction layer for transaction management, allowing for program-matic and declarative transaction control. By using the Spring abstraction layer for transactions,you can easily change the underlying transaction protocol and resource managers. You can startwith simple, local, resource-specific transactions and move to global, multiresource transactionswithout having to change your code. Transactions are covered in full detail in Chapter 15.

The Spring MVC FrameworkAlthough Spring can be used in almost any application, from a service-only application, throughweb and rich-client ones, it provides a rich array of classes for creating web-based applications.Using Spring, you have maximum flexibility when you are choosing how to implement your webfront end.

CHAPTER 1 ■ INTRODUCING SPRING8