Mastering Spring 5.0 A comprehensive guide to becoming an expert in the Spring Framework This book is based on Spring Version 5.0 RC1 Ranga Rao Karanam BIRMINGHAM - MUMBAI Mastering Spring 5.0 Copyright © 2017 Packt Publishing All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information. First published: June 2017 Production reference: 1240617 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-78712-317-5 www.packtpub.com Credits Author Copy Editor Ranga Rao Karanam Stuti Srivastava Reviewer Project Coordinator Jarosław Krochmalski Prajakta Naik Commissioning Editor Proofreader Kunal Parikh Safis Editing Acquisition Editor Indexer Denim Pinto Rekha Nair Content Development Editor Graphics Siddhi Chavan Abhinash Sahu Technical Editor Production Coordinator Supriya Thabe Shraddha Falebhai About the Author Ranga Rao Karanam is a programmer, trainer, and architect. His areas of interest include Cloud Native Applications, microservices, evolutionary design, high-quality code, DevOps, BDD, TDD, and refactoring. He loves consulting for start-ups on developing scalable, component-based Cloud Native applications, and following modern development practices such as BDD, continuous delivery, and DevOps. He loves the freedom the Spring Framework brings to developing enterprise Java applications. Ranga started in28minutes with the vision of creating high-quality courses on developing Cloud Native Java applications. He is looking forward to enhancing his already considerable success--75,000 students on Udemy and 35,000 subscribers on YouTube. Ranga likes to play cricket and go hiking. His dream is to spend a year hiking the Himalayas. My family and best friends – thank you for everything! About the Reviewer Jarosław Krochmalski is a passionate software designer and developer who specializes in the financial business domain. He has over 12 years of experience in software development. He is a clean-code and software craftsmanship enthusiast. He is a Certified ScrumMaster and a fan of Agile. His professional interests include new technologies in web application development, design patterns, enterprise architecture, and integration patterns. He likes to experiment with NoSQL and cloud computing. Jaroslaw has been working with IDEA since its first release and has observed the IDE grow and mature. He has been designing and developing software professionally since 2000 and has been using Java as his primary programming language since 2002. In the past, he worked for companies such as Kredyt Bank (KBC) and Bank BPS on many large-scale projects, such as international money orders, express payments, and collection systems. He currently works as a consultant for the Danish company 7N as an infrastructure architect for the Nykredit bank. You can reach him via Twitter at @jkroch or by email at [email protected]. He has authored the books, IntelliJ Idea Essentials, Developing with Docker, and Docker and Kubernetes for Java Developers by Packt, and he has reviewed another book by Packt, Spring Essentials. Hello to all my friends in Finsys, 7N, and Nykredit – keep up the good work! www.PacktPub.com For support files and downloads related to your book, please visit www.PacktPub.com. Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details. At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks. https://www.packtpub.com/mapt Get the most in-demand software skills with Mapt. Mapt gives you full access to all Packt books and video courses, as well as industry-leading tools to help you plan your personal development and advance your career. Why subscribe? Fully searchable across every book published by Packt Copy and paste, print, and bookmark content On demand and accessible via a web browser Customer Feedback Thanks for purchasing this Packt book. At Packt, quality is at the heart of our editorial process. To help us improve, please leave us an honest review on this book's Amazon page at https://www.amazon.com/dp/1787123170. If you'd like to join our team of regular reviewers, you can e-mail us at [email protected]. We award our regular reviewers with free eBooks and videos in exchange for their valuable feedback. Help us be relentless in improving our products! Table of Contents Preface 1 Chapter 1: Evolution to Spring Framework 5.0 7 Spring Framework 8 Problems with EJB 8 Why is Spring Framework popular? 8 Simplified unit testing 9 Reduction in plumbing code 9 How does Spring Framework do this magic? 10 Architectural flexibility 11 Keep up with changing times 12 Spring modules 12 Spring Core Container 13 Cross-cutting concerns 13 Web 14 Business 14 Data 15 Spring Projects 15 Spring Boot 16 Spring Cloud 16 Spring Data 16 Spring Batch 17 Spring Security 18 Spring HATEOAS 18 New features in Spring Framework 5.0 19 Baseline upgrades 19 JDK 9 runtime compatibility 20 Usage of JDK 8 features in Spring Framework code 20 Reactive programming support 21 Functional web framework 21 Java modularity with Jigsaw 22 Kotlin support 23 Dropped features 24 Spring Boot 2.0 new features 25 Summary 26 Chapter 2: Dependency Injection 27 Understanding dependency injection 28 Understanding dependencies 28 The Spring IoC container 31 Defining beans and wiring 31 Creating a Spring IoC container 33 Java configuration for the application context 33 A quick review 34 Launching the application context with Java configuration 35 The console log 36 The XML configuration for the application context 37 Defining the XML Spring configuration 37 Launching an application context with the XML configuration 38 Writing JUnit using the Spring context 38 Unit testing with mocks 40 Container managed beans 42 Dependency injection types 43 The setter injection 43 The constructor injection 44 Constructor versus setter injection 44 Spring bean scopes 44 Java versus XML configuration 45 The @Autowired annotation in depth 46 The @Primary annotation 46 The @Qualifier annotation 47 Other important Spring annotations 47 Exploring Contexts and dependency injection 48 An example of CDI 49 Summary 50 Chapter 3: Building a Web Application with Spring MVC 51 Java web application architecture 52 Model 1 architecture 52 Model 2 architecture 53 Model 2 Front Controller architecture 54 Basic flows 55 Basic setup 55 Adding dependency for Spring MVC 55 Adding DispatcherServlet to web.xml 56 Creating Spring context 57 Flow 1 - Simple controller flow without View 57 Creating a Spring MVC controller 58 Running the web application 58 [ ii ]