What's @SpringBootApplication Annotation?

Tommy - Feb 3 '23 - - Dev Community

In a nutshell, @SpringBootApplication is a convenience annotation that is equivalent to declaring three separate annotations @Configuration, @EnableAutoConfiguration, and @ComponentScan with their default attributes. It is typically used on the main class of a Spring Boot application to enable auto-configuration, component scanning, and to define the application context.

  • @Configuration: Indicates that the class is a source of bean definitions for the application context.

  • @EnableAutoConfiguration: Enables Spring Boot's auto-configuration mechanism, which automatically configures the application based on the dependencies that are added in the classpath.

  • @ComponentScan: Enables component scanning so that the annotated components are automatically discovered and registered as beans in the application context.

By using the @SpringBootApplication annotation, developers can write less code and make their code easier to read and understand.

ex:

package com.example.myapplication;

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

@SpringBootApplication // same as @Configuration @EnableAutoConfiguration @ComponentScan
public class Application {

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

}
Enter fullscreen mode Exit fullscreen mode
. . . . . . . . . . . . . . . . . . .
Terabox Video Player