
What is Spring Boot?
On June 22, 2022 by JulietteSpring Boot is one of the most talked-about technologies in the world of Spring lately. What is Spring Boot and how does it work? To understand the concept, we must first reflect on how we build applications with the Spring Framework.
There are three steps to perform. The first is to create a Maven/Gradle project and download the necessary dependencies. Secondly, we develop the application, and thirdly, we deploy it on a server. If we think about it a bit in detail, step two is a development task. The other steps are more infrastructure-oriented. We shouldn’t have to continually choose dependencies and the deployment server.
Spring Boot
SpringBoot was born to simplify steps 1 and 3 so that we can focus on developing our application. How does it work?. The approach is simple and we will understand it by carrying out an example. For this, we are going to connect to the Boot Assistant called Spring Initializer.
Spring Boot is a nice framework, but it’s mostly compared with Django, Play, etc.
String Boot Starter and Simplifications
The wizard is intuitive; we choose the package to which we want our classes to belong; we choose the name of the project, and finally the dependencies. Of course, it is no longer about choosing jars by JAR, but by the type of application that we need, this concept is called Spring Starter. Therefore, instead of having to choose 10 or 20 dependencies, it is much more comfortable to choose 2 starters and Spring Boot takes care of the rest.
In this case, I’m going to build a Spring MVC application and choose the Web or Starter Web dependency. Click Generate project and it will download a Maven project in zip format. We unzipped the project and this is its content.
A Spring application with a fully configured Maven framework. The next step is to use Maven and write to the command line.
mvn eclipse:eclipse
This will convert the Maven project into an Eclipse project that we can open with the editor. If we have a modern version of Eclipse in, we will not even have to do that step, it will be enough to import the project directly as Maven Project. Let’s see the content of the HolaSpringBootApplication class
package com.architecturejava ; import org.springframework.boot.SpringApplication ; import org.springframework.boot.autoconfigure.SpringBootApplication ; @SpringBootApplication public class HelloSpringBootApplication { public static void main ( String [] args ) { SpringApplication. run ( HelloSpringBootApplication.class , args ) ; } } This class is in charge of starting our Spring application; unlike a classic approach, it does not need to be deployed on a web server since Spring Boot provides one. Let's modify it and add an annotation.
package com.architecturejava ;
import org.springframework.boot.SpringApplication ;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration ;
import org.springframework.boot.autoconfigure.SpringBootApplication ;
import org.springframework.context.annotation.ComponentScan ;
@SpringBootApplication
@ComponentScan ( basePackages = “com.javaarchitecture” )
public class HelloSpringBootApplication {
public static void main ( String [] args ) {
SpringApplication. run ( HelloSpringBootApplication.class , args ) ;
}
}
Recall that the @ComponentScan annotation scans packages for classes that SpringFramework can inject. In this case we are going to build a HelloWorld controller.
import org.springframework.stereotype.Controller ;
import org.springframework.web.bind.annotation.RequestMapping ;
import org.springframework.web.bind.annotation.ResponseBody ;
@Controller
public class HelloController {
@RequestMapping ( “/” )
@ResponseBody
String home () {
return “HelloWorld” ;
}
}
It’s time to run our application as a console application using the right button run as Java Application This will open a web server and we will access the URL.
SpringBoot has simplified all the operations when building the application, we have practically not had to select Spring dependencies and it has not been necessary to define any Tomcat server in our development environment since Spring Boot comes with one integrated. Spring Boot Tomcat and Docker
Why does Spring Boot have Tomcat built-in? It. Very simple because from now on the deployments are not going to be carried out on Standard Web Servers that store dozens of applications, but rather each one of the applications is going to be deployed in a Docker container, completely isolated from the rest and independent. The container needs the application to be fully operational on its own.
Leave a Reply