THE EXPERT’S VOICE® IN OPEN SOURCE Covers Spring Framework 3 Spring Recipes A Problem-Solution Approach Learn to use the full power of Spring 3 through coding recipes! SECOND EDITION Gary Mak, Josh Long, Daniel Rubio and Spring Recipes Second Edition ■ ■ ■ Gary Mak Josh Long Daniel Rubio Spring Recipes, Second Edition Copyright © 2010 by Gary Mak, Josh Long, and Daniel Rubio 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-4302-2499-0 ISBN-13 (electronic): 978-1-4302-2500-3 Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1 Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights. President and Publisher: Paul Manning Lead Editor: Tom Welsh Technical Reviewer: Manuel Jordan, Mario Gray and Greg Turnquist Editorial Board: Clay Andres, Steve Anglin, Mark Beckner, Ewan Buckingham, Gary Cornell, Jonathan Gennick, Jonathan Hassell, Michelle Lowman, Matthew Moodie, Duncan Parkes, Jeffrey Pepper, Frank Pohlmann, Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh Coordinating Editor: Laurin Becker Copy Editor: Mary Ann Fugate, Heather Lang Production Support: Patrick Cunningham Indexer: BIM Indexing & Proofreading Services Artist: April Milne Cover Designer: Anna Ishchenko Distributed to the book trade worldwide by Springer Science+Business Media, LLC., 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail [email protected], or visit www.springeronline.com. For information on translations, please e-mail [email protected], or visit www.apress.com. Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Special Bulk Sales–eBook Licensing web page at www.apress.com/info/bulksales. 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 www.apress.com. You will need to answer questions pertaining to this book in order to successfully download the code. To my parents, Clark Long and Kathleen MacDonald –Josh Long To my family –Daniel Rubio Contents at a Glance About the Author ................................................................................................ xxxvii About the Technical Reviewers ......................................................................... xxxviii Acknowledgments ............................................................................................... xxxix Introduction .............................................................................................................. xli ■ Chapter 1: Introduction to Spring ............................................................................ 1 ■ Chapter 2: Advanced Spring IoC Container ........................................................... 61 ■ Chapter 3: Spring AOP and AspectJ Support ....................................................... 117 ■ Chapter 4: Scripting in Spring ............................................................................. 159 ■ Chapter 5: Spring Security .................................................................................. 171 ■ Chapter 6: Integrating Spring with Other Web Frameworks ............................... 219 ■ Chapter 7: Spring Web Flow ................................................................................ 249 ■ Chapter 8: Spring @MVC ..................................................................................... 297 ■ Chapter 9: Spring REST ........................................................................................ 371 ■ Chapter 10: Spring and Flex ................................................................................ 405 ■ Chapter 11: Grails ................................................................................................ 459 ■ Chapter 12: Spring Roo ........................................................................................ 501 ■ Chapter 13: Spring Testing .................................................................................. 525 ■ Chapter 14: Spring Portlet MVC Framework ....................................................... 569 ■ Chapter 15: Data Access...................................................................................... 597 iv ■ CONTENTS AT A GLANCE ■ Chapter 16: Transaction Management in Spring ................................................. 663 ■ Chapter 17: EJB, Spring Remoting, and Web Services ........................................ 707 ■ Chapter 18: Spring in the Enterprise ................................................................... 765 ■ Chapter 19: Messaging ........................................................................................ 803 ■ Chapter 20: Spring Integration ............................................................................ 829 ■ Chapter 21: Spring Batch .................................................................................... 875 ■ Chapter 22: Spring on the Grid ............................................................................ 909 ■ Chapter 23: jBPM and Spring .............................................................................. 935 ■ Chapter 24: OSGi and Spring ............................................................................... 959 Index ....................................................................................................................... 985 v Contents About the Authors ............................................................................................... xxxvii About the Technical Reviewers ......................................................................... xxxviii Acknowledgments ............................................................................................... xxxix Introduction .............................................................................................................. xli ■ Chapter 1: Introduction to Spring ............................................................................ 1 1-1. Instantiating the Spring IoC Container ........................................................................ 1 Problem .................................................................................................................................................... 1 Solution .................................................................................................................................................... 1 How It Works ............................................................................................................................................ 3 1-2. Configuring Beans in the Spring IoC Container .......................................................... 4 Problem .................................................................................................................................................... 4 Solution .................................................................................................................................................... 4 How It Works ............................................................................................................................................ 5 1-3. Creating Beans by Invoking a Constructor ............................................................... 14 Problem .................................................................................................................................................. 14 Solution .................................................................................................................................................. 15 How It Works .......................................................................................................................................... 15 1-4. Resolving Constructor Ambiguity ............................................................................. 18 Problem .................................................................................................................................................. 18 Solution .................................................................................................................................................. 18 How It Works .......................................................................................................................................... 18 vi ■ CONTENTS 1-5. Specifying Bean References .................................................................................... 21 Problem .................................................................................................................................................. 21 Solution .................................................................................................................................................. 21 How It Works .......................................................................................................................................... 21 1-6. Specifying the Data Type for Collection Elements .................................................... 25 Problem .................................................................................................................................................. 25 Solution .................................................................................................................................................. 25 How It Works .......................................................................................................................................... 25 1-7. Creating Beans Using Spring’s FactoryBean ............................................................ 28 Problem .................................................................................................................................................. 28 Solution .................................................................................................................................................. 28 How It Works .......................................................................................................................................... 28 1-8. Defining Collections Using Factory Beans and the Utility Schema ........................... 30 Problem .................................................................................................................................................. 30 Solution .................................................................................................................................................. 30 How It Works .......................................................................................................................................... 30 1-9. Checking Properties with Dependency Checking ..................................................... 32 Problem .................................................................................................................................................. 32 Solution .................................................................................................................................................. 32 How It Works .......................................................................................................................................... 33 1-10. Checking Properties with the @Required Annotation ............................................ 35 Problem .................................................................................................................................................. 35 Solution .................................................................................................................................................. 35 How It Works .......................................................................................................................................... 35 1-11. Auto-Wiring Beans with XML Configuration ........................................................... 38 Problem .................................................................................................................................................. 38 Solution .................................................................................................................................................. 38 How It Works .......................................................................................................................................... 39 vii ■ CONTENTS 1-12. Auto-Wiring Beans with @Autowired and @Resource ........................................... 42 Problem .................................................................................................................................................. 42 Solution .................................................................................................................................................. 42 How It Works .......................................................................................................................................... 43 1-13. Inheriting Bean Configuration ................................................................................. 49 Problem .................................................................................................................................................. 49 Solution .................................................................................................................................................. 49 How It Works .......................................................................................................................................... 50 1-14. Scanning Components from the Classpath ............................................................ 53 Problem .................................................................................................................................................. 53 Solution .................................................................................................................................................. 53 How It Works .......................................................................................................................................... 53 Summary ......................................................................................................................... 58 ■ Chapter 2: Advanced Spring IoC Container ........................................................... 61 2-1. Creating Beans by Invoking a Static Factory Method ............................................... 61 Problem .................................................................................................................................................. 61 Solution .................................................................................................................................................. 61 How It Works .......................................................................................................................................... 61 2-2. Creating Beans by Invoking an Instance Factory Method ........................................ 62 Problem .................................................................................................................................................. 62 Solution .................................................................................................................................................. 63 How It Works .......................................................................................................................................... 63 2-3. Declaring Beans from Static Fields .......................................................................... 64 Problem .................................................................................................................................................. 64 Solution .................................................................................................................................................. 64 How It Works .......................................................................................................................................... 64 2-4. Declaring Beans from Object Properties .................................................................. 66 Problem .................................................................................................................................................. 66 Solution .................................................................................................................................................. 66 How It Works .......................................................................................................................................... 66 viii
Description: