over the last few years, the java spring 2 · pdf file · 2011-07-01java...

298
Beginning Spring 2 THE EXPERT’S VOICE ® IN JAVA TECHNOLOGY Beginning Spring 2 From Novice to Professional Dave Minter Learn to build powerful enterprise Java applications using the Spring Framework

Upload: trinhcong

Post on 11-Mar-2018

219 views

Category:

Documents


5 download

TRANSCRIPT

  • this print for content onlysize & color not accurate spine = 0.693" 296 page count

    EMPOWERING PRODUCTIVITY FOR THE JAVA DEVELOPER

    Beginning Spring 2: From Novice to ProfessionalDear Reader,

    Over the last few years, the Java application development space has been flooded with tools and frameworks. The Spring framework is an agile, open source toolkit for knitting together disparate libraries and tools into powerful enterprise Java applications. Spring has a distinct, consistent design philoso-phy that has been applied carefully to all of its various libraries and wrappers. Once you understand the Spring approach, you will be able to get up to speed very quickly with unfamiliar components.

    Im an enthusiastic full-time developer of Spring-based Java applications. In this book, I show you how to use Spring, but I also show you how it makes your code easier to write, simpler to test, and as a result more reliable and support-able. I explain how to use the core features of Spring to build complex appli-cations, and how the Spring philosophy applies to the vast array of external software that it supports. By providing a simple but complete sample applica-tion, the book places the various features in context and provides you with a solid basis for further exploration.

    The examples I use include everything that you will need in the course of creating a typical web application, so you will see how to apply the latest Spring features to produce web output with Spring MVC and Spring Web Flow, how to send e-mail by using Springs wrappers for the JavaMail APIs, and how to secure your application with the Acegi libraries. I have also devoted a chapter to the creation of unit tests for applications written using Spring.

    I hope you enjoy reading this book as much as I enjoyed writing it.

    Dave Minter

    Author of

    Beginning Hibernate

    Pro Hibernate 3

    Building Portals with the Java Portlet API

    US $39.99

    Shelve in Java Programming

    User level: BeginnerIntermediate

    Minter

    Beginning Spring 2

    THE EXPERTs VOICE IN JAVA TECHNOLOGY

    Beginning

    Spring 2From Novice to Professional

    CYAN MAGENTA

    YELLOW BLACK PANTONE 123 C

    Dave Minter

    Companion eBook

    Available

    THE APRESS JAVA ROADMAP

    Expert Spring MVCand Web Flow

    Building Spring 2Enterprise Applications

    Spring 2 Recipes

    Beginning Spring 2

    www.apress.comjava.apress.com

    SOURCECODEONLINE

    Companion eBook

    See last page for details

    on $10 eBook version

    ISBN-13: 978-1-59059-685-2ISBN-10: 1-59059-685-4

    9 781590 596852

    53999

    Learn to build powerful enterprise Java applications using the Spring Framework

  • Beginning Spring 2From Novice to Professional

    Dave Minter

    Minter_685-4FRONT.fm Page i Wednesday, November 14, 2007 6:22 AM

  • Beginning Spring 2: From Novice to Professional

    Copyright 2008 by Dave Minter

    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-685-2

    ISBN-10 (pbk): 1-59059-685-4

    ISBN-13 (electronic): 978-1-4302-0493-0

    ISBN-10 (electronic): 1-4302-0493-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 the U.S. and other countries. Apress, Inc., is not affiliated with Sun Microsystems, Inc., and this book was written without endorsement from Sun Microsystems, Inc.

    Lead Editor: Steve AnglinTechnical Reviewer: Kris LanderEditorial Board: Steve Anglin, Ewan Buckingham, Tony Campbell, Gary Cornell, Jonathan Gennick,

    Jason Gilmore, Kevin Goff, Jonathan Hassell, Matthew Moodie, Joseph Ottinger, Jeffrey Pepper, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh

    Project Manager: Kylie JohnstonCopy Editor: Sharon WilkeyAssociate Production Director: Kari Brooks-CoponyProduction Editor: Gwen BurdaCompositor: Susan Glinert StevensProofreader: Linda SeifertIndexer: Toma MulliganArtist: Kinetic Publishing Services, LLCCover 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], 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 [email protected], or visit http://www.apress.com.

    The information in this book is distributed on an as is basis, without warranty. Although every precaution 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.

    Minter_685-4FRONT.fm Page ii Wednesday, November 14, 2007 6:22 AM

    mailto:[email protected]://www.springeronline.commailto:[email protected]://www.apress.comhttp://www.apress.comhttp://www.apress.com

  • To my parents

    Minter_685-4FRONT.fm Page iii Wednesday, November 14, 2007 6:22 AM

  • Minter_685-4FRONT.fm Page iv Wednesday, November 14, 2007 6:22 AM

  • v

    Contents at a Glance

    About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiiiAbout the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvAcknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xviiIntroduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix

    CHAPTER 1 An Introduction to Spring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1

    CHAPTER 2 Presenting the Sample Application . . . . . . . . . . . . . . . . . . . . . . . . . . . 13

    CHAPTER 3 The Heart of Spring: Inversion of Control . . . . . . . . . . . . . . . . . . . . . . 29

    CHAPTER 4 Data Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55

    CHAPTER 5 The Service Layer, Transaction Management, and AOP . . . . . . . . 75

    CHAPTER 6 Web Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107

    CHAPTER 7 Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137

    CHAPTER 8 Sending E-mail . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157

    CHAPTER 9 Remoting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171

    CHAPTER 10 Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193

    APPENDIX The Spring IDE Plug-in for Eclipse . . . . . . . . . . . . . . . . . . . . . . . . . . . 229

    INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245

    Minter_685-4FRONT.fm Page v Wednesday, November 14, 2007 6:22 AM

  • Minter_685-4FRONT.fm Page vi Wednesday, November 14, 2007 6:22 AM

  • vii

    Contents

    About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiiiAbout the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvAcknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xviiIntroduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix

    CHAPTER 1 An Introduction to Spring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1

    Frameworks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1Inversion of Control (IOC) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2

    Dependency Lookup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2The Problem with Dependency Lookup . . . . . . . . . . . . . . . . . . . . . . . . 3Dependency Injection as a Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . 4Dependency Injection as an Aid to Testing . . . . . . . . . . . . . . . . . . . . . 5

    An Agile Framework . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5Aspect-Oriented Programming (AOP) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .