What is Spring Boot (with video)
In this article, we will explain what is Spring Boot.
You can also check this tutorial in the following video:
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 hurdles 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 applications’ opinionated views and enables us to get started with minimal fuss with configurations.
1. What is Spring Boot
It 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 the 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 its 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 the local development environment hassle-free and developers don’t have to worry about configuring development web servers in their machines.
To learn more about their differences, check the Spring vs Spring Boot Example.
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 overcome the problems faced.
4. Notable features
It comes with several feature enhancements. In this section, we are going to see some of the notable features.
- Autoconfiguration – The autoconfiguration feature eliminates the boilerplate code used to configure some of the popular libraries. For example, in a 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. It can also 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 Boot
Actuator solves this hurdle. The actuator provides metrics and insights into running theSpring 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
–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 installSpring Boot
CLI explicitly. CLI is a command-line based interface and lets us createSpring Boot
application using Groovy programming language.Spring Boot
CLI leverages on auto-configuration and starter poms.
5.What are Boot Starters and which are?
They help in the dependency management of a complicated project. They help in cutting down the added dependencies count by adding only one dependency. The 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.
Name | Usage |
---|---|
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 Narayana JTA Starter. |
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 |
5.1 Maven Dependencies
Let us look at the spring boot starters
which are the maven dependencies. Maven dependencies are managed in pom.xml. The naming convention is spring-boot-starter-*. The convention of the third-party starters is xyz-spring-boot-starter if the third-party project is XYZ. These dependencies help in downloading the right jars from the maven repository. In the maven pom.xml, it is specified with the version. This helps in changing the dependency and the version very easily. Multi-module projects can be managed using maven dependencies.
You can have the Maven project inherit from spring-boot-starter-parent different attributes such as java compiler version, source encoding, dependencies, resource filtering, and plugin configuration.
5.1.1 Actuator dependency
Actuator helps in spring boot application monitoring. You can add custom actuator endpoints in the spring boot app. Actuator dependency can be added to the maven project by adding a spring-boot-starter-actuator. This is specified in pom.xml and the sample pom.xml snippet is shown below.
pom.xml
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> <version>2.2.2.RELEASE</version> </dependency>
5.1.2 Security dependency
You can add security dependency to specify the basic authentication for the application. You can use spring-boot-starter-security starter
for authenticating the endpoints which do not include “/” and “/home”. This is specified in pom.xml and the sample pom.xml snippet is shown below.
pom.xml
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> <version>2.2.2.RELEASE</version> <scope>test</scope> </dependency>
5.1.3 Web dependency
You can develop a spring web application by specifying the web dependency. The dependency helps in using Spring MVC, REST, and Tomcat. This dependency helps in decreasing the build dependency count. This is specified in pom.xml and the sample pom.xml snippet is shown below.
pom.xml
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>2.2.2.RELEASE</version> </dependency>
5.1.4 Leaf dependency
You can use Leaf dependency for creating web applications based on XHTML/HTML5. ThymeLeaf is a java based library used to build a web app serving XHTML/HTML5. This is specified in pom.xml and the sample pom.xml snippet is shown below.
pom.xml
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> <version>2.2.2.RELEASE</version> </dependency>
5.1.5 Test dependency
You can use test dependency to create unit tests for the project. It can be created by using IDE or Spring Initializr. You can specify the scope of the tests using the scope tag. This is specified in pom.xml and the sample pom.xml snippet is shown below.
pom.xml
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <version>2.2.2.RELEASE</version> <scope>test</scope> </dependency>
6. Why it is so popular?
Its 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, the 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 the 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.
7. Disadvantages
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
The module spring-boot-actuator
provides production-ready monitoring features. By adding the dependency spring-boot-starter-actuator
in pom, enables the actuator feature.
The 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 endpoint can be enabled or disabled. Endpoints can be served via either HTTP or JMX.
Some of the actuator endpoints are listed below,
API Endpoint | Description |
---|---|
beans | Displays a list of all the beans in your application |
auditEvents | Exposes all the audit events for the current application |
caches | Exposes all the caches in the application |
conditions | Shows conditions that were evaluated on configuration and auto configurations |
configprops | Displays a list of all @ConfigurationProperties |
env | Exposes properties from Spring’s ConfigurableEnvironment |
flyway | Shows the flyway migrations that have been applied |
health | Shows application health information |
httptrace | Displays HTTP trace information |
info | Displays applications arbitrary information |
integrationgraph | Shows the spring integration graph. requires spring-integration-core dependency |
loggers | Shows and modifies logging configuration |
liquibase | Shows any liquibase migration applied |
metrics | Shows the current application’s metrics |
mappings | Shows a list of @RequestMapping |
scheduledtasks | Displays a list of scheduled tasks in the application |
sessions | Enables us to retrieve and delete sessions |
shutdown | Lets the application shutdown gracefully |
threaddump | Performs a thread dump |
If the application is a web application then below additional endpoints are available:
API Endpoint | Description |
---|---|
heapdump | Returns current heap dump |
jolokia | Exposes JMX beans over HTTP and it requires jolokia-core dependency |
logfile | Returns the contents of the log file |
prometheus | Exposes the metrics in a format that can be scraped by the Prometheus server. This requires micrometer-registry-prometheus dependency. |
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
.
9. Example
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 application by visit 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.
{"_links":{"self":{"href":"http://localhost:8080/actuator","templated":false},"beans":{"href":"http://localhost:8080/actuator/beans","templated":false},"caches-cache":{"href":"http://localhost:8080/actuator/caches/{cache}","templated":true},"caches":{"href":"http://localhost:8080/actuator/caches","templated":false},"health":{"href":"http://localhost:8080/actuator/health","templated":false},"health-path":{"href":"http://localhost:8080/actuator/health/{*path}","templated":true},"info":{"href":"http://localhost:8080/actuator/info","templated":false},"conditions":{"href":"http://localhost:8080/actuator/conditions","templated":false},"configprops":{"href":"http://localhost:8080/actuator/configprops","templated":false},"env":{"href":"http://localhost:8080/actuator/env","templated":false},"env-toMatch":{"href":"http://localhost:8080/actuator/env/{toMatch}","templated":true},"loggers":{"href":"http://localhost:8080/actuator/loggers","templated":false},"loggers-name":{"href":"http://localhost:8080/actuator/loggers/{name}","templated":true},"heapdump":{"href":"http://localhost:8080/actuator/heapdump","templated":false},"threaddump":{"href":"http://localhost:8080/actuator/threaddump","templated":false},"metrics":{"href":"http://localhost:8080/actuator/metrics","templated":false},"metrics-requiredMetricName":{"href":"http://localhost:8080/actuator/metrics/{requiredMetricName}","templated":true},"scheduledtasks":{"href":"http://localhost:8080/actuator/scheduledtasks","templated":false},"mappings":{"href":"http://localhost:8080/actuator/mappings","templated":false}}}
For example, when you access http://localhost:8080/actuator/health you see the below output:
You can try different actuator endpoints to get more ideas on actuators.
10. Additional sources
11. 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 Dec. 22nd, 2021