What is Spring Boot

Last modified: April 03, 2022

by Matteo Carlo Giavarini

1. Overview

Spring Boot is an open source, widely-used framework based on Java that enables and facilitates the development of applications in a microservices context.

The purpose of this tutorial is to introduce the concepts at the base of microservices architecture and to illustrate what are the major pros of Spring Boot.

2. What are Microservices and how they communicate

A microservices architecture allows developers to develop and deploy a service independently of other services in an environment. Due to the ability to act on individual components of the architecture, as opposed to a monolithic application, release times and error probability are reduced. Additionally, the number of unavailable features is reduced during the release (Because only one service goes down). Microservices can communicate with each other with API REST or SOAP.

An API (Application Programming Interface) REST (Representational State Transfer) is a web-accessible "resource" reachable with HTTP protocol of which whose constructs are: POST,GET,PATCH,PUT,DELETE (CRUD). It can exchange different types of payloads, the common-used is Json.

SOAP (Simple Object Access Protocol.) is a communication protocol that defines standards to be strictly followed, requires more bandwidth/resource than REST and permits XML data format only. For these reasons SOAP is less preferred than REST.

3. Benefits of Spring Boot

Let's see why Spring Boot is so popoular.

3.1. Stand-alone applications

Spring Boot provides the possibility to incorporate in your application an embedded web server like Tomcat or Jetty. This means that you can "just run" your application through the .jar (Java Archive) file. You can also produce a .war (Web Application Archive) file and execute the application on a separated web server.

3.2. Auto-Configuration and @Annotations

One of the most appreciated feature of Spring Boot is the auto-configuration of the application, this is called IoC (Inversion of Control) and it is an implementation of fifth principle of S-O-L-I-D, the Dependency Injection. In simple terms this means that Objects declare their dependecy through an interface, then the framework will choose and inject the implementation of the requested dependecy. This is the opposite of we usually do when we (developers) declare a dependecy and provide an implementation (calling the constructor of the object). The IoC enables developers to create modular applications consisting of loosely coupled components and to speed up development, for more about IoC click here.

To enable Spring Boot configuration you have to use over your main class the annotation @EnableAutoConfiguration or @SpringBootApplication that includes the first one. @SpringBootApplication includes:

  • @EnableAutoConfiguration: Enables Auto-configuration
  • @ComponentScan: Tell to the framework to search the components in the classpath of the application
  • @Configuration: Indicates that the class has @Bean definition methods

Example of usage of @SpringBootApplication:
@SpringBootApplication
public class MyApplication {
	public static void main(String[] args){ 
		SpringApplication.run(MyApplication.class, args);
	}
}

3.3. Spring starters

Another advantage that Spring Boot provides are several packages of dependencies called Spring Boot Starters, basically you can include in your POM (Project Object Model) the dependecy descriptors that you want import in your application. This is really helpful because avoid you to copy-paste tons of dependency code.

POM file is an XML file that contains information about the project and configuration details used by Maven to build the project. Maven is powerful tool based on POM that can be used to build, publish, and deploy applications.

For example if you want to monitorize the health stauts of your application, basically you can add at the POM file the Actuator starter dependency descriptior, like this:
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
</dependencies>
Maven will download and add the relative jar at your application.

3.4. Spring Initializr

Spring Initializr is a web-based tool that allows developers to generate and inizialize a new Spring Boot project. You can also add since the beginning all the dependency/starters that you'll need in your application.

4. Conclusion

In this article, we learned what is a microservice architecture, what is Spring Boot framework and why you could use it.

Spring bottom

© matteocarlogiavarini. All Rights Reserved.