Talking about how Spring Boot works

In our previous tutorials, we looked at an overview about the Spring Boot and how to create a Spring Boot project using the CLI, using the Spring Initialzr Web and the Spring Tool Suite. Some of you who have not worked much with Spring Boot, will wonder how it works. So, in this tutorial, I will take a moment to talk about how the Spring Boot works for you. If you already know, then review again to see if you understand correctly.

OK, I will first create a new Spring Boot project with HelloController as follows:

Talking about how Spring Boot works

The contents of the HelloController class:

Maven dependencies:

Now, if you run this project and access the http://localhost:8080/ path, you will see the following output:

Talking about how Spring Boot works

As you can see, although we do not declare necessary configuration files like web.xml, @Configuration annotation, or any bean in the Spring container, our application is still running normally and displaying the content that we want.

To do this, Spring Boot must rely on the @SpringBootApplication annotation declared in the SpringBootExampleApplication class:

This annotation allows the Spring Boot to automatically scan all of the configurations and beans in our application.

The content of this annotation is as follows:

@SpringBootApplication annotation is a combination of three other Spring annotations, including @SpringBootConfiguration, @ComponentScan, and @EnableAutoConfiguration where the @EnableAutoConfiguration annotation is the most important.

With @EnableAutoConfiguration annotation, Spring Boot will automatically configure our application based on the classpath, annotations, and configuration information that we defined.

All of these annotations will help the Spring Boot automatically configure our applications accordingly and we do not need to worry about configuring them.

In our example, Spring Boot checks the classpath in our project, and because of its dependency having spring-boot-starter-web, Spring Boot will be configured the application as a web application.

In addition, Spring Boot will treat its HelloController as a web controller based on the @Controller annotation and the @RequestMapping annotation.

And because my application has a dependency as a Tomcat server, Spring Boot will also use this Tomcat server to run my application.

Add Comment