In this post, we feature a comprehensive article explaining what is Spring Boot.
Java is a ubiquitous language and Spring/
Spring Boot has become the default framework 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 I mentioned 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 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 Bootprovides an auto-configuration facility (tries to automatically configure the application based on the jars added).
Spring Bootcomes with an embedded web server. This makes the 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 Bootauto detects H2 driver and Hibernate jars and auto-configures the data source. This eliminates data source related configuration code.
Spring Bootcan 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 @EnableAutoConfiguration or @SprinBootApplication
- 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 BootActuator solves this hurdle. The actuator provides metrics and insights into running the
Spring Bootapplication. 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 BootActuator provides various REST endpoints to fetch these data.
Spring Boot Initializer– 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 BootCLI explicitly. CLI is a command-line based interface and lets us create
Spring Bootapplication using Groovy programming language.
Spring BootCLI leverages on auto-configuration and starter poms.
5.What are Spring Boot Starters and which are?
Spring Boot Starters help in the dependency management of a complicated project. They help in cutting down the added dependencies count by adding only one dependency.
Spring Boot framework has a number of bootstrap starters to add dependencies to the classpath. These starters are dependency descriptors. All the starters follow the convention spring-boot-starter-*. * stands for an application type. For example, if you are using JPA with Spring, spring-boot-starter-data-jpa is added as a dependency in the POM project file. They help in the bootstrapping process. The starter has the dependencies and predefined configuration bits. Sample starter implementation is shown in this article. A list of
Spring boot starters is attached below in the table.
|spring-boot-starter-thymeleaf||to build MVC web applications|
|spring-boot-starter-data-couchbase||used for the Couchbase document-oriented database|
|spring-boot-starter-artemis||used for JMS messaging using Apache Artemis.|
|spring-boot-starter-web-services||used for Spring Web Services.|
|spring-boot-starter-mail||used to support Java Mail|
|spring-boot-starter-data-redis||used for Redis key-value data store with Spring Data Redis|
|spring-boot-starter-web||used for developing the web application, including RESTful applications using Spring MVC.|
|spring-boot-starter-data-gemfire||used to GemFire distributed data store|
|spring-boot-starter-activemq||used in JMS messaging using Apache ActiveMQ.|
|spring-boot-starter-data-elasticsearch||used in Elasticsearch search and analytics engine|
|spring-boot-starter-integration||used for Spring Integration.|
|spring-boot-starter-test||used to unit test Boot applications|
|spring-boot-starter-jdbc||used for JDBC support|
|spring-boot-starter-mobile||used for developing mobile web applications|
|spring-boot-starter-validation||used for Java Bean Validation with Hibernate Validator.|
|spring-boot-starter-hateoas||used to develop a hypermedia-based RESTful web application with Spring MVC and Spring HATEOAS.|
|spring-boot-starter-jersey||used to build RESTful web applications using JAX-RS and Jersey.|
|spring-boot-starter-data-neo4j||used for the Neo4j graph database|
|spring-boot-starter-data-ldap||used for Spring Data LDAP.|
|spring-boot-starter-websocket||used for developing the WebSocket applications.|
|spring-boot-starter-aop||used for aspect-oriented programming with Spring AOP and AspectJ.|
|spring-boot-starter-amqp||used for Spring AMQP and Rabbit MQ.|
|spring-boot-starter-data-cassandra||used for Cassandra distributed database and Spring Data Cassandra.|
|spring-boot-starter-social-facebook||used for Spring Social Facebook.|
|spring-boot-starter-jta-atomikos||used for JTA transactions using Atomikos.|
|spring-boot-starter-security||used for Spring Security.|
|spring-boot-starter-mustache||used for building MVC web applications using Mustache views.|
|spring-boot-starter-data-jpa||used for Spring Data JPA with Hibernate.|
|spring-boot-starter||used for core starter, including auto-configuration support, logging, and YAML.|
|spring-boot-starter-groovy-templates||used for building MVC web applications using Groovy Template views.|
|spring-boot-starter-freemarker||used for building MVC web applications using FreeMarker views.|
|spring-boot-starter-batch||used for Spring Batch.|
|spring-boot-starter-social-linkedin||used for Spring Social LinkedIn.|
|spring-boot-starter-cache||used for Spring Framework’s caching support.|
|spring-boot-starter-data-solr||used for the Apache Solr search platform with Spring Data Solr.|
|spring-boot-starter-data-mongodb||used for MongoDB document-oriented database and Spring Data MongoDB.|
|spring-boot-starter-jooq||used for jOOQ to access SQL databases.|
|spring-boot-starter-jta-narayana||used for |
|spring-boot-starter-cloud-connectors||used for Spring Cloud Connectors|
|spring-boot-starter-jta-bitronix||used for JTA transactions using Bitronix.|
|spring-boot-starter-social-twitter||used for Spring Social Twitter.|
|spring-boot-starter-data-rest||used for exposing Spring Data repositories over REST|
|spring-boot-starter-actuator||used for Boot’s Actuator that provides production-ready features|
|spring-boot-starter-remote-shell||used for the CRaSH remote shell to monitor and manage your application|
|spring-boot-starter-undertow||used for Undertow as the embedded servlet container|
|spring-boot-starter-jetty||used for Jetty as the embedded servlet container|
|spring-boot-starter-logging||used for logging using Logback|
|spring-boot-starter-tomcat||used for Tomcat as the embedded servlet container|
|spring-boot-starter-log4j2||used for Log4j2 for logging|
6. Why Boot is so popular?
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.
6.1 What is MicroService and why we use it?
Microservices are based on loosely coupled architecture principles. The principle is related to designing microservices with lesser dependencies with other components in the application. Microservices based application has more than one unit. API gateway is used for authentication of microservices. Microservice is highly testable and maintainable. Each service is independently deployable. They improve productivity as each microservice can be developed by lesser engineers. Every microservice can be working with different data stores. Microservices based application has advantages such as continuous delivery and deployment, smaller units, improved fault isolation, and flexibility in choosing a tech stack for a microservice.
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 boot abstracts many configurations, getting the fine-grained control can be cumbersome sometimes. This might even make the debugging difficult.
- Larger the application, the 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.
8. Monitoring 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 as well. 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.
10. 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
Last updated on Oct. 13th, 2020