Preface
What Is Spring Boot?
Spring Boot, the leading Java enterprise framework, provides numerous benefits to the developer community. It's user-friendly, simplifies microservices development, and, when developed correctly, can achieve scalability, making it ideal for building robust enterprise applications.
This book covers all the essential features of the widely used Java framework, up to its latest major release, Spring Boot 3, which was launched in November 2022. It has been developed over several years to offer a comprehensive overview of the framework.
Prior Knowledge and Target Group
As a widely adopted framework in the Java community, Spring has evolved into a de facto standard in recent years. This book is tailored for individuals interested in creating enterprise applications that incorporate database connectivity and web services. It caters to both novices and seasoned developers, with a plethora of practical examples included. However, a solid grasp of Java and proficiency in tools such as Maven are required prerequisites, along with a fundamental understanding of relational databases.
Date4u Demo Application, Tasks, and Solutions
This book, in conjunction with a reliable development environment, equips you with the skills to create Spring programs. However, simply reading about a new framework is insufficient to become proficient. To learn a programming language or framework thoroughly, you must practice and become fluent, treating it like a foreign language where the Spring framework is the vocabulary.
To illustrate the central components of an enterprise application, this book employs an amusing example of a dating application for unicorns. The demonstration showcases the interaction of components, storage of profile data in a database, and access via web services. You are guided through each step of the application with tasks scattered throughout the text for practice. Each task is accompanied by a suggested solution to facilitate sequential reading. The Date4u application, which exemplifies the concepts discussed in the book, is downloadable at https://rheinwerk-computing.com/5764/.
Chapter Organization
The book is divided into the following chapters:
Chapter 1, "Introduction," introduces the Spring Framework and the "extension" Spring Boot. The chapter first shows how to create a project and then explains the structure of the Maven project object model (POM) file, starters and dependencies, and configuration and logging.
Chapter 2, "Containers for Spring-Managed Beans," focuses on the Spring context. Step by step, a mesh of Spring-managed beans is built from a single class to store and load images in the file system.
Managing Spring-managed beans is a core task of the Spring framework, but there are many other features addressed in Chapter 3, "Selected Modules of the Spring Framework," including external configuration, event handling, conversion classes, and various utility classes.
Chapter 4, "Selected Proxies," continues with a discussion of special "rings" that wrap around components and thus take over caching, asynchronous calls, or validation. This offers the advantage that certain tasks can be shifted to the framework so that your code becomes slenderer.
The first four chapters focus on the Spring container and helper classes, but not on storage technologies. That changes in Chapter 5, "Connecting to Relational Databases," in which a database management system is prepared and SQL queries to the database are demonstrated.
Writing to databases using the Java Database Connectivity (JDBC) application programming interface (API) is simple and direct, but inconvenient. Therefore, Chapter 6, "Jakarta Persistence with Spring," introduces object-relational mapping.
Data access layers are necessary in every major application. Chapter 7, "Spring Data JPA," shows the possible uses of data access layers by using the family member Spring Data JPA as an example. It's easy to write repositories with Spring Data, and many queries and data transformations are realized by the framework.
Relational databases are flanked by nonrelational database systems, which are also excellently integrated into the Spring Data family. Chapter 8, "Spring Data for NoSQL Databases," uses MongoDB and Elasticsearch to show how Spring Data concepts translate to the NoSQL world.
After describing an important part of the infrastructure with data storage, we continue with applications of HTTP in Chapter 9, "Spring Web." There, the focus is on RESTful web services and HTTP clients.
With the knowledge acquired so far, you could operate an application, but it's important to also be able to look into the application from the outside to check whether the "operating temperature" is right. Chapter 10, "Logging and Monitoring," introduces the Spring Boot Actuator project as a way of releasing data from an application that can then be retrieved and visualized by external tools.
The fact that a program runs in the development environment and doesn't fail in the test cases is good, but it's only half the battle because a program must be rolled out. For this purpose, Chapter 11, "Build and Deployment," introduces Open Container Initiative (OCI) containers (aka Docker), among other things.
Spring Boot 3 is new, and it will be a while before teams move to Java 17. Those developing applications on Java 8 will find some tips on migrating an application from Spring Boot 2 to Spring Boot 3 in Appendix A, "Migrating from Spring Boot 2 to Spring Boot 3."
The * Sections
The Spring framework encompasses many intricacies that can be overwhelming for beginners and novices. To help distinguish between essential and nonessential information, some section headings are marked with an asterisk (*), indicating that they can be skipped without sacrificing important concepts required for subsequent chapters.
Which Java Version Is Used in the Book?
Spring Boot 3 necessitates Java 17, which comes with long-term support. This implies that runtime vendors provide extensive support for many years to ensure that the release remains up to date for an extended period. Nevertheless, Spring Boot 3 can also work with more recent versions of Java, such as Java 21.
Required Software
To execute Java programs, a Java virtual machine (JVM) is required. Initially, obtaining a runtime environment was a simple task. The runtime environment originated from Sun Microsystems and was subsequently adopted by Oracle after acquiring Sun. However, the present situation is much more perplexing. Several institutions compile the OpenJDK, which comprises hundreds of thousands of lines of C(++) and Java source code, and package it into a distribution known as the Java runtime environment. The most well-known Java runtime environments include the following:
There are others as well, such as those from Azul Systems or BellSoft. To learn with this book, you can use a distribution of your choice; if you're not sure, you'll do fine with Adoptium.
Development Environment
Java source code is just text, so in principle, a simple text editor is enough. However, you can't expect great productivity with an editor such as Notepad. Because modern development environments support us in many ways-color highlighting of keywords, automatic code completion, intelligent error correction, inserting code blocks, visualization of states in the debugger, and many more-it's advisable to use a full integrated development environment (IDE). Three popular IDEs are IntelliJ, Eclipse, and Visual Studio Code. On the other hand, Apache NetBeans is losing more and more ground.
As with the Java runtime environments, you can choose which development environment you want to use. Eclipse, NetBeans, and Visual Studio Code are free and open source. IntelliJ IDEA Community Edition is also free and open source, but the more powerful IntelliJ IDEA Ultimate Edition is not. The Ultimate version of IntelliJ is certainly the most powerful Java IDE on the market, and its support for Spring is exemplary and unmatched by other IDEs.
Conventions
...