Beginning Spring Boot 2 Applications and Microservices with the Spring Framework (K. Siva Prasad Reddy) (Z-Library)

Author: K. Siva Prasad Reddy

Java

Learn Spring Boot and how to build Java-based enterprise, web, and microservice applications with it. In this book, you'll see how to work with relational and NoSQL databases, build your first microservice, enterprise, or web application, and enhance that application with REST APIs. You'll also learn how to build reactive web applications using Spring Boot along with Spring Web Reactive. Then you’ll secure your Spring Boot-created application or service before testing and deploying it. After reading and learning with Beginning Spring Boot 2, you'll have the skills and techniques to start building your first Spring Boot applications and microservices with confidence to take the next steps in your career journey. What You'll Learn Use Spring Boot autoconfiguration Work with relational and NoSQL databases Build web applications with Spring Boot Apply REST APIs using Spring Boot Cre ate reactive web applications using Spring Web Reactive Secure your Spring Boot applications or web services Test and deploy your Spring Boot applications Who This Book Is For Experienced Java and Spring Framework developers who are new to the new Spring Boot micro-framework.

📄 File Format: PDF
💾 File Size: 4.7 MB
46
Views
0
Downloads
0.00
Total Donations

📄 Text Preview (First 20 pages)

ℹ️

Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

📄 Page 1
Beginning Spring Boot 2 Applications and Microservices with the Spring Framework — K. Siva Prasad Reddy www.allitebooks.com
📄 Page 2
Beginning Spring Boot 2 Applications and Microservices with the Spring Framework K. Siva Prasad Reddy www.allitebooks.com
📄 Page 3
Beginning Spring Boot 2: Applications and Microservices with the Spring Framework K. Siva Prasad Reddy Hyderabad, India ISBN-13 (pbk): 978-1-4842-2930-9 ISBN-13 (electronic): 978-1-4842-2931-6 DOI 10.1007/978-1-4842-2931-6 Library of Congress Control Number: 2017955551 Copyright © 2017 by K. Siva Prasad Reddy 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: Kezia Endsley 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 orders-ny@springer-sbm.com, 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 rights@apress.com, or visit http://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 http://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/9781484229309. For more detailed information, please visit http://www.apress.com/source-code. Printed on acid-free paper www.allitebooks.com
📄 Page 4
iii Contents at a Glance About the Author ��������������������������������������������������������������������������������������������������� xiii About the Technical Reviewer ���������������������������������������������������������������������������������xv Acknowledgments �������������������������������������������������������������������������������������������������xvii Introduction ������������������������������������������������������������������������������������������������������������xix ■Chapter 1: Introduction to Spring Boot ������������������������������������������������������������������ 1 ■Chapter 2: Getting Started with Spring Boot ������������������������������������������������������� 21 ■Chapter 3: Spring Boot Autoconfiguration ����������������������������������������������������������� 35 ■Chapter 4: Spring Boot Essentials ����������������������������������������������������������������������� 47 ■Chapter 5: Working with JdbcTemplate �������������������������������������������������������������� 55 ■Chapter 6: Working with MyBatis ������������������������������������������������������������������������ 65 ■Chapter 7: Working with JOOQ ���������������������������������������������������������������������������� 71 ■Chapter 8: Working with JPA ������������������������������������������������������������������������������� 83 ■Chapter 9: Working with MongoDB ��������������������������������������������������������������������� 99 ■Chapter 10: Web Applications with Spring Boot������������������������������������������������ 107 ■Chapter 11: Building REST APIs Using Spring Boot ������������������������������������������� 133 ■Chapter 12: Reactive Programming Using Spring WebFlux ������������������������������� 157 ■Chapter 13: Securing Web Applications ������������������������������������������������������������ 175 ■Chapter 14: Spring Boot Actuator ��������������������������������������������������������������������� 197 ■Chapter 15: Testing Spring Boot Applications ��������������������������������������������������� 221 www.allitebooks.com
📄 Page 5
■ Contents at a GlanCe iv ■Chapter 16: Creating a Custom Spring Boot Starter ������������������������������������������ 247 ■Chapter 17: Spring Boot with Groovy, Scala, and Kotlin ������������������������������������ 259 ■Chapter 18: Introducing JHipster ���������������������������������������������������������������������� 279 ■Chapter 19: Deploying Spring Boot Applications ����������������������������������������������� 289 Index ��������������������������������������������������������������������������������������������������������������������� 301 www.allitebooks.com
📄 Page 6
v Contents About the Author ��������������������������������������������������������������������������������������������������� xiii About the Technical Reviewer ���������������������������������������������������������������������������������xv Acknowledgments �������������������������������������������������������������������������������������������������xvii Introduction ������������������������������������������������������������������������������������������������������������xix ■Chapter 1: Introduction to Spring Boot ������������������������������������������������������������������ 1 Overview of the Spring Framework ���������������������������������������������������������������������������������� 1 Spring Configuration Styles ���������������������������������������������������������������������������������������������� 2 Developing Web Application Using SpringMVC and JPA ��������������������������������������������������� 3 A Quick Taste of Spring Boot ������������������������������������������������������������������������������������������ 16 Easy Dependency Management ����������������������������������������������������������������������������������������������������������� 19 Autoconfiguration ��������������������������������������������������������������������������������������������������������������������������������� 19 Embedded Servlet Container Support �������������������������������������������������������������������������������������������������� 19 Summary ������������������������������������������������������������������������������������������������������������������������ 20 ■Chapter 2: Getting Started with Spring Boot ������������������������������������������������������� 21 What Is Spring Boot? ������������������������������������������������������������������������������������������������������ 21 Spring Boot Starters ����������������������������������������������������������������������������������������������������������������������������� 21 Spring Boot Autoconfiguration �������������������������������������������������������������������������������������������������������������� 22 Elegant Configuration Management ����������������������������������������������������������������������������������������������������� 22 Spring Boot Actuator����������������������������������������������������������������������������������������������������������������������������� 22 Easy-to-Use Embedded Servlet Container Support ������������������������������������������������������������������������������ 22 www.allitebooks.com
📄 Page 7
■ Contents vi Your First Spring Boot Application ���������������������������������������������������������������������������������� 23 Using Spring Initializr ��������������������������������������������������������������������������������������������������������������������������� 23 Using the Spring Tool Suite ������������������������������������������������������������������������������������������������������������������� 24 Using Intellij IDEA ��������������������������������������������������������������������������������������������������������������������������������� 25 Using NetBeans IDE ������������������������������������������������������������������������������������������������������������������������������ 26 Exploring the Project ���������������������������������������������������������������������������������������������������������������������������� 26 The Application Entry Point Class ����������������������������������������������������������������������������������� 31 Fat JAR Using the Spring Boot Maven Plugin ����������������������������������������������������������������� 32 Spring Boot Using Gradle ����������������������������������������������������������������������������������������������� 32 Maven or Gradle? ��������������������������������������������������������������������������������������������������������������������������������� 33 Summary ������������������������������������������������������������������������������������������������������������������������ 33 ■Chapter 3: Spring Boot Autoconfiguration ����������������������������������������������������������� 35 Exploring the Power of @Conditional ����������������������������������������������������������������������������� 35 Using @Conditional Based on System Properties �������������������������������������������������������������������������������� 36 Using @Conditional Based on the Presence/Absence of a Java Class ������������������������������������������������� 38 Using @Conditional Based on the Configured Spring Beans ���������������������������������������������������������������� 38 Using @Conditional Based on a Property’s Configuration �������������������������������������������������������������������� 39 Spring Boot’s Built-In @Conditional Annotations ����������������������������������������������������������� 40 How Spring Boot Autoconfiguration Works �������������������������������������������������������������������� 42 Summary ������������������������������������������������������������������������������������������������������������������������ 45 ■Chapter 4: Spring Boot Essentials ����������������������������������������������������������������������� 47 Logging��������������������������������������������������������������������������������������������������������������������������� 47 Externalizing Configuration Properties ��������������������������������������������������������������������������� 49 Type-Safe Configuration Properties ������������������������������������������������������������������������������������������������������ 49 Relaxed Binding ������������������������������������������������������������������������������������������������������������������������������������ 50 Validating Properties with the Bean Validation API ������������������������������������������������������������������������������� 50 Developer Tools �������������������������������������������������������������������������������������������������������������� 51 Summary ������������������������������������������������������������������������������������������������������������������������ 53 www.allitebooks.com
📄 Page 8
■ Contents vii ■Chapter 5: Working with JdbcTemplate �������������������������������������������������������������� 55 Using JdbcTemplate Without SpringBoot ����������������������������������������������������������������������� 55 Using JdbcTemplate with Spring Boot ��������������������������������������������������������������������������� 58 Initializing the Database ����������������������������������������������������������������������������������������������������������������������� 58 Using Other Connection Pooling Libraries �������������������������������������������������������������������������������������������� 62 Database Migration with Flyway ������������������������������������������������������������������������������������ 63 Summary ������������������������������������������������������������������������������������������������������������������������ 64 ■Chapter 6: Working with MyBatis ������������������������������������������������������������������������ 65 Using the Spring Boot MyBatis Starter ��������������������������������������������������������������������������� 65 Summary ������������������������������������������������������������������������������������������������������������������������ 69 ■Chapter 7: Working with JOOQ ���������������������������������������������������������������������������� 71 Introduction to JOOQ ������������������������������������������������������������������������������������������������������ 71 Using Spring Boot’s JOOQ Starter ���������������������������������������������������������������������������������� 72 Configure Spring Boot JOOQ Starter ����������������������������������������������������������������������������������������������������� 73 Database Schema ��������������������������������������������������������������������������������������������������������������������������������� 73 Code Generation Using the JOOQ Maven Codegen Plugin �������������������������������������������������������������������� 74 Add JOOQ Generated Code as a Source Folder ������������������������������������������������������������������������������������ 76 Domain Objects ������������������������������������������������������������������������������������������������������������������������������������ 77 Using JOOQ DSL ����������������������������������������������������������������������������������������������������������������������������������� 77 Summary ������������������������������������������������������������������������������������������������������������������������ 82 ■Chapter 8: Working with JPA ������������������������������������������������������������������������������� 83 Introducing the Spring Data JPA ������������������������������������������������������������������������������������ 83 Using Spring Data JPA with Spring Boot ������������������������������������������������������������������������ 85 Add Dynamic Query Methods ��������������������������������������������������������������������������������������������������������������� 88 Using the Sort and Pagination Features ����������������������������������������������������������������������������������������������� 88 Working with Multiple Databases ��������������������������������������������������������������������������������������������������������� 89 Use OpenEntityManagerInViewFilter for Multiple Data Sources ����������������������������������������������������������� 96 Summary ������������������������������������������������������������������������������������������������������������������������ 97 www.allitebooks.com
📄 Page 9
■ Contents viii ■Chapter 9: Working with MongoDB ��������������������������������������������������������������������� 99 Introducing MongoDB ����������������������������������������������������������������������������������������������������� 99 Installing MongoDB ������������������������������������������������������������������������������������������������������ 100 Installing MongoDB on Windows �������������������������������������������������������������������������������������������������������� 100 Installing MongoDB on MacOS ����������������������������������������������������������������������������������������������������������� 101 Installing MongoDB on Linux �������������������������������������������������������������������������������������������������������������� 101 Getting Started with MongoDB Using the Mongo Shell ������������������������������������������������ 101 Introducing Spring Data MongoDB ������������������������������������������������������������������������������� 102 Using Embedded Mongo for Testing ����������������������������������������������������������������������������� 105 Summary ���������������������������������������������������������������������������������������������������������������������� 106 ■Chapter 10: Web Applications with Spring Boot������������������������������������������������ 107 Introducing SpringMVC������������������������������������������������������������������������������������������������� 107 Developing Web Application Using Spring Boot ����������������������������������������������������������� 109 Using the Tomcat, Jetty, and Undertow Embedded Servlet Containers ������������������������ 112 Customizing Embedded Servlet Containers ����������������������������������������������������������������� 114 Customizing SpringMVC Configuration ������������������������������������������������������������������������� 115 Registering Servlets, Filters, and Listeners as Spring Beans ��������������������������������������� 116 Spring Boot Web Application as a Deployable WAR ������������������������������������������������������ 119 View Templates that Spring Boot Supports ������������������������������������������������������������������ 120 Using the Thymeleaf View Templates ������������������������������������������������������������������������������������������������� 121 Working with Thymeleaf Forms ���������������������������������������������������������������������������������������������������������� 122 Form Validation ����������������������������������������������������������������������������������������������������������������������������������� 124 File Uploading ��������������������������������������������������������������������������������������������������������������� 128 Using ResourceBundles for Internationalization (i18n) ������������������������������������������������ 128 ResourceBundles for Hibernate Validation Errors �������������������������������������������������������� 129 Error Handling �������������������������������������������������������������������������������������������������������������� 130 Summary ���������������������������������������������������������������������������������������������������������������������� 132 www.allitebooks.com
📄 Page 10
■ Contents ix ■Chapter 11: Building REST APIs Using Spring Boot ������������������������������������������� 133 Introduction to RESTful Web Services �������������������������������������������������������������������������� 133 REST API Using SpringMVC ������������������������������������������������������������������������������������������ 134 CORS (Cross-Origin Resource Sharing) Support ��������������������������������������������������������������������������������� 144 Exposing JPA Entities with Bi-Directional References Through RESTful Services ����������������������������� 146 REST API Using Spring Data REST �������������������������������������������������������������������������������� 149 Sorting and Pagination ����������������������������������������������������������������������������������������������������������������������� 151 CORS Support in Spring Data REST ���������������������������������������������������������������������������������������������������� 153 Exception Handling ������������������������������������������������������������������������������������������������������� 153 Summary ���������������������������������������������������������������������������������������������������������������������� 155 ■Chapter 12: Reactive Programming Using Spring WebFlux ������������������������������� 157 Introduction to Reactive Programming ������������������������������������������������������������������������� 157 Reactive Streams ������������������������������������������������������������������������������������������������������������������������������� 158 Project Reactor ����������������������������������������������������������������������������������������������������������������������������������� 158 Reactive Web Applications Using Spring WebFlux ������������������������������������������������������� 159 WebFlux Using the Annotation-Based Programming Model ��������������������������������������������������������������� 160 WebFlux Using a Functional Programming Model ������������������������������������������������������������������������������ 163 Thymeleaf Reactive Support ��������������������������������������������������������������������������������������������������������������� 169 Reactive WebClient ����������������������������������������������������������������������������������������������������������������������������� 172 Testing Spring WebFlux Applications �������������������������������������������������������������������������������������������������� 173 Summary ���������������������������������������������������������������������������������������������������������������������� 174 ■Chapter 13: Securing Web Applications ������������������������������������������������������������ 175 Spring Security in Spring Boot Web Application ����������������������������������������������������������� 175 Implementing the Remember-Me Feature ������������������������������������������������������������������� 184 Simple Hash-Based Token as Cookie ������������������������������������������������������������������������������������������������� 184 Persistent Tokens ������������������������������������������������������������������������������������������������������������������������������� 186 Cross-Site Request Forgery ����������������������������������������������������������������������������������������� 187 Method-Level Security ������������������������������������������������������������������������������������������������� 188 Securing the REST API Using Spring Security �������������������������������������������������������������� 190 Summary ���������������������������������������������������������������������������������������������������������������������� 195 www.allitebooks.com
📄 Page 11
■ Contents x ■Chapter 14: Spring Boot Actuator ��������������������������������������������������������������������� 197 Introducing the Spring Boot Actuator ��������������������������������������������������������������������������� 197 Exploring Actuator’s Endpoints������������������������������������������������������������������������������������� 199 The /info Endpoint ������������������������������������������������������������������������������������������������������������������������������ 200 The /health Endpoint �������������������������������������������������������������������������������������������������������������������������� 201 The /beans Endpoint ��������������������������������������������������������������������������������������������������������������������������� 201 The /autoconfig Endpoint�������������������������������������������������������������������������������������������������������������������� 202 The /mappings Endpoint ��������������������������������������������������������������������������������������������������������������������� 204 The /configprops Endpoint������������������������������������������������������������������������������������������������������������������ 204 The /metrics Endpoint ������������������������������������������������������������������������������������������������������������������������ 205 The /env Endpoint ������������������������������������������������������������������������������������������������������������������������������� 206 The /trace Endpoint ���������������������������������������������������������������������������������������������������������������������������� 207 The /dump Endpoint ��������������������������������������������������������������������������������������������������������������������������� 208 The /loggers Endpoint������������������������������������������������������������������������������������������������������������������������� 209 The /logfile Endpoint ��������������������������������������������������������������������������������������������������������������������������� 211 The /shutdown Endpoint ��������������������������������������������������������������������������������������������������������������������� 211 The /actuator Endpoint ����������������������������������������������������������������������������������������������������������������������� 212 Customizing Actuator Endpoints ���������������������������������������������������������������������������������� 213 Securing Actuator Endpoints ���������������������������������������������������������������������������������������� 214 Implementing Custom Health Indicators ���������������������������������������������������������������������� 215 Capturing Custom Application Metrics ������������������������������������������������������������������������� 217 CORS Support for Actuator Endpoints �������������������������������������������������������������������������� 219 Monitoring and Management Over JMX ����������������������������������������������������������������������� 219 Summary ���������������������������������������������������������������������������������������������������������������������� 220 ■Chapter 15: Testing Spring Boot Applications ��������������������������������������������������� 221 Testing Spring Boot Applications ���������������������������������������������������������������������������������� 221 Testing with Mock Implementations ���������������������������������������������������������������������������� 225 Testing with Mockito �������������������������������������������������������������������������������������������������������������������������� 227
📄 Page 12
■ Contents xi Testing Slices of Application Using @*Test Annotations ���������������������������������������������� 230 Testing SpringMVC Controllers Using @WebMvcTest ������������������������������������������������������������������������� 231 Testing SpringMVC REST Controllers Using @WebMvcTest ��������������������������������������������������������������� 232 Testing Secured Controller/Service Methods ������������������������������������������������������������������������������������� 234 Testing Persistence Layer Components Using @DataJpaTest and @JdbcTest ���������������������������������� 241 Summary ���������������������������������������������������������������������������������������������������������������������� 246 ■Chapter 16: Creating a Custom Spring Boot Starter ������������������������������������������ 247 Introducing Twitter4j ���������������������������������������������������������������������������������������������������� 247 Custom Spring Boot Starter ����������������������������������������������������������������������������������������� 248 Create the twitter4j-spring-boot-autoconfigure Module �������������������������������������������������������������������� 249 Create the twitter4j-spring-boot-starter Module �������������������������������������������������������������������������������� 253 Application Using twitter4j-spring-boot-starter ����������������������������������������������������������� 255 Summary ���������������������������������������������������������������������������������������������������������������������� 257 ■Chapter 17: Spring Boot with Groovy, Scala, and Kotlin ������������������������������������ 259 Using Spring Boot with Groovy ������������������������������������������������������������������������������������� 259 Introducing Groovy ����������������������������������������������������������������������������������������������������������������������������� 259 Creating a Spring Boot Application Using Groovy ������������������������������������������������������������������������������� 262 Using Spring Boot with Scala ��������������������������������������������������������������������������������������� 266 Introducing Scala �������������������������������������������������������������������������������������������������������������������������������� 266 Creating a Spring Boot Application Using Scala ��������������������������������������������������������������������������������� 268 Using Spring Boot with Kotlin ��������������������������������������������������������������������������������������� 272 Introducing Kotlin ������������������������������������������������������������������������������������������������������������������������������� 272 Creating a Spring Boot Application Using Kotlin ��������������������������������������������������������������������������������� 273 Summary ���������������������������������������������������������������������������������������������������������������������� 278 ■Chapter 18: Introducing JHipster ���������������������������������������������������������������������� 279 Introducing JHipster ����������������������������������������������������������������������������������������������������� 279 Installing JHipster �������������������������������������������������������������������������������������������������������� 279 Prerequisites��������������������������������������������������������������������������������������������������������������������������������������� 280
📄 Page 13
■ Contents xii Creating a JHipster Application ������������������������������������������������������������������������������������ 280 Creating Entities ����������������������������������������������������������������������������������������������������������� 283 Using the JHipster Entity Sub-Generator �������������������������������������������������������������������������������������������� 284 Using JDL Studio �������������������������������������������������������������������������������������������������������������������������������� 284 Managing Relationships ����������������������������������������������������������������������������������������������� 285 Summary ���������������������������������������������������������������������������������������������������������������������� 287 ■Chapter 19: Deploying Spring Boot Applications ����������������������������������������������� 289 Running Spring Boot Applications in Production Mode ������������������������������������������������ 289 Deploying Spring Boot Application on Heroku �������������������������������������������������������������� 291 Running a Spring Boot Application on Docker �������������������������������������������������������������� 296 Installing Docker ��������������������������������������������������������������������������������������������������������������������������������� 296 Running a Spring Boot Application in a Docker Container ������������������������������������������������������������������ 297 Running Multiple Containers Using docker-compose ������������������������������������������������������������������������� 299 Summary ���������������������������������������������������������������������������������������������������������������������� 300 Index ��������������������������������������������������������������������������������������������������������������������� 301
📄 Page 14
xiii About the Author K. Siva Prasad Reddy has more than 11 years of experience in building enterprise software systems on the Java platform. He worked on building scalable distributed enterprise applications in banking and e-commerce domains using Java, Spring, RESTful web services, JPA, and NoSQL technologies. He is also the author of Java Persistence with Mybatis 3 and PrimeFaces Beginners Guide with other publishers. His current technical focus is on modern architectures, including microservices, continuous integration and continuous delivery (CI/CD), and DevOps. He enjoys coding in Java 8, Kotlin, and Spring Boot, and has a passion for automating repetitive work. He blogs regularly at http://sivalabs.in, or you can follow him on Twitter @sivalabs and GitHub https://github.com/sivaprasadreddy.
📄 Page 15
xv About the Technical Reviewer Massimo Nardone has more than 23 years of experience in security, web/mobile development, and cloud and IT architecture. His true IT passions are security and Android. He has been programming and teaching people how to program with Android, Perl, PHP, Java, VB, Python, C/C++, and MySQL for more than 20 years. He holds a Master of Science degree in Computing Science from the University of Salerno, Italy. He has worked as a project manager, software engineer, research engineer, chief security architect, information security manager, PCI/SCADA auditor, and senior lead IT security/cloud/SCADA architect for many years. His technical skills include security, Android, cloud, Java, MySQL, Drupal, Cobol, Perl, web and mobile development, MongoDB, D3, Joomla, Couchbase, C/C++, WebGL, Python, Pro Rails, Django CMS, Jekyll, Scratch, etc. He worked as visiting lecturer and supervisor for exercises at the Networking Laboratory of the Helsinki University of Technology (Aalto University). He also holds four international patents (in the PKI, SIP, SAML, and Proxy areas). He currently works as a Chief Information Security Officer (CISO) for CargotecOyj and is member of ISACA Finland chapter board. Massimo has reviewed more than 40 IT books for different publishers and is the coauthor of Pro Android Games (Apress, 2015).
📄 Page 16
xvii Acknowledgments I would like to thank my wife Neha Jain and my family members for their continuous support all the days I spent writing this book. I would like to express my gratitude to the Apress team, specifically to Steve Anglin and Mark Powers, for their continuous support throughout the journey. I would also like to thank the reviewers for providing valuable feedback that helped improve the quality of the content.
📄 Page 17
xix Introduction Spring is the most popular Java-based framework for building enterprise applications. The Spring framework provides a rich ecosystem of projects to address modern application needs, like security, simplified access to relational and NoSQL datastores, batch processing, integration with social networking sites, large volume of data streams processing, etc. As Spring is a very flexible and customizable framework, there are usually multiple ways to configure the application. Although it is a good thing to have multiple options, it can be overwhelming to the beginners. Spring Boot addresses this “Spring applications need complex configuration” problem by using its powerful autoconfiguration mechanism. Spring Boot is an opinionated framework following the “Convention Over Configuration” approach, which helps build Spring-based applications quickly and easily. The main goal of Spring Boot is to quickly create Spring-based applications without requiring the developers to write the same boilerplate configuration again and again. In recent years, the microservices architecture has become the preferred architecture style for building complex enterprise applications. Spring Boot is a great choice for building microservices-based applications using various Spring Cloud modules. This book will help you understand what Spring Boot is, how Spring Boot helps you build Spring-based applications quickly and easily, and the inner workings of Spring Boot using easy-to-follow examples. What This Book Covers This book covers the following topics: • What is Spring Boot and how does it improve developer productivity? • How does Spring Boot autoconfiguration work behind the scenes? • How do you create custom Spring Boot starters? • Working with databases using JdbcTemplate, MyBatis, JOOQ, and Spring Data JPA • Working with the MongoDB NoSQL database • Developing web applications using Spring Boot and Thymeleaf • Developing Reactive Web Applications using Spring WebFlux • Developing REST API using Spring Boot • Securing web applications using SpringSecurity • Monitoring Spring Boot applications with Spring Boot Actuator • Testing Spring Boot applications • Developing Spring Boot applications in Groovy, Scala, and Kotlin • Running Spring Boot applications in the Docker container
📄 Page 18
■ IntroduCtIon xx What You Need for This Book To follow the examples in this book, you must have the following software installed: • JDK 1.8 • Your favorite IDE • Spring Tool Suite • IntelliJ IDEA • NetBeans IDE • Build tools • Maven • Gradle • Database server • MySQL • PostgreSQL
📄 Page 19
1© K. Siva Prasad Reddy 2017 K. S. Prasad Reddy, Beginning Spring Boot 2, DOI 10.1007/978-1-4842-2931-6_1 CHAPTER 1 Introduction to Spring Boot The Spring framework is a very popular and widely used Java framework for building web and enterprise applications. Spring at its core is a dependency injection container that provides flexibility to configure beans in multiple ways, such as XML, Annotations, and JavaConfig. Over the years, the Spring framework grew exponentially by addressing the needs of modern business applications like security, support for NoSQL datastores, handling big data, batch processing, integration with other systems, etc. Spring, along with its sub-projects, became a viable platform for building enterprise applications. The Spring framework is very flexible and provides multiple ways of configuring the application components. With a rich set of features combined with multiple configuration options, configuring Spring applications become complex and error-prone. The Spring team created Spring Boot to address the complexity of configuration through its powerful AutoConfiguration mechanism. This chapter takes a quick look at the Spring framework. You’ll develop a web application using SpringMVC and JPA the traditional way (without using Spring Boot). Then you will look at the pain points of the traditional way and see how to develop the same application using Spring Boot. Overview of the Spring Framework If you are a Java developer, then there is a good chance that you have heard about the Spring framework and have used it in your projects. The Spring framework was created primarily as a dependency injection container, but it is much more than that. Spring is very popular for several reasons: • Spring’s dependency injection approach encourages writing testable code • Easy-to-use and powerful database transaction management capabilities • Spring simplifies integration with other Java frameworks, like the JPA/Hibernate ORM and Struts/JSF web frameworks • State-of-the-art Web MVC framework for building web applications Along with the Spring framework, there are many other Spring sub-projects that help build applications that address modern business needs: • Spring Data: Simplifies data access from relational and NoSQL datastores. • Spring Batch: Provides a powerful batch-processing framework. • Spring Security: Robust security framework to secure applications.
📄 Page 20
Chapter 1 ■ IntroduCtIon to SprIng Boot 2 • Spring Social: Supports integration with social networking sites like Facebook, Twitter, LinkedIn, GitHub, etc. • Spring Integration: An implementation of enterprise integration patterns to facilitate integration with other enterprise applications using lightweight messaging and declarative adapters. There are many other interesting projects addressing various other modern application development needs. For more information, take a look at http://spring.io/projects. Spring Configuration Styles Spring initially provided an XML-based approach for configuring beans. Later Spring introduced XML-based DSLs, Annotations, and JavaConfig-based approaches for configuring beans. Listings 1-1 through 1-3 show how each of those configuration styles looks. Listing 1-1. Example of XML-Based Configuration <bean id="userService" class="com.apress.myapp.service.UserService"> <property name="userDao" ref="userDao"/> </bean> <bean id="userDao" class="com.apress.myapp.dao.JdbcUserDao"> <property name="dataSource" ref="dataSource"/> </bean> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy- method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/test"/> <property name="username" value="root"/> <property name="password" value="secret"/> </bean> <!-- DSL based configuration --> <beans> <jee:jndi-lookup id="entityManagerFactory" jndi-name="persistence/defaultPU"/> </beans> Listing 1-2. Example of Annotation-Based Configuration @Service public class UserService { private UserDao userDao; @Autowired public UserService(UserDao dao){ this.userDao = dao; } ... ... }
The above is a preview of the first 20 pages. Register to read the complete e-book.

💝 Support Author

0.00
Total Amount (¥)
0
Donation Count

Login to support the author

Login Now
Back to List