This will be a short post more focused towards introducing Spring Boot, We will discuss “What is Spring Boot“? and how Spring Boot is helpful in building Java based enterprise applications.
Introduction
Before we can answer “What is Spring Boot?” , I am sure that as a Java developer, it’s very likely that we have worked with Spring Framework based application directly or indirectly.Spring has a number of ways to configure its behavior, it provides the option to use XML based configurations or to use Annotations, JavaConfig is becoming the de facto standard for the new Spring based applications.Although these options seem really good, with large enterprise applications involves hundreds of modules and complicated business rules, these configurations can become really complicated. Below are some of the complications which large application can bring in to the picture
- Each module has its own configurations.
- Each module contains its own set of dependencies (third-party dependencies)
- Upgrading application (e.g
Spring 4.x to 5.x
) will be complicated as we need to ensure that all required dependencies are upgraded correctly. - In case some of the dependencies are not working correctly, trying to find out root cause is a large application is really complicated and challenging.
All above issues are more or less related to making sure we have everything before dev team can start working on actual tasks. Now let’s talk about another use case which we use to do with any Spring based application, Let’s say we want to create a web-based application, these are most common steps most of us will be doing on regular basis
- Create a web application using Maven or IDE of our choice.
- Copy standard web application configurations (web.xml configuration for Spring MVC application).
- Tweak above configurations based on our requirements.
- Configure Spring MVC standard beans like
ViewResolver, MessageSource etc.
- Configure Database properties to be used for our application.
- Establish DB layer and ensure underlying DB connection is in place before we can start using it (
EntityManagerFactory
,TransactionManager
etc.)
This list can grow significantly based on type of our application
1. What is Spring Boot ?
All of the above steps seem very to us but they add a lot of overhead to the development team and instead of focusing on solving the actual business problem, initial time will be consumed to ensure that everything is in the correct place to start work. To answer the question “What is Spring Boot”, think of Spring Boot as a tool which can do these initial tasks for us automatically.
Spring Boot works on an opinionated view of the Spring platform being used by us and ensures that team can quickly start working on solving the actual business problem rather than spending time on the initial configurations and setup.Spring Boot provides the following feature out of the box
- It simplifies Spring dependencies by taking the opinionated view ( we will discuss it in more details).
- Spring Boot provides a pre-configured set of technologies/framework to reduces error-prone configuration so we as a developer focused on building our business logic rather than thinking of project setup.
- You really don’t need those big
XML configurations
for your project. - Embed Tomcat, Jetty or Undertow directly.
- Provide opinionated
Maven POM
to simplify your configuration
Using Spring Boot, it’s easy to manage and handle issues highlighted in the introduction section .We are not required to manually search for compatible jars during upgrade, Spring Boot will ensure that our application is upgraded to the correct version (This is called working on application with minimum fuss)
Let’s take a look at a sample pom.xml
for our web application to get an understanding of sample Spring Boot configuration
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.javadevjournal.demo</groupId>
<artifactId>sample web application</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.7.RELEASE</version>
</parent>
<name>rWeb Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-hateoas</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
</project>
Just pay close attention to <packaging>
tag, Spring Boot provides flexibility to run our application as a jar
and not forcing us to use war
as required packaging type.Let’s take a look at the below image to get an understanding of what is Spring boot and what are it’s different components.
2. Better Dependency Management
Just check configuration closely and you won’t be finding any entry for all those Spring dependencies (like web MVC, core, AOP, ORM, Validation API
etc.), you might have noticed similar entries spring-boot-starter-*
, this is Spring Boot dependency management process. We have added spring-boot-starter-web
to our pom.xml
and Spring Boot will pull all required dependencies for Spring MVC
application (no more manual configurations).
3. Auto Configurations
Spring Boot auto configuration is another interesting feature of Spring Boot this is why Spring Boot team say’s that it has opinions.These are some of work Spring Boot will do for you
- It will add all dependencies as highlighted in point 2.
- Auto Configurations indicates that Spring Boot has some reasonable defaults i.e based on the configurations Spring Boot will guess the type of application and will supply default implementations required to run your application in case we have not defined those in our application. in case you define these, Spring Boot will ensure that these defaults will be taken out of the context and let your custom configurations will take charge of application.
- To give a more clear picture, let’s say you have defined dependency for JPA and have not defined any database configurations, Spring Boot will automatically create required configurations for us.
4. Servlet Container
Do you recall that process of deploying your application on the Servlet container (Tomcat etc.), every time we make those small changes and require to deploy those into the app server to test our changes? Spring Boot provides support for embedded Servlet container and we are no longer require to deploy our application on app server (This can be run easily using standard main method) but at the same time we can access our application on the browser using http://<host>:<port>
Spring-boot-starter-web entry
in our pom.xml
will provide embedded servlet container for our web application, Apache Tomcat is the default servlet container provided by Spring Boot, however, Spring boot provides ways to use other servlet containers (all we have to add the required starter for this).
Read our post Building an Application with Spring Boot to start building your application with Spring Boot.
5. Difference between Spring and Spring Boot
If you are starting with Spring Boot, there are multiple questions that might come to your mind:
- Is Spring Boot a replacement for Spring Framework?
- What is Spring Boot and how it is different than Spring or Spring MVC.
- What is the difference between Spring and Spring Boot?
- Can we use Spring and Spring Boot together?
It’s very important that we understand the difference between Spring framework and Spring Boot. This can help us to take a better decision if we should use Spring Boot or not.
- Spring Boot is build on top of Spring framework and it’s not a replacement of the spring.
- Think of Spring boot as a tool set to build your Spring powered application. This tool set understand what components and dependencies are required to build a specific application and will help you to bootstrap the application quickly.
- Take an example of building Spring mvc web application, Spring Boot understand what all components are required and it will quickly bootstrap the web application so we can focus on the business components and not on bootstrapping the application.
- Keep in mind that spring boot will only wire the components required for our application (e.g DI, Spring MVC, Spring Data etc) but the core work will finally be handled by these components only.
Do You Need to Learn Spring First?
Yes, If you are starting with the Spring framework. Remember Spring Boot is an integration framework and makes it easy to build Spring framework application by providing you with sensible default and bootstrapping all required components. Keep in mind that you still need the understanding of the Spring module which you like to use with the help of Spring Boot.
Why Is Spring Boot So Popular?
There are multiple reasons why Spring Boot is so popular. Spring Boot make it easy to build spring based enterprise application by providing sensible default and configurations. You don’t have to worry about the configurations or dependencies as they are taken care automatically by Spring Boot.
What is Spring Boot?
Spring Boot is open source project with an opinionated view. Spring Boot helps you to create stand-alone, production-grade Spring-based Applications that you can run.It takes an opinionated view of the Spring platform and third-party libraries, so that you can get started with minimum fuss
Summary
In this post, we get an understanding of the Spring Boot, we covered What is Spring Boot? and what are the Benefits of Spring Boot? We discussed different features of the Spring Boot. Spring Boot internally does a lot of things for us which seems to magical to us. In this series, we will be uncovering all these internal details of the Spring Boot.Refer to the Spring Boot documentation for more details.
I’m very new at Java specially at Java Spring, and this is so helpful to me.
Thank you
I am happy that it helped you Siavash!!