Introducing Spring Framework 6 Learning and Building Java-based Applications With Spring (Felipe Gutierrez, Joseph B. Ottinger) (Z-Library)

Author: Felipe Gutierrez, Joseph B. Ottinger

技术

Spring Framework 6 remains - by far - the leading de-facto "out of the box" practical Java meta application development framework for building complex enterprise, cloud-native applications as well as web applications and microservices. Introducing Spring Framework 6 is your hands-on tutorial guide for learning the Spring Framework 6 from top to bottom, and allows you to build an example application along the way from the ground-up. As you learn the Spring Framework over the course of this book, you’ll incrementally build your first Spring application piece-by-piece as you learn each module, project or component of the Spring Framework and its extensions and ecosystem. As you learn the various fundamentals, you'll then apply them immediately to your Spring application. This Spring application, My Documents, enables you to learn by doing. After reading this book, you will have the essentials you should need to start using the Spring Framework and building your own Java-based applications or microservices with it. What you'll learn: Get started with Spring Framework 6 by VMWare Tanzu and the Spring community Build your first My Documents application using Spring Framework and its extensions Test your Spring application Add persistence to your application using Spring Data JPA and more Show your Spring application on the Web with Spring MVC and related Use REST APIs to enhance your application and add messaging with Kafka and AMQP Integrate your Spring application with external systems using Spring Integration toolkit Who is this book for: This book is for those aspiring software developers and programmers who are new to Spring. Some prior programming experience recommended, preferably in Java.

📄 File Format: PDF
💾 File Size: 2.8 MB
49
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
Introducing Spring Framework 6 Learning and Building Java-based Applications With Spring — Second Edition — Felipe Gutierrez Joseph B. Ottinger
📄 Page 2
Introducing Spring Framework 6 Learning and Building Java-based Applications With Spring Second Edition Felipe Gutierrez Joseph B. Ottinger
📄 Page 3
Introducing Spring Framework 6: Learning and Building Java-based Applications With Spring ISBN-13 (pbk): 978-1-4842-8636-4 ISBN-13 (electronic): 978-1-4842-8637-1 https://doi.org/10.1007/978-1-4842-8637-1 Copyright © 2022 by Felipe Gutierrez, Joseph B. Ottinger 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. Managing Director, Apress Media LLC: Welmoed Spahr Acquisitions Editor: Steve Anglin Development Editor: Laura Berendson Coordinating Editor: Jill Balzano Cover designed by eStudioCalamar Cover image by Mink Mingle on Unsplash (www.unsplash.com) Distributed to the book trade worldwide by Apress Media, LLC, 1 New York Plaza, New York, NY 10004, U.S.A. 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 booktranslations@springernature.com; for reprint, paperback, or audio rights, please e-mail bookpermissions@springernature.com. 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 (https://github.com/Apress). For more detailed information, please visit http://www. apress.com/source-code. Printed on acid-free paper Felipe Gutierrez Cary, NC, USA Joseph B. Ottinger YOUNGSVILLE, NC, USA
📄 Page 4
To my parents, Rocio Cruz and Felipe Gutierrez. —Felipe Gutierrez To my beloved wife and sons, and to the rabbits that keep hounding my yard. —Joseph B. Ottinger
📄 Page 5
v Table of Contents About the Authors ���������������������������������������������������������������������������������������������������� ix About the Technical Reviewer ��������������������������������������������������������������������������������� xi Acknowledgments ������������������������������������������������������������������������������������������������� xiii Introduction �������������������������������������������������������������������������������������������������������������xv Part I: Spring Framework Basics �������������������������������������������������������������������� 1 Chapter 1: Your First Spring Application ������������������������������������������������������������������ 3 Pre-requirements �������������������������������������������������������������������������������������������������������������������������� 4 Source Code Organization ������������������������������������������������������������������������������������������������������������� 5 Hello World Example ������������������������������������������������������������������������������������������������������������������� 10 Hello, Boot ����������������������������������������������������������������������������������������������������������������������������������� 14 Hello, Kotlin ��������������������������������������������������������������������������������������������������������������������������������� 18 Summary������������������������������������������������������������������������������������������������������������������������������������� 21 Chapter 2: Working with Classes and Dependencies ��������������������������������������������� 23 The “My Documents” Application ����������������������������������������������������������������������������������������������� 23 Testing the Implementation �������������������������������������������������������������������������������������������������������� 28 Testing with Spring ��������������������������������������������������������������������������������������������������������������������� 32 Summary������������������������������������������������������������������������������������������������������������������������������������� 35 Chapter 3: Applying Different Configurations ��������������������������������������������������������� 37 Testing My Documents ���������������������������������������������������������������������������������������������������������������� 37 Annotation Configuration in Spring ��������������������������������������������������������������������������������������������� 45 Component Scanning ������������������������������������������������������������������������������������������������������������ 47 XML Configuration in Spring ������������������������������������������������������������������������������������������������������� 50 Expanding the Configuration ������������������������������������������������������������������������������������������������� 53
📄 Page 6
vi Component Scanning in XML ������������������������������������������������������������������������������������������������ 58 Is XML Configuration a Good Idea? ���������������������������������������������������������������������������������������� 59 Choosing a Configuration Approach �������������������������������������������������������������������������������������������� 60 Summary������������������������������������������������������������������������������������������������������������������������������������� 61 Chapter 4: Using Bean Scopes �������������������������������������������������������������������������������� 63 Scope ������������������������������������������������������������������������������������������������������������������������������������������ 63 The Scopes ��������������������������������������������������������������������������������������������������������������������������������� 63 Using the Scopes ������������������������������������������������������������������������������������������������������������������������ 65 Annotations ��������������������������������������������������������������������������������������������������������������������������������� 73 Summary������������������������������������������������������������������������������������������������������������������������������������� 74 Chapter 5: Using Resource Files ����������������������������������������������������������������������������� 75 Injecting a Resource ������������������������������������������������������������������������������������������������������������������� 75 Loading Injected Values from Property Files ������������������������������������������������������������������������������� 78 Internationalization ���������������������������������������������������������������������������������������������������������������� 81 Summary������������������������������������������������������������������������������������������������������������������������������������� 85 Part II: The Spring Framework ��������������������������������������������������������������������� 87 Chapter 6: Adding Simple Persistence to Your Spring Application ������������������������ 89 Persistence As a Concept������������������������������������������������������������������������������������������������������������ 89 Revisiting Our Simple Data Model ���������������������������������������������������������������������������������������������� 90 Choosing a Database ������������������������������������������������������������������������������������������������������������������ 93 Setting Up a JDBC Connection ���������������������������������������������������������������������������������������������������� 94 The JDBCTemplate �������������������������������������������������������������������������������������������������������������������� 99 Our Service Interfaces and the SearchEngine Implementation ������������������������������������������������ 100 Tying It All Together ������������������������������������������������������������������������������������������������������������������� 104 Summary����������������������������������������������������������������������������������������������������������������������������������� 106 Chapter 7: Letting Spring Build Your Data Access Objects ����������������������������������� 107 The Project �������������������������������������������������������������������������������������������������������������������������������� 107 Spring Data Repositories ���������������������������������������������������������������������������������������������������������� 113 Summary����������������������������������������������������������������������������������������������������������������������������������� 118 Table of ConTenTs
📄 Page 7
vii Chapter 8: Showing Your Spring Application on the Web ������������������������������������� 119 Thymeleaf ��������������������������������������������������������������������������������������������������������������������������������� 124 Tying It All Together ������������������������������������������������������������������������������������������������������������������� 128 Summary����������������������������������������������������������������������������������������������������������������������������������� 132 Part III: Advanced Techniques with Spring Framework ����������������������������� 133 Chapter 9: Integrating Your Spring Application with External Systems ��������������� 135 The Process ������������������������������������������������������������������������������������������������������������������������������ 136 Summary����������������������������������������������������������������������������������������������������������������������������������� 146 Chapter 10: Exposing a REST API ������������������������������������������������������������������������� 147 What Is REST? ��������������������������������������������������������������������������������������������������������������������������� 147 Building a REST API in Spring ��������������������������������������������������������������������������������������������������� 148 Summary����������������������������������������������������������������������������������������������������������������������������������� 164 Chapter 11: Sending Emails from Within Spring �������������������������������������������������� 165 Sending Email ��������������������������������������������������������������������������������������������������������������������������� 165 Set Up MailTrap ������������������������������������������������������������������������������������������������������������������� 166 The Email Aspect of the Project ������������������������������������������������������������������������������������������� 168 Asynchronous Tasks in Spring �������������������������������������������������������������������������������������������������� 180 Adding Scheduling Events in Spring ����������������������������������������������������������������������������������������� 182 Summary����������������������������������������������������������������������������������������������������������������������������������� 190 Part IV: The New Spring I/O ������������������������������������������������������������������������ 191 Chapter 12: Using Dynamic Languages ���������������������������������������������������������������� 193 Loading Functionality Dynamically with Groovy ����������������������������������������������������������������������� 194 The Simplest Dynamic MessageService ����������������������������������������������������������������������������� 196 Using Spring to Configure the Dynamic MessageService ��������������������������������������������������� 200 Inline Dynamic Content�������������������������������������������������������������������������������������������������������� 202 Summary����������������������������������������������������������������������������������������������������������������������������������� 204 Table of ConTenTs
📄 Page 8
viii Chapter 13: Where Do You Go From Here? ������������������������������������������������������������ 205 Spring and the Impact on Development ������������������������������������������������������������������������������������ 205 The Wider World of Spring �������������������������������������������������������������������������������������������������������� 207 Index ��������������������������������������������������������������������������������������������������������������������� 209 Table of ConTenTs
📄 Page 9
ix About the Authors Felipe Gutierrez is a solutions software architect, with a bachelor’s and a master’s degree in computer science from Instituto Tecnologico y de Estudios Superiores de Monterrey Campus Ciudad de Mexico, with over 20 years of IT experience, during which time he developed programs for companies in multiple vertical industries, such as government, retail, healthcare, education, and banking. He is currently working as a principal technical instructor for Pivotal, specializing in Cloud Foundry, Spring Framework, Spring Cloud Native Applications, Groovy, and RabbitMQ, among other technologies. He has worked as a solutions architect for big companies like Nokia, Apple, Redbox, and Qualcomm, among others. He is also the author of Introducing Spring Framework, Pro Spring Boot, and Spring Boot Messaging, all published by Apress. Joseph B. Ottinger is reputedly an expert software developer, coder, and programmer with experience covering many technologies and platforms. He was the Editor in Chief for both Java Developer Journal and TheServerSide.com and has contributed to a large number of publications, open source projects, and commercial products over the years, using many different languages (but primarily Java, Python, and JavaScript). He is the author of Hibernate Recipes and Beginning Hibernate for Apress and has authored other books as well as a few articles here and there. He’s also constantly writing odes to random furry creatures grooving in caves with members of ancient cultures, or something.
📄 Page 10
xi About the Technical Reviewer Manuel Jordan Elera is an autodidactic developer and researcher who enjoys learning new technologies for his own experiments and creating new integrations. Manuel won the Springy Award 2013 Community Champion and Spring Champion. In his little free time, he reads the Bible and composes music on his guitar. Manuel is known as dr_pompeii. He has tech-reviewed numerous books, including Pro Spring MVC with WebFlux (Apress, 2020), Pro Spring Boot 2 (Apress, 2019), Rapid Java Persistence and Microservices (Apress, 2019), Java Language Features (Apress, 2018), Spring Boot 2 Recipes (Apress, 2018), and Java APIs, Extensions and Libraries (Apress, 2018). You can read his detailed tutorials on Spring technologies and contact him through his blog at www.manueljordanelera.blogspot.com. You can follow Manuel on his Twitter account, @dr_pompeii.
📄 Page 11
xiii Acknowledgments I would like to express all my gratitude to the Apress team: first and foremost to Steve Anglin for accepting my proposal; Laura Berendson and Jill Balzaono for helping me out when I needed it; and the rest of the Apress team involved in this project. Thanks to everybody for making this possible. Thanks to our technical reviewer, Manuel Jordan, and the entire Spring team for making the Spring Framework the best programming and configuration model for modern Java-based enterprise applications. Thanks to my parents, Rocio Cruz and Felipe Gutierrez, for all their love and support, and to my best friend, my brother Edgar Gerardo Gutierrez. Even though we live far away, we are closer than ever; thanks, “macnitous.” —Felipe Gutierrez I’m constantly amazed that I’m asked to write these acknowledgments and dedications, mostly because they feel so limiting to point out only specific persons and events to whom I’m grateful. I would like to thank both grammar and spelling, and my sense of restraint in using both proper grammar and spelling in doing so, as well as the patience of my wife and family (and friends), without whom I’d hardly ever be able to do anything. This book was written to the tune of many classic ’70s tunes, many of which were cheesy soft rock for some reason, and I’d like to thank a whole host of one-hit- wonders without which I’d never wonder what the record industry was thinking back then. It’s awesome, and I miss it. May all of us live with as much love in our hearts for each other as we can stand. —Joseph B. Ottinger
📄 Page 12
xv Introduction This book is an introduction to the well-known Spring Framework that offers an inversion of control container for the Java platform. The Spring Framework is an open source application framework that can be used with any Java application. After reading this book, you will know how to do the following: • Use the Spring Framework efficiently. • Add persistence through JDBC databases, with an easy migration to NoSQL. • Do unit and integration testing. • Create web applications and expose RESTful APIs. • Send messages via JMS, a model that extends to AMQP, RabbitMQ, and MQTT. • Use dynamic languages like Groovy, Ruby, and Bean Shell for Spring. • Use Groovy with Spring. • Use the new Spring Boot and Spring XD technologies. Who This Book Is For Introducing Spring Framework 6 is a hands-on guide for any developer who is new to the Spring Framework and wants to learn how to build applications with it. Within this book you will find all the necessary elements to create enterprise-ready applications by using the Spring Framework and all its features and modules.
📄 Page 13
xvi How This Book Is Organized This book uses a simple My Documents application that you will develop incrementally over the course of the book. The book consists of the following four parts: • Part 1: Spring Framework Basics: You will learn about the dependency injection design pattern, and Spring’s container implementation and how it will help you create a better design by programming toward interfaces. You’ll learn the different configurations that you can apply to the Spring Framework. You will also learn how to use bean scopes, work with collections and resource files, and how to test your Spring applications. • Part 2: The Spring Framework: You will learn to add persistence and integrate your Spring application with other systems. And you will be able to add your Spring application to the Web. • Part 3: Advanced Techniques with Spring Framework: You will learn how to use message brokers with Spring for massive scalability and distributed architecture, how to expose your application using a RESTful API, and how to send emails and schedule events in your application. • Part 4: The New Spring I/O: You will learn how to integrate Spring and Groovy into your Spring application. You’ll also get pointers on where to look for further technologies in your journey with Spring. Source Code All source code used in this book can be found at github.com/apress/introducing- spring- framework6. Included in this download will be the following online-only appendixes: • Installing Java and Gradle for your Operating System • Other Recommended Tools With all that said, let’s go ahead and start with the Spring Framework! InTroduCTIon
📄 Page 14
PART I Spring Framework Basics
📄 Page 15
3 CHAPTER 1 Your First Spring Application Most books start with a very long explanation about the technology they are using, the history of it, and often a small example that you can’t run until you reach later chapters. In this book, I am going to take a different approach. I am going to start with some basic examples, and I will explain in detail what they do and how to use them so that you get to know Spring quickly. The examples in this chapter will show you how easy it is to integrate the Spring Framework into any existing project or how to start one from scratch and modify it without any effort. Figure 1-1 shows the Spring portfolio website (https://spring.io). In this website, you can find all of the Spring Extensions, guides, and documentation to help you understand better the Spring ecosystem. The Spring Framework itself can be found at https://spring.io/projects/spring- framework, if you’re interested in drilling down already. © Felipe Gutierrez, Joseph B. Ottinger 2022 F. Gutierrez and J. B. Ottinger, Introducing Spring Framework 6, https://doi.org/10.1007/978-1-4842-8637-1_1
📄 Page 16
4 Figure 1-1. https://spring.io Pre-requirements In order to start with the first Spring Framework example, we need to have some tools installed. • You need the Java Development Kit (JDK) installed and configured, accessible on the command line. Unlike previous versions of Spring, which supported a wide range of Java versions, Spring 6 requires Java 17 or higher. There are many sites that provide Java 17, but the most appropriate will be either Adoptium’s distribution page, at https:// adoptium.net/releases.html, or Oracle’s site, at www.oracle.com/ java/technologies/downloads/, either of which are appropriate. You can also install Java via SDKMan! if you’re on OSX (see https:// sdkman.io). Chapter 1 Your First spring appliCation
📄 Page 17
5 • We’re going to use the Gradle Build Tool, found at https://gradle.org, for the book (See Figure 1-2). Gradle is one of the two most popular and capable build tools for the JVM; choosing Gradle here is done mostly because it has very simple and short build scripts. We use a build tool because it’s very much standard practice and gives us easy dependency management and a full build lifecycle, including the ability to run tests as part of our build; Gradle is also the tool of choice of the Spring team itself. Source Code Organization We’re going to be looking at a lot of source code in this book. This part will describe how it’s laid out so it’s easy to understand what is going where. Figure 1-2. The Gradle Website Chapter 1 Your First spring appliCation
📄 Page 18
6 The book is going to be organized as a Gradle project, with various subdirectories.1 Most of the subdirectories will be “modules” of the project, although there will be exceptions. The top-level project needs its own directory; for the authors, it’s the isf6 directory (for “Introducing Spring 6”). Most of the chapters' code will be in a clearly named directory mapping to the chapter number: thus, chapter02, chapter03, and so forth. If a chapter has two separate projects, they’ll be named with the chapter and then a description of the project, like chapter01-hello-world and chapter01-hello-boot. Let’s take a look at the “top-level” project and set it up, and then we’ll get to writing some actual code that uses Spring. The first thing we need to do, after installing Java and Gradle, is run a command to initialize Gradle, gradle init. That will give us a short interaction where Gradle asks us what build options we want. The defaults are fine, because we’ll be overwriting them; the main result we want from this process is the setup of the Gradle wrapper. We want to replace two files in this directory, build.gradle and settings.gradle. The top-level build.gradle file – in your “project working directory,” so all relative paths start with “.” – should look like this: Listing 1-1. build.gradle apply plugin: 'java' sourceCompatibility = 17 targetCompatibility = 17 ext { springFrameworkVersion = "6.0.0-M4" testNgVersion = "7.6.1" } 1 Gradle uses a wrapper that downloads a copy of Gradle for each project. This is very useful for repeatable builds, but it creates a copy of Gradle on your hard drive for each project. As a result, it’s far more efficient for us to have a single project for the book, rather than a project for each chapter, although some chapters will have separate projects, such as the chapter-01-hello-boot project. Chapter 1 Your First spring appliCation
📄 Page 19
7 allprojects { apply plugin: 'java' repositories { maven { url "https://repo.spring.io/milestone" } maven { url "https://repo.spring.io/snapshot" mavenContent { snapshotsOnly() } } mavenCentral() } dependencies { implementation \ "ch.qos.logback:logback-classic:1.2.11" testImplementation \ "org.testng:testng:$testNgVersion" } test { useTestNG() } } This file looks like a lot – but it’s also one of the longer build files we’ll have. What it’s doing is telling Gradle that we have a Java project (therefore, use the Java compiler), what level of Java to require (17, the current long-term support release as of writing and a requirement for Spring 6), a set of properties to provide version information to the build, and then a set of things that all projects should do: they’re all Java projects, they all should pull dependencies from a set of external locations, and they should all rely on the TestNG testing framework. The next file we’ll want to overwrite is the settings.gradle file. Chapter 1 Your First spring appliCation
📄 Page 20
8 What this one does is describe the subprojects to include in the “main build.” This one will change as we develop the content in the book. Our settings.gradle needs to do two things: 1. Configure our project so it can find the plug-ins we’ll want. 2. Configure the submodules for our book’s content. As this book is being written, Spring 6 has not been released. As a result, we need to use repositories that aren’t “official releases.” We already see the references to milestone and snapshot repositories in our build.gradle, but Gradle resolves dependencies and plug-ins through different mechanisms; thus, we need to configure them separately. After the preamble for plug-in resolution – the bit that starts with pluginManagement – we have a simple way to name the project as a whole (isf6), and then we include our first subproject, the chapter01-hello-spring module. We’ll be adding to this as we proceed through the book.2 We’re going to start off with a simple “Hello, world” project, with a project name of chapter01-hello-world, so our settings.gradle file will be simple to start off with. Listing 1-2. settings.gradle pluginManagement { repositories { maven { url "https://repo.spring.io/milestone" } maven { url "https://repo.spring.io/snapshot" mavenContent { snapshotsOnly() } } gradlePluginPortal() } } 2 If you’re looking at the source download, you’ll see that the settings.gradle has a lot more to it and starts with a //tag::chapter01[] comment. These comments are to help format the book, which used the actual source code for listings instead of copied source. Chapter 1 Your First spring appliCation
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