Boot

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 Spring Boot Tutorial – 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 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 InitializerBoot 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.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.

NameUsage
spring-boot-starter-thymeleafto build MVC web applications
spring-boot-starter-data-couchbaseused for the Couchbase document-oriented database
spring-boot-starter-artemisused for JMS messaging using Apache Artemis.
spring-boot-starter-web-servicesused for Spring Web Services.
spring-boot-starter-mailused to support Java Mail
spring-boot-starter-data-redis used for Redis key-value data store with Spring Data Redis
spring-boot-starter-webused for developing the web application, including RESTful applications using Spring MVC.
spring-boot-starter-data-gemfireused to GemFire distributed data store
spring-boot-starter-activemq used in JMS messaging using Apache ActiveMQ.
spring-boot-starter-data-elasticsearchused in Elasticsearch search and analytics engine
spring-boot-starter-integrationused for Spring Integration.
spring-boot-starter-testused to unit test Boot applications
spring-boot-starter-jdbcused for JDBC support
spring-boot-starter-mobile used for developing mobile web applications
spring-boot-starter-validationused for Java Bean Validation with Hibernate Validator.
spring-boot-starter-hateoasused to develop a hypermedia-based RESTful web application with Spring MVC and Spring HATEOAS.
spring-boot-starter-jerseyused to build RESTful web applications using JAX-RS and Jersey.
spring-boot-starter-data-neo4jused for the Neo4j graph database
spring-boot-starter-data-ldap used for Spring Data LDAP.
spring-boot-starter-websocketused for developing the WebSocket applications.
spring-boot-starter-aop used for aspect-oriented programming with Spring AOP and AspectJ.
spring-boot-starter-amqpused 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-freemarkerused 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-solrused 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-jooqused for jOOQ to access SQL databases.
spring-boot-starter-jta-narayanaused for Spring Boot Narayana JTA Starter.
spring-boot-starter-cloud-connectorsused for Spring Cloud Connectors
spring-boot-starter-jta-bitronixused for JTA transactions using Bitronix.
spring-boot-starter-social-twitter used for Spring Social Twitter.
spring-boot-starter-data-restused for exposing Spring Data repositories over REST
spring-boot-starter-actuatorused for Boot’s Actuator that provides production-ready features
spring-boot-starter-remote-shellused for the CRaSH remote shell to monitor and manage your application
spring-boot-starter-undertowused for Undertow as the embedded servlet container
spring-boot-starter-jettyused for Jetty as the embedded servlet container
spring-boot-starter-loggingused for logging using Logback
spring-boot-starter-tomcatused for Tomcat as the embedded servlet container
spring-boot-starter-log4j2used for Log4j2 for logging
Spring Boot Starters

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 EndpointDescription
beansDisplays a list of all the beans in your application
auditEventsExposes all the audit events for the current application
cachesExposes all the caches in the application
conditionsShows conditions that were evaluated on configuration and auto configurations
configpropsDisplays a list of all @ConfigurationProperties
envExposes properties from Spring’s ConfigurableEnvironment
flywayShows the flyway migrations that have been applied
healthShows application health information
httptraceDisplays HTTP trace information
infoDisplays applications arbitrary information
integrationgraphShows the spring integration graph. requires spring-integration-core dependency
loggersShows and modifies logging configuration
liquibaseShows any liquibase migration applied
metricsShows the current application’s metrics
mappingsShows a list of @RequestMapping
scheduledtasksDisplays a list of scheduled tasks in the application
sessionsEnables us to retrieve and delete sessions
shutdownLets the application shutdown gracefully
threaddumpPerforms a thread dump

If the application is a web application then below additional endpoints are available:

API EndpointDescription
heapdumpReturns current heap dump
jolokiaExposes JMX beans over HTTP and it requires jolokia-core dependency
logfileReturns the contents of the log file
prometheusExposes 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,

What is Spring Boot - Spring Starter
Spring Starter

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,

What is Spring Boot - Hello World REST
Hello World REST endpoint

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:

What is Spring Boot - Health endpoint
Health endpoint 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.

Download
You can download the full source code of this example here: What is Spring Boot

Last updated on Dec. 22nd, 2021

Santosh Balgar

He is a Software Engineer working in an industry-leading organization. He has completed his bachelors from Visweswaraya Technological University. In his career, he has worked in designing and implementing various software systems involving Java/J2EE, Spring/ Spring Boot, React JS, JQuery, Hibernate and related database technologies. He loves to share his knowledge and always look forward to learning and explore new technologies. He loves to spend his free time with his family. He enjoys traveling and loves to play cricket.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button