Spring Recipes, 2nd Edition Ebook free dowwnload
® THE EXPERT’S VOICE
Covers Spring Framework 3
Spring Recipes
A Problem-Solution Approach Learn to use the full power of Spring 3 through coding recipes!
and Gary Mak, Josh Long, Daniel Rubio
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 BeckerCopy 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 For information on translations, please e-mail
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 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 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
Acknowledgments ...............................................................................................
Chapter 1: Introduction to Spring ■
■
Chapter 3: Spring AOP and AspectJ Support ■
Chapter 4: Scripting in Spring ■
Chapter 5: Spring Security ■
Chapter 6: Integrating Spring with Other Web Frameworks ■
Chapter 7: Spring Web Flow ■
Chapter 8: Spring @MVC ■
Chapter 9: Spring REST ■
Chapter 10: Spring and Flex ■
Chapter 11: Grails ■
Chapter 12: Spring Roo ■
Chapter 13: Spring Testing ■
Chapter 14: Spring Portlet MVC Framework ■
■
■
Chapter 16: Transaction Management in Spring
■
Chapter 17: EJB, Spring Remoting, and Web Services
■
Chapter 18: Spring in the Enterprise
■
Chapter 19: Messaging
■
Chapter 20: Spring Integration
■
Chapter 21: Spring Batch
■
Chapter 22: Spring on the Grid
■
Chapter 23: jBPM and Spring
■
Chapter 24: OSGi and Spring
Contents
About the Authors ...............................................................................................
Acknowledgments ...............................................................................................
Chapter 1: Introduction to Spring ■
1-1. Instantiating the Spring IoC Container Problem ....................................................................................................................... How It Works ..................................................................................................................
1-2. Configuring Beans in the Spring IoC Container Problem ....................................................................................................................... How It Works ..................................................................................................................
1-3. Creating Beans by Invoking a Constructor Problem ....................................................................................................................... How It Works ..................................................................................................................
1-4. Resolving Constructor Ambiguity Problem ....................................................................................................................... How It Works ..................................................................................................................
1-5. Specifying Bean References
Problem .......................................................................................................................
How It Works ..................................................................................................................
1-6. Specifying the Data Type for Collection Elements
Problem .......................................................................................................................
How It Works ..................................................................................................................
1-7. Creating Beans Using Spring’s FactoryBean
Problem .......................................................................................................................
How It Works ..................................................................................................................
1-8. Defining Collections Using Factory Beans and the Utility Schema
Problem .......................................................................................................................
How It Works ..................................................................................................................
1-9. Checking Properties with Dependency Checking
Problem .......................................................................................................................
How It Works ..................................................................................................................
1-10. Checking Properties with the @Required Annotation
Problem .......................................................................................................................
How It Works ..................................................................................................................
1-11. Auto-Wiring Beans with XML Configuration
Problem .......................................................................................................................
How It Works ..................................................................................................................
1-12. Auto-Wiring Beans with @Autowired and @Resource Problem ....................................................................................................................... How It Works ..................................................................................................................
1-13. Inheriting Bean Configuration Problem ....................................................................................................................... How It Works ..................................................................................................................
1-14. Scanning Components from the Classpath Problem ....................................................................................................................... How It Works ..................................................................................................................
Summary .......................................................................................................................
■
2-1. Creating Beans by Invoking a Static Factory Method Problem ....................................................................................................................... How It Works ..................................................................................................................
2-2. Creating Beans by Invoking an Instance Factory Method Problem ....................................................................................................................... How It Works ..................................................................................................................
2-3. Declaring Beans from Static Fields Problem ....................................................................................................................... How It Works ..................................................................................................................
2-4. Declaring Beans from Object Properties Problem ....................................................................................................................... How It Works ..................................................................................................................
2-5. Using the Spring Expression Language
Problem .......................................................................................................................
How It Works ..................................................................................................................
2-6. Setting Bean Scopes
Problem .......................................................................................................................
How It Works ..................................................................................................................
2-7. Customizing Bean Initialization and Destruction
Problem .......................................................................................................................
How It Works ..................................................................................................................
2-8. Reducing XML Configuration with Java Config
Problem .......................................................................................................................
How It Works ..................................................................................................................
2-9. Making Beans Aware of the Container
Problem .......................................................................................................................
How It Works ..................................................................................................................
2-10. Loading External Resources
Problem .......................................................................................................................
How It Works ..................................................................................................................
2-11. Creating Bean Post Processors
Problem .......................................................................................................................
How It Works ..................................................................................................................
2-12. Externalizing Bean Configurations . ....................................................................... Problem . ................................................................................................................................................ How It Works .
2-13. Resolving Text Messages . Problem . ................................................................................................................................................ How It Works .
2-14. Communicating with Application Events . Problem . ................................................................................................................................................ How It Works .
2-15. Registering Property Editors in Spring . ............................................................... Problem . .............................................................................................................................................. How It Works . ......................................................................................................................................
2-16. Creating Custom Property Editors . ...................................................................... Problem . .............................................................................................................................................. How It Works . ......................................................................................................................................
2-17. Concurrency with TaskExecutors . ....................................................................... Problem . .............................................................................................................................................. How It Works . ......................................................................................................................................
Summary .......................................................................................................................
Chapter 3: Spring AOP and AspectJ Support ...................................................... ■
3-1. Enabling AspectJ Annotation Support in Spring .................................................... Problem . .............................................................................................................................................. How It Works . ......................................................................................................................................
3-2. Declaring Aspects with AspectJ Annotations
Problem .......................................................................................................................
How It Works ..................................................................................................................
3-3. Accessing the Join Point Information
Problem .......................................................................................................................
How It Works ..................................................................................................................
3-4. Specifying Aspect Precedence
Problem .......................................................................................................................
How It Works ..................................................................................................................
3-5. Reusing Pointcut Definitions
Problem .......................................................................................................................
How It Works ..................................................................................................................
3-6. Writing AspectJ Pointcut Expressions
Problem .......................................................................................................................
How It Works ..................................................................................................................
3-7. Introducing Behaviors to Your Beans
Problem .......................................................................................................................
How It Works ..................................................................................................................
3-8. Introducing States to Your Beans
Problem .......................................................................................................................
How It Works ..................................................................................................................
3-9. Declaring Aspects with XML-Based Configurations Problem ....................................................................................................................... How It Works ..................................................................................................................
3-10. Load-Time Weaving AspectJ Aspects in Spring Problem ....................................................................................................................... How It Works ..................................................................................................................
3-11. Configuring AspectJ Aspects in Spring Problem ....................................................................................................................... How It Works ..................................................................................................................
3-12. Injecting Spring Beans into Domain Objects Problem ....................................................................................................................... How It Works ..................................................................................................................
Summary .......................................................................................................................
Chapter 4: Scripting in Spring ■
19-1. Implementing Beans with Scripting Languages Problem ....................................................................................................................... How It Works ..................................................................................................................
19-2. Injecting Spring Beans into Scripts Problem ....................................................................................................................... How It Works ..................................................................................................................
19-3. Refreshing Beans from Scripts Problem ....................................................................................................................... How It Works ..................................................................................................................
19-4. Defining Script Sources Inline
Problem .......................................................................................................................
How It Works ..................................................................................................................
Summary .......................................................................................................................
Chapter 5: Spring Security
■
5-1. Securing URL Access
Problem .......................................................................................................................
How It Works ..................................................................................................................
5-2. Logging In to Web Applications
Problem .......................................................................................................................
How It Works ..................................................................................................................
5-3. Authenticating Users
Problem .......................................................................................................................
How It Works ..................................................................................................................
5-4. Making Access Control Decisions
Problem .......................................................................................................................
How It Works ..................................................................................................................
5-5. Securing Method Invocations
Problem .......................................................................................................................
How It Works ..................................................................................................................
5-6. Handling Security in Views
Problem .......................................................................................................................
How It Works ..................................................................................................................
5-7. Handling Domain Object Security Problem ....................................................................................................................... How It Works ..................................................................................................................
Summary .......................................................................................................................
Chapter 6: Integrating Spring with Other Web Frameworks ■
6-1. Accessing Spring in Generic Web Applications Problem ....................................................................................................................... How It Works ..................................................................................................................
6-2. Using Spring in Your Servlets and Filters Problem ....................................................................................................................... How It Works ..................................................................................................................
6-3. Integrating Spring with Struts 1.x Problem ....................................................................................................................... How It Works ..................................................................................................................
6-4. Integrating Spring with JSF Problem ....................................................................................................................... How It Works ..................................................................................................................
6-5. Integrating Spring with DWR Problem ....................................................................................................................... How It Works ..................................................................................................................
Summary .......................................................................................................................
Chapter 7: Spring Web Flow
■
7-1. Managing a Simple UI Flow with Spring Web Flow
Problem .......................................................................................................................
How It Works ..................................................................................................................
7-2. Modeling Web Flows with Different State Types
Problem .......................................................................................................................
How It Works ..................................................................................................................
7-3. Securing Web Flows
Problem .......................................................................................................................
How It Works ..................................................................................................................
7-4. Persisting Objects in Web Flows
Problem .......................................................................................................................
How It Works ..................................................................................................................
7-5. Integrating Spring Web Flow with JSF
Problem .......................................................................................................................
How It Works ..................................................................................................................
7-6. Using RichFaces with Spring Web Flow
Problem .......................................................................................................................
Approach ......................................................................................................................
Summary .......................................................................................................................
Chapter 8: Spring @MVC ■
8-1. Developing a Simple Web Application with Spring MVC Problem ....................................................................................................................... How It Works ..................................................................................................................
8-2. Mapping requests with @RequestMapping Problem ....................................................................................................................... How It Works ..................................................................................................................
8-3. Intercepting Requests with Handler Interceptors Problem ....................................................................................................................... How It Works ..................................................................................................................
8-4. Resolving User Locales Problem ....................................................................................................................... How It Works ..................................................................................................................
8-5. Externalizing Locale-Sensitive Text Messages Problem ....................................................................................................................... How It Works ..................................................................................................................
8-6. Resolving Views by Names Problem ....................................................................................................................... How It Works ..................................................................................................................
8-7. Views and Content Negotiation Problem ....................................................................................................................... How It Works ..................................................................................................................
Problem .......................................................................................................................
How It Works ..................................................................................................................
8-9. Assigning values in a Controller with @Value
Problem .......................................................................................................................
How It Works ..................................................................................................................
8-10. Handling Forms with Controllers
Problem .......................................................................................................................
How It Works ..................................................................................................................
8-11. Handling Multipage Forms with Wizard Form Controllers
Problem .......................................................................................................................
How It Works ..................................................................................................................
8-12. Bean validation with Annotations (JSR-303)
Problem .......................................................................................................................
How It Works ..................................................................................................................
8-13. Creating Excel and PDF Views
Problem .......................................................................................................................
How It Works ..................................................................................................................
Summary .......................................................................................................................
Chapter 9: Spring REST
■
9-1. Publishing a REST Service with Spring
Problem .......................................................................................................................
How It Works ..................................................................................................................
Problem ....................................................................................................................... How It Works ..................................................................................................................
9-3. Publishing RSS and Atom feeds Problem ....................................................................................................................... How It Works ..................................................................................................................
9-4. Publishing JSON with REST services Problem ....................................................................................................................... How It Works ..................................................................................................................
9-5. Accessing REST Services with Elaborate XML Responses Problem ....................................................................................................................... How It Works ..................................................................................................................
Summary .......................................................................................................................
Chapter 10: Spring and Flex ■
10-1. Getting started with Flex Problem ....................................................................................................................... How It Works ..................................................................................................................
10-2. Leaving the Sandbox Problem ....................................................................................................................... How It Works ..................................................................................................................
10-3. Adding the Spring BlazeDS support to an application Problem ....................................................................................................................... How It Works ..................................................................................................................
10-4. Exposing Services Through BlazeDS / Spring
Problem .......................................................................................................................
How It Works ..................................................................................................................
10-5. Working With Server-Side Objects
Problem .......................................................................................................................
How It Works ..................................................................................................................
10-6. Consuming Message-Oriented Services Using BlazeDS and Spring
Problem .......................................................................................................................
How It Works ..................................................................................................................
10-7. Bringing Dependency Injection to your ActionScript Client
Problem .......................................................................................................................
How It Works ..................................................................................................................
Summary .......................................................................................................................
Chapter 11: Grails
■
11-1. Getting and Installing Grails
Problem .......................................................................................................................
How It Works ..................................................................................................................
11-2. Creating a Grails Application
Problem .......................................................................................................................
How It Works ..................................................................................................................
11-3. Grails Plug-Ins
Problem .......................................................................................................................
How It Works ..................................................................................................................
11-4. Developing, Producing, and Testing in Grails Environments ............................... Problem . .............................................................................................................................................. How It Works . ......................................................................................................................................
11-5. Creating an Application’s Domain Classes . ......................................................... Problem . .............................................................................................................................................. How It Works . ......................................................................................................................................
11-6. Generating CRUD Controllers and Views for an Application’s Domain Classes ... Problem . .............................................................................................................................................. How It Works . ......................................................................................................................................
11-7. Internationalization (I18n) Message Properties . .................................................. Problem . .............................................................................................................................................. How it works . .......................................................................................................................................
11-8. Changing Permanent Storage Systems ............................................................... Problem . .............................................................................................................................................. How It Works . ......................................................................................................................................
11-9. Logging . Problem . .............................................................................................................................................. How It Works . ......................................................................................................................................
11-10. Running Unit and Integration Tests . .................................................................. Problem . .............................................................................................................................................. How It Works . ......................................................................................................................................
11-11. Using Custom Layouts and Templates
Problem .......................................................................................................................
How It Works ..................................................................................................................
11-12. Using GORM Queries
Problem .......................................................................................................................
How It Works ..................................................................................................................
11-13. Creating Custom Tags
Problem .......................................................................................................................
How It Works ..................................................................................................................
Summary .......................................................................................................................
Chapter 12: Spring Roo
■
12-1. Setting Up the Spring Roo Development Environment
Problem .......................................................................................................................
How It Works ..................................................................................................................
12-2. Creating Your First Spring Roo Project
Problem .......................................................................................................................
How It Works ..................................................................................................................
12-3. Importing an Existing Project into SpringSource Tool Suite
Problem .......................................................................................................................
How It Works ..................................................................................................................
12-4. Building A Better Application, Quicker
Problem .......................................................................................................................
How It Works ..................................................................................................................
12-5. Removing Spring Roo from Your Project Problem ....................................................................................................................... How It Works ..................................................................................................................
Summary .......................................................................................................................
Chapter 13: Spring Testing ■
13-1. Creating Tests with JUnit and TestNG Problem ....................................................................................................................... How It Works ..................................................................................................................
13-2. Creating Unit Tests and Integration Tests Problem ....................................................................................................................... How It Works ..................................................................................................................
13-3. Unit Testing Spring MVC Controllers Problem ....................................................................................................................... How It Works ..................................................................................................................