Table Of Contentcyan yelloW
MaGenTa Black
panTone 123 c
Books for professionals By professionals® The eXperT’s Voice® in open source
Companion
CCoovveerrss
eBook Available
Spring Recipes: A Problem-Solution Approach SSpprriinngg 22..55
aanndd eeaarrlliieerr vveerrssiioonnss
Dear Reader, S
Spring ii���� ������eee ��������iiii����pppp����eeee������������ aaaaaannnnnndddddd ����������������������������������� ppppppp��������������eeeeeeerrrrrrr��������������������� �������aaaaaaa�������aaaaaaa™ EE app�ica�i�n �ra�e��rk I
�a�e e�er ���ed,, aanndd SSpprriinngg �������� ii��pprr����ee���� ������ii���� ����rr����eerr ��nn pprree��ii�������� rree��eeaa����ee������� ����rrrr��������
Spring
In�er��i�n �� C�n�r�� and AOP �� per��i���ence, �ran��ac�i�n��, MVC, �e���ing, and p
��re, ��i�� b��k ������� y�� ��� �� �ake ��e ����� �� Spring ��r��g� de�ai�ed c�de
exa�p�e��� Typica� recipe�� range �r�� ��canning c��p�nen��� �r�� ��e c�a����pa��,
r
�ni� �e���ing Spring MVC c�n�r���er��, �� ��ec�ring �eb �������
Spring i�� n�� ���������� aaannn aaapppppp���iiicccaaa���iii���nnn ����rrrraaaa����eeeee�����������rrrrrrkkkkkk,,,,,,, bbbbbbb�������������� aaaaaaa���������������������������� aaaaaaa ppppppp�������aaaaaaa���������������������rrrrrrr������� ��������������aaaaaaa������� ��������nnnnnnnnddddddddeeeeeeeerrrrrrrr��������
i
�ie�� aa ������������eee ggggrrrr��������pppp �������� pppprrrr��������eeeecccc���������������� Spring Recipes c��er�� a������ a�� ��e ��d��e�� ��
��e Spring �ra�e��rk aanndd ������eee���eeerrraaa��� ccc������������nnn SSSppprrriiinnnggg ppprrr������eeeccc���������,,, iiinnnccc������dddiiinnnggg SSSppprrriiinnnggg n
Sec�ri�y, Spring Web ����, and Spring Web Ser�ice��� I c����e ����������eee ������pppiiiccc������ ccccaaaarrrreeee����
����y, bbeeccaa������ee II bbee��iiee��ee ����eeyy aaarrreee ������eee ��������������� ���aaa������aaabbb���eee aaaddddddiii���iii���nnn������ ������ SSSppprrriiinnnggg��������� ccc���rrreee
��nc�i�na�i�y� g
Spring pprr����iiddee���� ����ii��pp��ee ������������ii��nn���� ���� dddiii������iiiccc��������� ppprrr���bbb���eee�����������,,,, aaaannnndddd ������������iiiiiii�������ppppppp�������iiiiiiiccccccciiiiiii�������yyyyyyy ��������aaaaaaaa���������������� Recipes
�y �ain g�a� iinn ddee����iiggnniinngg ����ii���� bb����kk�� II aaaiii���eeeddd ������ ���������rrriiikkkeee aaaaa bbbbbbaaaaaa������aaaaaannnnnncccccceeeeee bbbbbbeeeeee������������eeeeeeeeeeeennnnnn
bread�� �� ����cc��ppee,, dddeeeppp������ ������ dddeee���aaaiii���,,,, rrrrraaaaapppppiiiiiddddd �����eeeeeaaaaarrrrrnnnnniiiiinnnnnggggg,,,,,, aaaaaannnnnndddddd ppppppprrrrrrreeeeeeerrrrrrreeeeeee��������������iiiiiii��������������iiiiiii�������eeeeeee ����������������kkkkkkkkiiiiiiii���������������������������������������� RRRRRRRRReeeeeeeeeaaaaaaaaadddddddddiiiiiiiiinnnnnnnnnggggggggg
Spring Recipes ��ii���� ��ee��pp yy���� ���� ��aa������eerr ����ee ���������� ii��pp��rr��aann�� ��������� ppppeeeerrrrcccceeeennnn���� ���������� SSSSSppppprrrrriiiiinnnnnggggg������������������
R
�ea��re�� ��ii���� �������������������� ��������������� ppppppeeeeeerrrrrrcccccceeeeeennnnnn������ �������������� ����������������eeeeeeee eeeeeeee������������������������rrrrrrrr�������� yyyyyyyy���������������� ��������������������������������dddddddd nnnnnnnneeeeeeeeeeeeeeeedddddddd ���������������� ��������eeeeeeeeaaaaaaaarrrrrrrrnnnnnnnn ����������������eeeeeeee�������� aaaaaaaa������������������������
Eac� c�ap�er ������rr����gg����yy eexxpp����rree���� aaannnn iiii����pppp����rrrr����aaaannnn���� ����������pppppiiiiiccccc ������������rrrrrr������������gggggg������ aaaaaaa ccccccc��������������ppppppp�������eeeeeee�������eeeeeee
e
rea����r�d exa�p�e ��a� y�� can ������ ���ep bbbyyy ���������������eeeeeppppp����� WWWWW�����eeeeennnnn yyyyy���������� rrrrreeeeeaaaaaddddd aaaaa ccccc�����aaaaappppp�����eeeeerrrrr
��r ��e �ir��� �i�e, I rec���end ����aa�� yyy������ ������������������ ������eee eeexxxaaa���ppp���eeee�������� ������������eeeeee������������eeeeeennnnnn������iiiiiiaaaaaa������������yyyyyy������� OOOOOOOnnnnnnnccccccceeeeeee
c
y�� �a�e g�ne ��r��g� a c�ap�er, y�� can ���e i� a�� a re�erence by ���king �p ��e
pr�b�e���������������������iii���nnn����bbbbbaaaaa����������eeeeeddddd rrrrreeeeeccccciiiiipppppeeeee��������������� OOOOOOrrrrrr,,,,,, iiiiii������ yyyyyy������������������rrrrrreeeeee ������������������kkkkkkiiiiiinnnnnngggggg ������������ ������������������������������eeeeee aaaaaa ������������ppppppeeeeeecccccciiiiii������iiiiiicccccc pppppprrrrrr������bbbbbb������ i A Problem-Solution Approach
�e�, y�� can z��� in �n ��e recipe�� y�� need by g�ancing a� ��e �ab�e �� c�n�
p
�en��� �r ��e index�
En��y �earning and ���ing Spring! e
Gary Mak
s
Learn to use the full power of Spring 2.5 through coding recipes!
Companion eBook THE APRESS ROADMAP
The Definitive Guide
to GigaSpaces
Pro Java™ EE
Spring Patterns
Beginning Spring 2
See last page for details
Spring Recipes
on $10 eBook version
Pro Spring 2.5
Gary Mak
SOURCE CODE ONLINE ISBN-13: 978-1-59059-979-2
www.apress.com ISBN-10: 1-59059-979-9
M
54999
a Technical review by Sam Brannen of SpringSource and Kris Lander
k
US $49.99
Shelve in
Java Programming
User level:
9 781590 599792
Intermediate–Advanced
this print for content only—size & color not accurate spine = 1.413" 752 page count
9799FM.qxd 5/27/08 3:47 PM Page i
Spring Recipes
A Problem-Solution Approach
Gary Mak
9799FM.qxd 5/27/08 3:47 PM Page ii
Spring Recipes: A Problem-Solution Approach
Copyright © 2008 by Gary Mak
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 retrieval
system, without the prior written permission of the copyright owner and the publisher.
ISBN-13 (pbk): 978-1-59059-979-2
ISBN-10 (pbk): 1-59059-979-9
ISBN-13 (electronic): 978-1-4302-0624-8
ISBN-10 (electronic): 1-4302-0624-1
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 occurrence
of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark
owner, with no intention of infringement of the trademark.
Java™ and all Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc., in
theUS and other countries. Apress, Inc., is not affiliated with Sun Microsystems, Inc., and this book was
written without endorsement from Sun Microsystems, Inc.
Lead Editors: Steve Anglin, Tom Welsh
Technical Reviewers: Sam Brannen, Kris Lander
Editorial Board: Clay Andres, Steve Anglin, Ewan Buckingham, Tony Campbell, Gary Cornell,
JonathanGennick, Matthew Moodie,Joseph Ottinger, Jeffrey Pepper, Frank Pohlmann,
Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh
Project Manager: Kylie Johnston
CopyEditor: Damon Larson
Associate Production Director: Kari Brooks-Copony
Production Editor: Laura Esterman
Compositor: Dina Quan
Proofreader:April Eddy
Indexer: Ron Strauss
Artist: April Milne
Cover Designer: Kurt Krames
Manufacturing 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 orders-ny@springer-sbm.com,or
visit 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 info@apress.com,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 Special
Bulk 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 precau-
tion has been taken in the preparation of this work, neither the author(s) nor Apress shall have any
liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly
or indirectly by the information contained in this work.
The source code for this book is available to readers at http://www.apress.com.
9799FM.qxd 5/27/08 3:47 PM Page iii
9799FM.qxd 5/27/08 3:47 PM Page iv
Contents at a Glance
About the Author..................................................................xv
About the Technical Reviewers.....................................................xvii
Acknowledgments................................................................xix
Introduction......................................................................xxi
PART 1 n n n Core
nCHAPTER 1 Inversion of Control and Containers .............................3
nCHAPTER 2 Introduction to Spring..........................................21
nCHAPTER 3 Bean Configuration in Spring...................................41
nCHAPTER 4 Advanced Spring IoC Container ................................93
nCHAPTER 5 Dynamic Proxy and Classic Spring AOP........................135
nCHAPTER 6 Spring 2.x AOP and AspectJ Support ..........................167
PART 2 n n n Fundamentals
nCHAPTER 7 Spring JDBC Support .........................................209
nCHAPTER 8 Transaction Management in Spring ...........................247
nCHAPTER 9 Spring ORM Support..........................................287
nCHAPTER 10 Spring MVC Framework.......................................321
nCHAPTER 11 Integrating Spring with Other Web Frameworks ...............395
nCHAPTER 12 Spring Testing Support .......................................417
iv
9799FM.qxd 5/27/08 3:47 PM Page v
PART 3 n n n Advanced
nCHAPTER 13 Spring Security...............................................465
nCHAPTER 14 Spring Portlet MVC Framework................................511
nCHAPTER 15 Spring Web Flow .............................................545
nCHAPTER 16 Spring Remoting and Web Services ...........................583
nCHAPTER 17 Spring Support for EJB and JMS ..............................625
nCHAPTER 18 Spring Support for JMX,E-mail,and Scheduling...............663
nCHAPTER 19 Scripting in Spring............................................697
nINDEX .......................................................................709
v
9799FM.qxd 5/27/08 3:47 PM Page vi
9799FM.qxd 5/27/08 3:47 PM Page vii
Contents
About the Author..................................................................xv
About the Technical Reviewers.....................................................xvii
Acknowledgments................................................................xix
Introduction......................................................................xxi
PART 1 n n n Core
nCHAPTER 1 Inversion of Control and Containers........................3
1-1.Using a Container to Manage Your Components..................4
1-2.Using a Service Locator to Reduce Lookup Complexity............9
1-3.Applying Inversion of Control and Dependency Injection..........11
1-4.Understanding Different Types of DependencyInjection..........13
1-5.Configuring a Container with a Configuration File................17
1-6.Summary ..................................................20
nCHAPTER 2 Introduction to Spring ......................................21
2-1.Introducing the Spring Framework ............................21
2-2.Installing the Spring Framework ..............................26
2-3.Setting Up a Spring Project...................................28
2-4.Installing Spring IDE.........................................30
2-5.Using Spring IDE’s Bean-Supporting Features ..................32
2-6.Summary ..................................................39
nCHAPTER 3 Bean Configuration in Spring ..............................41
3-1.Configuring Beans in the Spring IoC Container..................41
3-2.Instantiating the Spring IoC Container..........................45
3-3.Resolving Constructor Ambiguity..............................48
3-4.Specifying Bean References..................................51
3-5.Checking Properties with Dependency Checking................55
3-6.Checking Properties with the @Required Annotation.............58
vii
9799FM.qxd 5/27/08 3:47 PM Page viii
viii nCONTENTS
3-7.Auto-Wiring Beans with XML Configuration.....................60
3-8.Auto-Wiring Beans with @Autowired and @Resource............64
3-9.Inheriting Bean Configuration.................................71
3-10.Defining Collections for Bean Properties ......................74
3-11.Specifying the Data Type for Collection Elements ..............81
3-12.Defining Collections Using Factory Beans and the
UtilitySchema.............................................84
3-13.Scanning Components from the Classpath ....................86
3-14.Summary.................................................92
nCHAPTER 4 Advanced Spring IoC Container............................93
4-1.Creating Beans by Invoking a Constructor......................93
4-2.Creating Beans by Invoking a Static FactoryMethod.............97
4-3.Creating Beans by Invoking an Instance Factory Method .........98
4-4.Creating Beans Using Spring’s Factory Bean...................100
4-5.Declaring Beans from Static Fields...........................102
4-6.Declaring Beans from Object Properties.......................104
4-7.Setting Bean Scopes .......................................106
4-8.Customizing Bean Initialization and Destruction................108
4-9.Making Beans Aware of the Container........................114
4-10.Creating Bean Post Processors .............................115
4-11.Externalizing Bean Configurations...........................120
4-12.Resolving Text Messages..................................121
4-13.Communicating with Application Events .....................123
4-14.Registering Property Editors in Spring .......................126
4-15.Creating Custom Property Editors...........................129
4-16.Loading External Resources................................131
4-17.Summary................................................134
nCHAPTER 5 Dynamic Proxy and Classic Spring AOP..................135
5-1.Problems with Non-Modularized Crosscutting Concerns.........136
5-2.Modularizing Crosscutting Concerns with Dynamic Proxy .......144
5-3.Modularizing Crosscutting Concerns with Classic
Spring Advices.............................................150
5-4.Matching Methods with Classic Spring Pointcuts...............160
5-5.Creating Proxies for Your Beans Automatically.................163
5-6.Summary .................................................165
Description:Spring addresses most aspects of Java/Java EE application development and offers simple solutions to them. By using Spring, you will be lead to use industry best practices to design and implement your applications. The releases of Spring 2.x have added many improvements and new features to the 1.x v