In this post, we feature a comprehensive article explaining what is Spring Boot.
Java is a ubiquitous language and Spring/ Spring Boot has become default frameworks of choice for Java Web developers. Spring is one of the market-leading frameworks for building web and enterprise applications.
With the rise of the Spring framework there comes a lot of hurdle in configuring it. Even though Spring is quite flexible with this matter, integrations with different other frameworks made it tedious and erroneous. To handle this fuss Spring Boot was created.
Spring Boot makes it easy to create stand-alone, production-grade Spring applications. It takes Spring and third-party application’s opinionated views and enables us to get started with minimal fuss with configurations.
1. What is Spring Boot
Spring Boot is a Java-based application (Stand-alone, microservices, web) development framework. It is developed and maintained by the Pivotal team. Spring Boot enables developers to build production-grade applications quickly with its auto-configuration ability without losing a lot of time around Spring configurations.
2. Differences between Spring and Spring Boot
Understanding the differences between Spring and Spring boot is important before choosing between them. In this section, we will see how Spring Boot is different than the already existing Spring framework.
As said earlier Spring Boot is built on top of Spring framework. They work together to get the job done.
- Spring framework was built keeping flexibility in mind to enable developers to integrate with different frameworks. With the advent of newer frameworks, rightly configuring them became a fuss. Whereas Spring Boot’s primary focus is on reducing the configuration and code so that developers can focus more on the crucial parts of their application.
- Spring Boot provides an auto-configuration facility (tries to automatically configure the application based on the jars added).
- Spring Boot comes with an embedded web server. This makes local development environment hassle-free and developers don’t have to worry about configuring development web servers in their machines.
3. Is prior knowledge of Spring necessary?
Though many people feel prior knowledge of Spring is not essential, I feel having an idea of Spring Framework gives a strong foundation in learning Spring Boot. The framework itself is built on top of Spring to overcomes the problems faced.
4. Notable features
Spring boot comes with several feature enhancements. In this section, we are going to see some of the notable features.
- Autoconfiguration – Autoconfiguration feature eliminates the boilerplate code used to configure some of the popular libraries. For example, in traditional Spring application to integrate the H2 in-memory database and Hibernate, a separate data source configuration JDBC template bean to be defined. However, Spring Boot auto detects H2 driver and Hibernate jars and auto-configures the data source. This eliminates data source related configuration code. Spring Boot can make 200+ such decisions and can do auto configurations just by looking at the jar inclusions. Auto-configuration is disabled by default. You can enable auto-configuration using annotation
- Starter POM – Starter POM enables our application to find and add common dependencies. It removes a lot of hassle around fining various jars and keeping them up to date with different library versions. By adding spring-boot-starter-web dependency in the pom, we can bring in the minimum dependencies required for Spring Web applications to work.
- Actuator – With a lot of abstraction in place it becomes harder to debug and getting to know what is happening under the hood. Spring Boot Actuator solves this hurdle. The actuator provides metrics and insights into running the Spring Boot application. It allows us to see Spring Beans configured, Acuto configuration decisions, Environment variables, Command-Line arguments, System configurations and many more. It also enables us to see HTTP request traces, CPU and memory usage, garbage collection, data source usage and so on. Spring Boot Actuator provides various REST endpoints to fetch these data.
- Spring Boot Initializer – Spring Boot actuator enables us to create an initial structure of the project. Spring initializer can be accessed at Spring Initializer. Go to this website, enter all the metadata and dependencies, then download the project structure as a zip file. Later this can be imported as a Maven/Gradle project.
- Spring Boot CLI – This is an optional feature and one needs to install Spring Boot CLI explicitly. CLI is a command-line based interface and lets us create Spring Boot application using Groovy programming language. Spring Boot CLI leverages on auto-configuration and starter poms.
5. Why Spring Boot is so popular?
Spring Boot’s popularity is on the rise because it lets us focus on writing application code by eliminating the hassles in the setup process like project structure, configuration, and dependency management. It is opinionated and provides great support for Microservices based architecture. Notably, Spring Boot Actuator provides very granular metrics, this can be very helpful in managing microservices.
So far we have seen what makes Spring Boot a framework of choice. In this section let us see some pitfalls of this framework.
- We may not need all the dependencies introduced by stater POM always. This will increase the application binary size.
- As spring boot abstracts many configurations, getting the fine-grained control can be cumbersome sometimes. This might even make the debugging difficult.
- Larger the application, more the startup time. This might cause problems in a serverless environment.
- Dependencies increase the use of reflection. This might create a memory footprint to often resulting in failures with Out Of Memory.
7. Monitoring Spring Boot Applications
spring-boot-actuator provides production-ready monitoring features. By adding the dependency
spring-boot-starter-actuator in pom, enables the actuator feature.
Spring boot actuator provides several ready to use REST endpoints to fetch the metrics of a running application. You can add your own endpoints aswell. Each individual endpoints can be enabled or disabled. Endpoints can be served via either HTTP or JMX.
Some of the actuator endpoints are listed below,
|Displays a list of all the beans in your application|
|Exposes all the audit events for the current application|
|Exposes all the caches in the application|
|Shows conditions that were evaluated on configuration and auto configurations|
|Displays a list of all |
|Exposes properties from Spring’s |
|Shows the flyway migrations that have been applied|
|Shows application health information|
|Displays HTTP trace information|
|Displays applications arbitrary information|
|Shows the spring integration graph. requires |
|Shows and modifies logging configuration|
|Shows any liquibase migration applied|
|Shows the current application’s metrics|
|Shows a list of |
|Displays a list of scheduled tasks in the application|
|Enables us to retrieve and delete sessions|
|Lets the application shutdown gracefully|
|Performs a thread dump|
If the application is a web application then below additional endpoints are available,
|Returns current heapdump|
|Exposes JMX beans over HTTP and it requires |
|Returns the contents of the log file|
|Exposes the metrics in a format that can be scraped by Prometheus server. This requires |
By default endpoints except shutdown are enabled. To enable or disable any endpoint
management.endpoints.<endpoint id>.enabled = true/false. To make endpoints to be enabled or disabled individually, you need to set
management.endpoints.enabled-by-default = false.
In this section, I am going to show how easily a spring boot application can be created from Spring Initializer. Also, I am going to show how to access actuator endpoints.
To create the sample Spring Boot application by visiting Spring Initializer and choose the dependencies you need. I have chosen dependencies in the below image,
Run the application attached in the download section. Visit endpoint http://localhost:8080/hello to access the hello world endpoint. The output is as below,
Since I have added actuator dependency, the actuator is enabled by default. To see actuator endpoints access http://localhost:8080/actuator. The output is as below.
For example, when you access http://localhost:8080/actuator/health you see the below output,
You can try different actuator endpoints to get more idea on actuators.
9. Download the Source code
I have used IntelliJ Idea IDE to create and run this project. It works in Java 8 or above. Use any REST testing utilities like PostMan or any chrome extensions to test the different endpoints.
You can download the full source code of this example here: What is Spring Boot ?