Home/Learn/Spring Boot/@SpringBootApplication

@SpringBootApplication

Beginner
Core & Setup

A convenience annotation combining @Configuration, @EnableAutoConfiguration, and @ComponentScan that bootstraps the entire application context.

Overview

@SpringBootApplication is a meta-annotation that combines three annotations: @Configuration (marks the class as a source of bean definitions), @EnableAutoConfiguration (activates Spring Boot's auto-configuration machinery that reads META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports), and @ComponentScan (scans the current package and sub-packages for Spring components). Placing it on your main class is sufficient to launch a fully-wired application context. You can customise scanning via the scanBasePackages or scanBasePackageClasses attributes, and you can exclude specific auto-configurations via the exclude or excludeName attributes without removing the dependency entirely.

Anatomy of @SpringBootApplication

The annotation is shorthand for three common declarations. You can replace it with all three explicitly if you need fine-grained control — for example to scan a different root package or to disable a problematic auto-configuration during tests.

Java — @SpringBootApplication customisation
@SpringBootApplication(
    scanBasePackages = "com.example",
    exclude = { DataSourceAutoConfiguration.class }
)
public class OrderServiceApplication {
    public static void main(String[] args) {
        SpringApplication.run(OrderServiceApplication.class, args);
    }
}

// Equivalent explicit form:
// @Configuration
// @EnableAutoConfiguration(exclude = DataSourceAutoConfiguration.class)
// @ComponentScan("com.example")

Excluding Auto-Configurations

When a dependency is on the classpath but you do not want its auto-configuration applied (e.g. you supply your own DataSource bean), use the exclude attribute. You can also set spring.autoconfigure.exclude in application.properties for a non-code approach — useful in test profiles.

Properties — disabling auto-configs in tests
# application-test.properties
spring.autoconfigure.exclude=\
  org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration,\
  org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration

SpringApplication Customisation

Instead of the static run() shortcut, create a SpringApplication instance to customise banner mode, default profiles, or to add application listeners before the context starts.

Java — SpringApplication builder customisation
public static void main(String[] args) {
    SpringApplication app = new SpringApplication(OrderServiceApplication.class);
    app.setBannerMode(Banner.Mode.OFF);
    app.setDefaultProperties(Map.of("server.port", "8081"));
    app.addListeners(new ApplicationPidFileWriter());
    app.run(args);
}

Key Points to Remember

  • 1@SpringBootApplication = @Configuration + @EnableAutoConfiguration + @ComponentScan
  • 2Auto-configuration is conditional — @ConditionalOnMissingBean means your own beans take priority
  • 3Use exclude or spring.autoconfigure.exclude to disable unwanted auto-configs
  • 4scanBasePackages restricts component scanning to a specific root (improves startup)
  • 5Only one @SpringBootApplication is needed per application; typically on the main class
  • 6spring.main.lazy-initialization=true delays bean creation to first use, cutting startup time

Interview Questions

Sign in to ask Aria
1

What three annotations does @SpringBootApplication replace?

EasyInfosys
2

How do you exclude a specific auto-configuration class?

EasyTCS
3

Why should the main class be at the root package level?

MediumWipro
4

What is the difference between @Configuration and @Component?

MediumCapgemini
5

How does Spring Boot decide which auto-configurations to apply at startup?

HardAmazon

Ask Aria about @SpringBootApplication

Your personal AI tutor — ask anything about this concept

Revision Status

Personal Notes

Sign in to save personal notes for this topic.

Discussion

Sign in to join the discussion.

Loading discussion…