What is @SpringBootApplication annotation in Spring Boot?

The @SpringBootApplication annotation is a combination of three annotations:

  • @Configuration
  • @EnableAutoConfiguration
  • @ComponentScan

It is a convenient way to configure a Spring Boot application with sensible defaults and to enable Spring Boot’s auto-configuration feature.


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

public class MyApp {
    public static void main(String[] args) {
        SpringApplication.run(MyApp.class, args);

Here, we use @SpringBootApplication to annotate the main class. This enables:

First, it enables @Configuration, which indicates that the class contains Spring Bean definitions. This is essential for configuring our application with Spring Boot.

Second, it enables @EnableAutoConfiguration, which allows Spring Boot to automatically configure our application based on the dependencies we have added to our project. This feature is one of the key benefits of using Spring Boot, as it eliminates much of the boilerplate configuration code that is required in traditional Spring applications. For example, suppose you added the H2 (in-memory database) depdendency in your pom.xml e.g.


Now when we run our application, Spring Boot will automatically configure H2 for us, based on the presence of this dependency. This is because @EnableAutoConfiguration scans the classpath for libraries and automatically configures the application based on the dependencies it finds.

Finally, it enables @ComponentScan, which tells Spring where to look for Spring-managed components, such as Controllers, Services, and Repositories. By default, Spring Boot will scan the package of the main application class and its sub-packages for these components.

In the main() method, we use SpringApplication.run() to launch our Spring Boot application. This method starts the Spring Boot application context, which is responsible for managing the lifecycle of our application and providing access to Spring-managed components.

Speak Your Mind