CHAPTER 1
Getting Started with Spring Boot
As the web application development has changed from Java Server Pages (JSPs), Servlets to Spring, many problems of the boiler plate code have reduced. Spring has really reduced the boilerplate code to an extent. The Spring team has developed Spring Boot on the top of the Spring framework that eliminated boilerplate configurations required for Spring applications. This chapter will introduce you to the Spring Boot Framework with the latest version 2.4.3. It will cover the basics of setting up the Spring Boot workspace with tools like JDK 8, Spring Tool Suite (STS), Spring Initializr, and Apache Maven and Gradle as build tools.
Structure
In this chapter, we will discuss the following topics:
- Introduction to Spring Boot
- Features of Spring Boot
- Advantages of Spring Boot
- Breaking the monolithic way of developing software
- When to start using microservices?
- When not to start using microservices?
- System requirements
- Setting up the environment
- The 12-factor app
- Spring Initializr
Objectives
After studying this unit, you should be able to understand the concept of Spring Boot. You can set up your development environment and learn the 12-factor app that an application should have.
Introduction to Spring Boot
On October 17, 2012, Mike Youngstrom opened a JIRA ticket with the Spring framework team asking for the support for container less web application architectures. As the developers would be more interested in adopting a simpler framework, there should be a mechanism that would allow the developers not to remember both configurations for the Spring model as well as the servlet container on which they are executing their application. There were few items within the older architecture of Spring core that was configured in an inconsistent non-unified way that the developers have to first learn the servlet container on which they are going to deploy in addition to the Spring’s own configuration model.
He proposed an idea for having the servlet container and related tools as a part of the Spring component by embedding and unifying the configuration of those common web container services with the Spring container that can be bootstrapped from the main() method.
This issue was addressed by Phil Webb and from there the Spring Boot started evolving in 2013. On April 1, 2014, the first Spring Boot 1.0 Globally Available (GA) was released which addressed the preceding concern in addition to the issues.
Spring Boot is now an open-source Java-based framework used to create standalone microservices with production-ready features. It is heavily maintained by the Pivotal Team. Microservices is an architectural design that creates scalable, loosely coupled, and testable applications which have a single function module with well-defined interfaces. The microservices hence created by using Spring Boot can be owned and maintained by a small team; unlike it used to be while creating APIs by older technologies like Java web services that required a team of a larger size. This microservice design is adapted by many enterprises in recent years as they look to have their software development delivery in the Agile manner where they can continuously develop, test, and deliver.
Features of Spring Boot
Why the Spring team developed Spring Boot? How is it beneficial for developers to build their application on Spring Boot and not Spring? How are configurations managed? All features of Spring Boot would be explained in this and upcoming chapters. Few of them are listed as follows:
- Faster way of developing applications by reducing the boilerplate configurations.
- Loosely coupled dependencies.
- Starter packs available as part of dependency for simplifying builds and configuration.
- Creating production-ready microservices with actuator health endpoints such as
health, httptrace, mappings, metrics, info, configprops, env, and so on. - Embedded container server support. By default, Tomcat is used.
- Auto-configuration for dependencies; once the dependency is loaded into the class path, Spring Boot manages the class instantiations, when needed.
- Externalized configurations with the help of
.properties and .yml files and Spring Cloud Config. - Live reload for the application during the development phase.
- Ability to exclude/change the dependency version before deployment.
Advantages of Spring Boot
Spring Boot helps you to create stand-alone Spring-based applications with production-ready features that you can execute on the local workspace and the cloud platform which has the Java Virtual Machine (JVM) installed. By using Spring Boot as the development platform, you can get started with the creation of an application with minimum code setup with less development time, so that the focus will be in contract with the features that you want to imply in the application. This allows the developer to focus on the idea that evolves during the initial phase of the requirement and then it is the magic of Spring Boot that helps you to bootstrap your application, its external dependencies on the resources like accessing the database and managing the external calls to other applications that consume the data in the prescribed format. Mostly, Spring Boot applications need less Spring configuration.
After you build your Spring Boot application, it can be executed by java –jar <jar name> just like any other Java project that builds a jar file. The Spring Boot application can also be started by running the mvn spring-boot:run command on the root directory which contains the src folder.
Breaking the monolithic way of developing software
Since years the software development used to happen in a monolithic architecture. The developer or the business analyst or even the product owner of the software receives a big requirement at once and then after getting the details of the requirements, the team starts to work on. This leads to the waterfall model of developing a software which is good to release a Minimal Viable Product (MVP) that is a single release to showcase about what the product is. This single release is created in a single application containing all codes to resolve the user problem. Problem arises when the requirements are portrayed in such a way that the code already developed is duplicated to fulfill another requirement, that is, chunks of code can be repeated which leads to bad quality of code.
Now imagine for that requirement, all we need to repeat all sorts of regression and stress testing which was already done previously before releasing the product. Why should all codes be tested even if that single enhancement may not be related to test the whole application?
Since then, business capabilities are delivered using microservices. Now, selecting the microservice architecture over the monolithic architecture can have many reasons and may not be limited to the features of microservices as follows:
- Each service can have a single functionality, single data repository.
- Each service is in...