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 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 Web Starter and a HelloController class as follows:

Talking about how Spring Boot works

The contents of the HelloController class:

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 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 on 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.

By default, Spring Boot will use the Tomcat server to run the web application, so you will see some log mentions about the Tomcat server when you run this example application. You can change the Server runtime if you want!

Add Comment