ebook img

Spring 5 Recipes: A Problem-Solution Approach PDF

862 Pages·2017·13.402 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Spring 5 Recipes: A Problem-Solution Approach

Spring 5 Recipes A Problem-Solution Approach — Fourth Edition — Marten Deinum Daniel Rubio Josh Long Spring 5 Recipes A Problem-Solution Approach Fourth Edition Marten Deinum Daniel Rubio Josh Long Spring 5 Recipes: A Problem-Solution Approach Marten Deinum Daniel Rubio Meppel, Drenthe, The Netherlands Ensenada, Baja California, Mexico Josh Long Canyon Country, California, USA ISBN-13 (pbk): 978-1-4842-2789-3 ISBN-13 (electronic): 978-1-4842-2790-9 DOI 10.1007/978-1-4842-2790-9 Library of Congress Control Number: 2017954984 Copyright © 2017 by Marten Deinum, Daniel Rubio, and Josh Long This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed. 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. While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material contained herein. Cover image by Freepik (www.freepik.com) Managing Director: Welmoed Spahr Editorial Director: Todd Green Acquisitions Editor: Steve Anglin Development Editor: Matthew Moodie Technical Reviewer: Massimo Nardone Coordinating Editor: Mark Powers Copy Editor: Kim Wimpsett Distributed to the book trade worldwide by Springer Science+Business Media New York, 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. Apress Media, LLC is a California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation. For information on translations, please e-mail [email protected], or visit www.apress.com/ rights-permissions. Apress titles 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 Print and eBook Bulk Sales web page at www.apress.com/bulk-sales. Any source code or other supplementary material referenced by the author in this book is available to readers on GitHub via the book’s product page, located at www.apress.com/9781484227893. For more detailed information, please visit www.apress.com/source-code. Printed on acid-free paper Contents at a Glance About the Authors �������������������������������������������������������������������������������������������������xxxi About the Technical Reviewer ����������������������������������������������������������������������������xxxiii Acknowledgments �����������������������������������������������������������������������������������������������xxxv Introduction �������������������������������������������������������������������������������������������������������xxxvii ■ Chapter 1: Spring Development Tools ��������������������������������������������������������������������1 ■ Chapter 2: Spring Core Tasks ������������������������������������������������������������������������������27 ■ Chapter 3: Spring MVC ��������������������������������������������������������������������������������������117 ■ Chapter 4: Spring REST �������������������������������������������������������������������������������������183 ■ Chapter 5: Spring MVC: Async Processing ��������������������������������������������������������209 ■ Chapter 6: Spring Social ������������������������������������������������������������������������������������267 ■ Chapter 7: Spring Security ��������������������������������������������������������������������������������297 ■ Chapter 8: Spring Mobile �����������������������������������������������������������������������������������345 ■ Chapter 9: Data Access �������������������������������������������������������������������������������������361 ■ Chapter 10: Spring Transaction Management ���������������������������������������������������415 ■ Chapter 11: Spring Batch ����������������������������������������������������������������������������������447 ■ Chapter 12: Spring with NoSQL �������������������������������������������������������������������������483 ■ Chapter 13: Spring Java Enterprise Services and Remoting Technologies �������541 ■ Chapter 14: Spring Messaging ��������������������������������������������������������������������������615 ■ Chapter 15: Spring Integration ��������������������������������������������������������������������������655 iii ■ Contents at a GlanCe ■ Chapter 16: Spring Testing ��������������������������������������������������������������������������������691 ■ Chapter 17: Grails ����������������������������������������������������������������������������������������������731 ■ Appendix A: Deploying to the Cloud ������������������������������������������������������������������775 ■ Appendix B: Caching������������������������������������������������������������������������������������������795 Index ���������������������������������������������������������������������������������������������������������������������821 iv Contents About the Authors �������������������������������������������������������������������������������������������������xxxi About the Technical Reviewer ����������������������������������������������������������������������������xxxiii Acknowledgments �����������������������������������������������������������������������������������������������xxxv Introduction �������������������������������������������������������������������������������������������������������xxxvii ■ Chapter 1: Spring Development Tools ��������������������������������������������������������������������1 1-1. Build a Spring Application with the Spring Tool Suite ...............................................1 Problem ..................................................................................................................................................1 Solution...................................................................................................................................................1 How It Works ...........................................................................................................................................2 1-2. Build a Spring Application with the IntelliJ IDE .......................................................10 Problem ................................................................................................................................................10 Solution.................................................................................................................................................10 How It Works .........................................................................................................................................10 1-3. Build a Spring Application with the Maven Command-Line Interface ....................20 Problem ................................................................................................................................................20 Solution.................................................................................................................................................21 How It Works .........................................................................................................................................21 1-4. Build a Spring Application with the Gradle Wrapper ...............................................22 Problem ................................................................................................................................................22 Solution.................................................................................................................................................22 How It Works .........................................................................................................................................22 v ■ Contents 1-5. Build a Spring Application with the Gradle Command-Line Interface .....................23 Problem ................................................................................................................................................23 Solution.................................................................................................................................................23 How It Works .........................................................................................................................................24 1-6. Build a Spring Application with the Gradle Wrapper ...............................................25 Problem ................................................................................................................................................25 Solution.................................................................................................................................................25 How It Works .........................................................................................................................................25 Summary ........................................................................................................................26 ■ Chapter 2: Spring Core Tasks ������������������������������������������������������������������������������27 2-1. Use a Java Config to Configure POJOs ...................................................................28 Problem ................................................................................................................................................28 Solution.................................................................................................................................................28 How It Works .........................................................................................................................................28 2-2. Create POJOs by Invoking a Constructor ................................................................34 Problem ................................................................................................................................................34 Solution.................................................................................................................................................34 How It Works .........................................................................................................................................34 2-3. Use POJO References and Autowiring to Interact with Other POJOs ......................37 Problem ................................................................................................................................................37 Solution.................................................................................................................................................37 How It Works .........................................................................................................................................37 2-4. Autowire POJOs with the @Resource and @Inject Annotations .............................44 Problem ................................................................................................................................................44 Solution.................................................................................................................................................44 How It Works .........................................................................................................................................44 2-5. Set a POJO’s Scope with the @Scope Annotation ..................................................46 Problem ................................................................................................................................................46 Solution.................................................................................................................................................46 How It Works .........................................................................................................................................47 vi ■ Contents 2-6. Use Data from External Resources (Text Files, XML Files, Properties Files, or Image Files) .....................................................................................49 Problem ................................................................................................................................................49 Solution.................................................................................................................................................50 How It Works .........................................................................................................................................50 2-7. Resolve I18N Text Messages for Different Locales in Properties Files ...................54 Problem ................................................................................................................................................54 Solution.................................................................................................................................................54 How It Works .........................................................................................................................................54 2-8. Customize POJO Initialization and Destruction with Annotations ...........................56 Problem ................................................................................................................................................56 Solution.................................................................................................................................................57 How It Works .........................................................................................................................................57 2-9. Create Post-Processors to Validate and Modify POJOs ...........................................61 Problem ................................................................................................................................................61 Solution.................................................................................................................................................61 How It Works .........................................................................................................................................61 2-10. Create POJOs with a Factory (Static Method, Instance Method, Spring’s FactoryBean) ....................................................................................................64 Problem ................................................................................................................................................64 Solution.................................................................................................................................................64 How It Works .........................................................................................................................................64 2-11. Use Spring Environments and Profiles to Load Different Sets of POJOs ..............69 Problem ................................................................................................................................................69 Solution.................................................................................................................................................69 How It Works .........................................................................................................................................69 2-12. Make POJOs Aware of Spring’s IoC Container Resources .....................................71 Problem ................................................................................................................................................71 Solution.................................................................................................................................................71 How It Works .........................................................................................................................................73 vii ■ Contents 2-13. Use Aspect-Oriented Programming with Annotations ...........................................73 Problem ................................................................................................................................................73 Solution.................................................................................................................................................73 How It Works .........................................................................................................................................74 2-14. Access the Join Point Information ........................................................................81 Problem ................................................................................................................................................81 Solution.................................................................................................................................................81 How It Works .........................................................................................................................................82 2-15. Specify Aspect Precedence with the @Order Annotation .....................................83 Problem ................................................................................................................................................83 Solution.................................................................................................................................................83 How It Works .........................................................................................................................................83 2-16. Reuse Aspect Pointcut Definitions ........................................................................85 Problem ................................................................................................................................................85 Solution.................................................................................................................................................85 How It Works .........................................................................................................................................85 2-17. Write AspectJ Pointcut Expressions ......................................................................87 Problem ................................................................................................................................................87 Solution.................................................................................................................................................87 How It Works .........................................................................................................................................87 2-18. Use AOP for introductions for POJOs ....................................................................92 Problem ................................................................................................................................................92 Solution.................................................................................................................................................92 How It Works .........................................................................................................................................92 2-19. Introduce States to Your POJOs with AOP .............................................................94 Problem ................................................................................................................................................94 Solution.................................................................................................................................................94 How It Works .........................................................................................................................................95 viii ■ Contents 2-20. Use Load-Time Weaving AspectJ Aspects in Spring .............................................97 Problem ................................................................................................................................................97 Solution.................................................................................................................................................97 How It Works .........................................................................................................................................97 2-21. Configure AspectJ Aspects in Spring ..................................................................101 Problem ..............................................................................................................................................101 Solution...............................................................................................................................................101 How It Works .......................................................................................................................................102 2-22. Inject POJOs into Domain Objects with AOP .......................................................103 Problem ..............................................................................................................................................103 Solution...............................................................................................................................................103 How It Works .......................................................................................................................................104 2-23. Applying Concurrency with Spring and TaskExecutors .......................................105 Problem ..............................................................................................................................................105 Solution...............................................................................................................................................105 How It Works .......................................................................................................................................106 2-24. Communicate Application Events Between POJOs .............................................112 Problem ..............................................................................................................................................112 Solution...............................................................................................................................................112 How It Works .......................................................................................................................................112 Summary ......................................................................................................................115 ■ Chapter 3: Spring MVC ��������������������������������������������������������������������������������������117 3-1. Develop a Simple Web Application with Spring MVC ............................................117 Problem ..............................................................................................................................................117 Solution...............................................................................................................................................117 How It Works .......................................................................................................................................119 3-2. Map Requests with @RequestMapping ................................................................129 Problem ..............................................................................................................................................129 Solution...............................................................................................................................................129 How It Works .......................................................................................................................................129 ix

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.