Tải bản đầy đủ (.pdf) (1,105 trang)

Spring Recipes, 2nd Edition pdf

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (9.72 MB, 1,105 trang )

THE EXPERT’S VOICE
®
IN OPEN SOURCE
Gary Mak, Josh Long, and Daniel Rubio
Covers
Spring
Framework 3
Learn to use the full power of Spring 3 through coding recipes!
SECOND EDITION
Spring
Recipes
A Problem-Solution Approach
www.it-ebooks.info
www.it-ebooks.info


Spring Recipes
Second Edition










■ ■ ■
Gary Mak
Josh Long


Daniel Rubio


www.it-ebooks.info

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 , or visit www.springeronline.com.
For information on translations, please e-mail , 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.
www.it-ebooks.info
To my parents, Clark Long and Kathleen MacDonald
–Josh Long
To my family
–Daniel Rubio

























www.it-ebooks.info

iv
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
www.it-ebooks.info
■ CONTENTS AT A GLANCE
v
■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
www.it-ebooks.info

vi
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

www.it-ebooks.info
■ CONTENTS
vii
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


www.it-ebooks.info
■ CONTENTS
viii
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
www.it-ebooks.info
■ CONTENTS
ix
2-5. Using the Spring Expression Language 68

Problem 68
Solution 68
How It Works 68
2-6. Setting Bean Scopes 74
Problem 74
Solution 74
How It Works 74
2-7. Customizing Bean Initialization and Destruction 77
Problem 77
Solution 77
How It Works 77
2-8. Reducing XML Configuration with Java Config 82
Problem 82
Solution 82
How It Works 82
2-9. Making Beans Aware of the Container 86
Problem 86
Solution 86
How It Works 87
2-10. Loading External Resources 88
Problem 88
Solution 88
How It Works 89
2-11. Creating Bean Post Processors 91
Problem 91
Solution 91
How It Works 92


www.it-ebooks.info

■ CONTENTS
x
2-12. Externalizing Bean Configurations . 95
Problem . 95
Solution . 95
How It Works . 95
2-13. Resolving Text Messages 96
Problem . 96
Solution . 97
How It Works . 97
2-14. Communicating with Application Events 99
Problem . 99
Solution . 99
How It Works . 99
2-15. Registering Property Editors in Spring . 102
Problem . 102
Solution . 102
How It Works . 102
2-16. Creating Custom Property Editors . 105
Problem . 105
Solution . 105
How It Works . 105
2-17. Concurrency with TaskExecutors 107
Problem . 107
Solution . 107
How It Works . 107
Summary 116
■Chapter 3: Spring AOP and AspectJ Support 117
3-1. Enabling AspectJ Annotation Support in Spring 117
Problem . 117

Solution . 118
How It Works . 118
www.it-ebooks.info
■ CONTENTS
xi
3-2. Declaring Aspects with AspectJ Annotations 120
Problem 120
Solution 120
How It Works 121
3-3. Accessing the Join Point Information 126
Problem 126
Solution 127
How It Works 127
3-4. Specifying Aspect Precedence 128
Problem 128
Solution 128
How It Works 128
3-5. Reusing Pointcut Definitions 130
Problem 130
Solution 130
How It Works 130
3-6. Writing AspectJ Pointcut Expressions 132
Problem 132
Solution 132
How It Works 133
3-7. Introducing Behaviors to Your Beans 138
Problem 138
Solution 138
How It Works 138
3-8. Introducing States to Your Beans 141

Problem 141
Solution 141
How It Works 141


www.it-ebooks.info
■ CONTENTS
xii
3-9. Declaring Aspects with XML-Based Configurations 143
Problem 143
Solution 143
How It Works 143
3-10. Load-Time Weaving AspectJ Aspects in Spring 146
Problem 146
Solution 146
How It Works 147
3-11. Configuring AspectJ Aspects in Spring 152
Problem 152
Solution 152
How It Works 153
3-12. Injecting Spring Beans into Domain Objects 154
Problem 154
Solution 154
How It Works 154
Summary 158
■Chapter 4: Scripting in Spring 159
19-1. Implementing Beans with Scripting Languages 159
Problem 159
Solution 159
How It Works 160

19-2. Injecting Spring Beans into Scripts 164
Problem 164
Solution 164
How It Works 164
19-3. Refreshing Beans from Scripts 167
Problem 167
Solution 167
How It Works 168
www.it-ebooks.info
■ CONTENTS
xiii
19-4. Defining Script Sources Inline 168
Problem 168
Solution 168
How It Works 169
Summary 170
■Chapter 5: Spring Security 171
5-1. Securing URL Access 172
Problem 172
Solution 172
How It Works 173
5-2. Logging In to Web Applications 183
Problem 183
Solution 183
How It Works 183
5-3. Authenticating Users 187
Problem 187
Solution 188
How It Works 188
5-4. Making Access Control Decisions 199

Problem 199
Solution 200
How It Works 200
5-5. Securing Method Invocations 203
Problem 203
Solution 203
How It Works 203
5-6. Handling Security in Views 206
Problem 206
Solution 206
How It Works 206
www.it-ebooks.info
■ CONTENTS
xiv
5-7. Handling Domain Object Security 208
Problem 208
Solution 208
How It Works 209
Summary 218
■Chapter 6: Integrating Spring with Other Web Frameworks 219
6-1. Accessing Spring in Generic Web Applications 220
Problem 220
Solution 220
How It Works 220
6-2. Using Spring in Your Servlets and Filters 224
Problem 224
Solution 225
How It Works 225
6-3. Integrating Spring with Struts 1.x 230
Problem 230

Solution 230
How It Works 231
6-4. Integrating Spring with JSF 237
Problem 237
Solution 237
How It Works 238
6-5. Integrating Spring with DWR 244
Problem 244
Solution 244
How It Works 244
Summary 248


www.it-ebooks.info
■ CONTENTS
xv
■Chapter 7: Spring Web Flow 249
7-1. Managing a Simple UI Flow with Spring Web Flow 249
Problem 249
Solution 249
How It Works 250
7-2. Modeling Web Flows with Different State Types 258
Problem 258
Solution 259
How It Works 259
7-3. Securing Web Flows 272
Problem 272
Solution 273
How It Works 273
7-4. Persisting Objects in Web Flows 275

Problem 275
Solution 275
How It Works 275
7-5. Integrating Spring Web Flow with JSF 283
Problem 283
Solution 283
How It Works 283
7-6. Using RichFaces with Spring Web Flow 291
Problem 291
Solution 291
Approach 291
Summary 295



www.it-ebooks.info
■ CONTENTS
xvi
■Chapter 8: Spring @MVC 297
8-1. Developing a Simple Web Application with Spring MVC 297
Problem 297
Solution 297
How It Works 299
8-2. Mapping requests with @RequestMapping 310
Problem 310
Solution 311
How It Works 311
8-3. Intercepting Requests with Handler Interceptors 314
Problem 314
Solution 315

How It Works 315
8-4. Resolving User Locales 318
Problem 318
Solution 319
How It Works 319
8-5. Externalizing Locale-Sensitive Text Messages 321
Problem 321
Solution 321
How It Works 321
8-6. Resolving Views by Names 322
Problem 322
Solution 322
How It Works 323
8-7. Views and Content Negotiation 325
Problem 325
Solution 326
How It Works 326

www.it-ebooks.info
■ CONTENTS
xvii
8-8. Mapping Exceptions to Views 329
Problem 329
Solution 329
How It Works 329
8-9. Assigning values in a Controller with @Value 331
Problem 331
Solution 331
How It Works 331
8-10. Handling Forms with Controllers 333

Problem 333
Solution 333
How It Works 333
8-11. Handling Multipage Forms with Wizard Form Controllers 348
Problem 348
Solution 348
How It Works 349
8-12. Bean validation with Annotations (JSR-303) 359
Problem 359
Solution 359
How It Works 359
8-13. Creating Excel and PDF Views 362
Problem 362
Solution 362
How It Works 362
Summary 369
■Chapter 9: Spring REST 371
9-1. Publishing a REST Service with Spring 371
Problem 371
Solution 371
How It Works 372
www.it-ebooks.info
■ CONTENTS
xviii
9-2. Accessing a REST Service with Spring 376
Problem 376
Solution 376
How It Works 377
9-3. Publishing RSS and Atom feeds 381
Problem 381

Solution 381
How It Works 382
9-4. Publishing JSON with REST services 391
Problem 391
Solution 391
How It Works 392
9-5. Accessing REST Services with Elaborate XML Responses 394
Problem 394
Solution 394
How It Works 394
Summary 404
■Chapter 10: Spring and Flex 405
10-1. Getting started with Flex 406
Problem 406
Solution 407
How It Works 407
10-2. Leaving the Sandbox 412
Problem 412
Solution 412
How It Works 413
10-3. Adding the Spring BlazeDS support to an application 424
Problem 424
Solution 424
How It Works 425
www.it-ebooks.info
■ CONTENTS
xix
10-4. Exposing Services Through BlazeDS / Spring 430
Problem 430
Solution 430

How It Works 430
10-5. Working With Server-Side Objects 437
Problem 437
Solution 437
How It Works 437
10-6. Consuming Message-Oriented Services Using BlazeDS and Spring 440
Problem 440
Solution 441
How It Works 441
10-7. Bringing Dependency Injection to your ActionScript Client 452
Problem 452
Solution 453
How It Works 453
Summary 457
■Chapter 11: Grails 459
11-1. Getting and Installing Grails 459
Problem 459
Solution 459
How It Works 460
11-2. Creating a Grails Application 460
Problem 460
Solution 461
How It Works 461
11-3. Grails Plug-Ins 466
Problem 466
Solution 466
How It Works 467
www.it-ebooks.info
■ CONTENTS
xx

11-4. Developing, Producing, and Testing in Grails Environments 468
Problem . 468
Solution . 468
How It Works . 468
11-5. Creating an Application’s Domain Classes 470
Problem . 470
Solution . 471
How It Works . 471
11-6. Generating CRUD Controllers and Views for an Application’s Domain Classes 473
Problem . 473
Solution . 473
How It Works . 473
11-7. Internationalization (I18n) Message Properties 477
Problem . 477
Solution . 477
How it works 477
11-8. Changing Permanent Storage Systems 480
Problem . 480
Solution . 480
How It Works . 480
11-9. Logging 483
Problem . 483
Solution . 483
How It Works . 483
11-10. Running Unit and Integration Tests . 486
Problem . 486
Solution . 486
How It Works . 487
www.it-ebooks.info
■ CONTENTS

xxi
11-11. Using Custom Layouts and Templates 492
Problem 492
Solution 492
How It Works 492
11-12. Using GORM Queries 495
Problem 495
Solution 495
How It Works 495
11-13. Creating Custom Tags 497
Problem 497
Solution 497
How It Works 497
Summary 499
■Chapter 12: Spring Roo 501
12-1. Setting Up the Spring Roo Development Environment 503
Problem 503
Solution 503
How It Works 503
12-2. Creating Your First Spring Roo Project 506
Problem 506
Solution 506
How It Works 506
12-3. Importing an Existing Project into SpringSource Tool Suite 512
Problem 512
Solution 512
How It Works 512
12-4. Building A Better Application, Quicker 514
Problem 514
Solution 515

How It Works 515
www.it-ebooks.info
■ CONTENTS
xxii
12-5. Removing Spring Roo from Your Project 521
Problem 521
Solution 522
How It Works 522
Summary 523
■Chapter 13: Spring Testing 525
13-1. Creating Tests with JUnit and TestNG 526
Problem 526
Solution 526
How It Works 526
13-2. Creating Unit Tests and Integration Tests 532
Problem 532
Solution 532
How It Works 533
13-3. Unit Testing Spring MVC Controllers 542
Problem 542
Solution 542
How It Works 542
13-4. Managing Application Contexts in Integration Tests 544
Problem 544
Solution 544
How It Works 545
13-5. Injecting Test Fixtures into Integration Tests 551
Problem 551
Solution 551
How It Works 551

13-6. Managing Transactions in Integration Tests 555
Problem 555
Solution 555
How It Works 556
www.it-ebooks.info
■ CONTENTS
xxiii
13-7. Accessing a Database in Integration Tests 561
Problem 561
Solution 561
How It Works 562
13-8. Using Spring’s Common Testing Annotations 565
Problem 565
Solution 565
How It Works 566
Summary 568
■Chapter 14: Spring Portlet MVC Framework 569
14-1. Developing a Simple Portlet with Spring Portlet MVC 569
Problem 569
Solution 569
How It Works 571
14-2. Mapping Portlet Requests to Handlers 579
Problem 579
Solution 579
How It Works 579
14-3. Handling Portlet Forms with Simple Form Controllers 587
Problem 587
Solution 587
How It Works 587
Summary 595

■Chapter 15: Data Access 597
Problems with Direct JDBC 598
Setting Up the Application Database 598
Understanding the Data Access Object Design Pattern 600
Implementing the DAO with JDBC 600
Configuring a Data Source in Spring 602
www.it-ebooks.info

×