Dung (Donny) Nguyen

Senior Software Engineer

SpringBootApplication Annotation

The @SpringBootApplication annotation in Spring Boot is a convenience annotation that combines three other important annotations commonly used in Spring applications:

Combined Annotations

  1. @EnableAutoConfiguration
    • Purpose: Enables Spring Boot’s auto-configuration feature.
    • Details: This tells Spring Boot to automatically configure the application context based on the dependencies on the classpath. For example, if spring-boot-starter-web is on the classpath, Spring Boot will set up a default configuration for a web application (like setting up a DispatcherServlet).
    • Usage without @SpringBootApplication:
      @EnableAutoConfiguration
      public class AppConfig {
      }
      
  2. @ComponentScan
    • Purpose: Enables component scanning.
    • Details: This annotation directs Spring to scans for Spring-managed components (e.g., @Component, @Service, @Repository, @Controller) within the specified package and its sub-packages. This is essential for Spring’s Dependency Injection (DI) mechanism to work.
    • Usage without @SpringBootApplication:
      import org.springframework.context.annotation.ComponentScan;
      import org.springframework.context.annotation.Configuration;
      import org.springframework.stereotype.Service;
      
      @ComponentScan(basePackages = "com.example.service")
      @Configuration
      public class ComponentScanExample {
          public static void main(String[] args) {
              SpringApplication.run(ComponentScanExample.class, args);
          }
      }
      
      // In another package: com.example.service
      package com.example.service;
      
      import org.springframework.stereotype.Service;
      
      @Service
      public class MyService {
          public String getServiceMessage() {
              return "Service is working!";
          }
      }
      
      // Spring will detect and register MyService as a bean because of @ComponentScan.
      
  3. @Configuration
    • Purpose: Marks the class as a source of bean definitions.
    • Details: This annotation indicates that a class contains bean definitions. It is equivalent to a Spring XML configuration file but uses Java code.
    • Usage without @SpringBootApplication:
      import org.springframework.context.annotation.Bean;
      import org.springframework.context.annotation.Configuration;
      
      @Configuration
      public class ConfigurationExample {
          // Spring creates and manages a bean of type MyBean
          @Bean
          public MyBean myBean() {
              return new MyBean();
          }
      }
      
      class MyBean {
          public String sayHello() {
              return "Hello from MyBean!";
          }
      }
      

Using @SpringBootApplication

Combining all three annotations with @SpringBootApplication simplifies the configuration.

Example: Using @SpringBootApplication

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

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

@RestController
class MyController {
    @GetMapping("/")
    public String home() {
        return "Hello from Spring Boot Application!";
    }
}

How @SpringBootApplication Simplifies Code

Without @SpringBootApplication, you would need to combine the annotations explicitly:

import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableAutoConfiguration
@ComponentScan(basePackages = "com.example")
public class WithoutSpringBootApplication {
    public static void main(String[] args) {
        SpringApplication.run(WithoutSpringBootApplication.class, args);
    }
}

Additional Features of @SpringBootApplication

  1. Excluding Auto-Configurations
    You can exclude specific auto-configurations:
    @SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
    public class App {
        public static void main(String[] args) {
            SpringApplication.run(App.class, args);
        }
    }
    
  2. Custom Component Scanning
    You can customize component scanning with @SpringBootApplication:
    @SpringBootApplication(scanBasePackages = "com.example.custom")
    public class CustomScanApp {
        public static void main(String[] args) {
            SpringApplication.run(CustomScanApp.class, args);
        }
    }
    

Benefits of @SpringBootApplication

By understanding the annotations that @SpringBootApplication combines, you gain better control and insight into how Spring Boot initializes and configures your application.