Question 21 · Section 5

How @SpringBootApplication Works

4. exclude -> disable auto-configurations 5. basePackageClasses -> type-safe scanning 6. FailureAnalyzer -> your own error messages

Language versions: English Russian Ukrainian

Junior Level

@SpringBootApplication is the main annotation of a Spring Boot application.

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

It does 3 things:

  1. @Configuration -> class is a configuration
  2. @EnableAutoConfiguration -> Spring Boot configures beans itself
  3. @ComponentScan -> scans packages and finds @Component, @Service

Rule:

  • Place in the root package
  • All sub-packages will be scanned

Middle Level

Anatomy

@SpringBootConfiguration    // = @Configuration for tests
@EnableAutoConfiguration   // Auto-configuration
@ComponentScan             // Package scanning
public @interface SpringBootApplication { }

ComponentScan Behavior

// Application in com.example.app
// -> Scans com.example.app and all sub-packages

// Services in com.example.common -> will NOT be found!
// Solution:
@SpringBootApplication(scanBasePackages = "com.example")

Startup Process

SpringApplication.run():
  1. Create ApplicationContext -> container for all beans
  2. Load Environment -> application.properties, environment variables, CLI arguments
  3. Refresh Context -> Spring core: registers BPPs, creates ALL beans, resolves dependencies
  4. Start embedded server -> Tomcat/Jetty/Undertow on specified port
  5. Execute CommandLineRunner -> your code after full context startup

proxyBeanMethods

// By default: true (CGLIB proxy)
// For optimization:
@SpringBootConfiguration(proxyBeanMethods = false)
// -> Faster startup, less memory: by default Spring creates a CGLIB subclass
// for each @Configuration class (Full mode). Bytecode generation takes
// time and memory. With proxyBeanMethods = false, @Bean method calls go directly.
// -> But @Bean methods create new objects on call

Senior Level

Refresh Context

Heart of startup:
  1. BeanFactoryPostProcessor -> modify BeanDefinition
  2. BeanPostProcessor registration
  3. Scanning + auto-configuration
  4. Bean creation
  5. Server startup

FailureAnalyzers

Spring Boot catches startup errors:
  -> "Port already in use"
  -> "DataSource not configured"

-> Nice messages instead of stack trace
-> Can create your own analyzers

Production Experience

Real scenario: ComponentScan did not find beans

Application -> com.example.app
Services -> com.example.services
-> Beans not found!

Solution:
  @SpringBootApplication(scanBasePackages = "com.example")

Best Practices

  1. Root package -> com.example.app
  2. scanBasePackages -> if beans are in other packages
  3. proxyBeanMethods = false - if @Bean methods do not call each other (otherwise singleton semantics will break: each call creates a new object).
  4. exclude -> disable auto-configurations
  5. basePackageClasses -> type-safe scanning
  6. FailureAnalyzer -> your own error messages

Summary for Senior

  • @SpringBootApplication = Configuration + AutoConfig + ComponentScan
  • Location -> determines scanning scope
  • Refresh Context -> key bean creation phase
  • proxyBeanMethods -> startup optimization
  • FailureAnalyzers -> readable errors
  • scanBasePackages -> for beans outside root package

Interview Cheat Sheet

Must know:

  • @SpringBootApplication = @Configuration + @EnableAutoConfiguration + @ComponentScan
  • Place in root package - scans all sub-packages
  • SpringApplication.run(): create context -> load Environment -> Refresh Context -> start server -> CommandLineRunner
  • proxyBeanMethods = false speeds up startup, but breaks singleton on @Bean method calls to each other
  • scanBasePackages / basePackageClasses - if beans are outside the root package
  • Refresh Context - key phase: BPP registration, auto-configuration, bean creation
  • FailureAnalyzers give readable errors instead of stack traces

Common follow-up questions:

  • What happens if Application is not placed in the root package? - Components in other package branches will not be found by ComponentScan.
  • What is proxyBeanMethods needed for? - CGLIB proxy ensures singleton semantics on @Bean method calls; disabling speeds up startup.
  • Can auto-configuration be disabled? - Yes, via @SpringBootApplication(exclude = …).
  • What does Refresh Context do? - Registers BeanPostProcessors, creates all beans, starts the server.

Red flags (DO NOT say):

  • “@SpringBootApplication just runs the application” - no, it is 3 annotations in one.
  • “You can put @SpringBootApplication on any class” - only on a configuration class with main().
  • “proxyBeanMethods = false is always better” - no, breaks singleton on inter-@Bean calls.
  • “ComponentScan loads all classes into JVM” - no, ASM reads only bytecode.

Related topics:

  • [[23. What does the ComponentScan annotation do]]
  • [[24. What is a Configuration class]]
  • [[22. What is a starter in Spring Boot]]
  • [[26. What does the Autowired annotation do]]