Backend Development 13 min read

Understanding Spring Boot’s @SpringBootApplication and Auto‑Configuration Mechanism

This article explains how Spring Boot’s @SpringBootApplication annotation combines several meta‑annotations, how auto‑configuration is loaded from META‑INF/spring.factories, and how the framework creates and refreshes the application context, including the embedded Tomcat server and custom starter creation.

Architecture Digest
Architecture Digest
Architecture Digest
Understanding Spring Boot’s @SpringBootApplication and Auto‑Configuration Mechanism

The entry point of a Spring Boot application is the class annotated with @SpringBootApplication . This annotation is a composite of @SpringBootConfiguration , @EnableAutoConfiguration and @ComponentScan , which together turn the class into a configuration source, enable automatic bean registration, and scan the package hierarchy.

@SpringBootConfiguration itself is essentially @Configuration , meaning the class can declare @Bean methods without any XML configuration. The underlying @Configuration annotation is meta‑annotated with @Component , so configuration classes are also Spring components.

@EnableAutoConfiguration activates Spring Boot’s auto‑configuration feature. It imports AutoConfigurationImportSelector , which reads the list of auto‑configuration classes from META-INF/spring.factories via SpringFactoriesLoader . These classes are then registered in the bean factory, providing default beans for data sources, web servers, etc.

The selector works together with @AutoConfigurationPackage to automatically add the package of the main class (and its sub‑packages) to the component scan. The Registrar class implements ImportBeanDefinitionRegistrar to register the detected package names.

During application startup, SpringApplication.run() creates a ConfigurableApplicationContext and calls refreshContext() . The refresh process prepares the bean factory, applies post‑processors, registers bean post‑processors, initializes message sources, event multicaster, and finally instantiates all non‑lazy singletons.

For web applications, ServletWebServerApplicationContext overrides onRefresh() to create a web server. The ServletWebServerFactory interface abstracts the server implementation; Spring Boot provides TomcatServletWebServerFactory , JettyServletWebServerFactory , etc. The Tomcat factory creates a Tomcat instance, configures connectors, and returns a WebServer object.

To illustrate custom starters, the article shows a minimal Maven configuration that depends on spring-boot-autoconfigure , a service interface, a properties class annotated with @ConfigurationProperties , and an auto‑configuration class that declares a @Bean guarded by @ConditionalOnMissingBean . The starter is registered in META-INF/spring.factories under org.springframework.boot.autoconfigure.EnableAutoConfiguration , allowing other projects to import the functionality simply by adding the starter dependency.

Overall, the article walks through the inner workings of Spring Boot’s bootstrapping, auto‑configuration loading, context refresh, embedded server creation, and how to extend the framework with custom starters.

Code examples:

@SpringBootApplication
public class StartEurekaApplication {
    public static void main(String[] args) {
        SpringApplication.run(StartEurekaApplication.class, args);
    }
}
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = {
    @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
    @Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class)})
public @interface SpringBootApplication {}
public interface ServletWebServerFactory {
    WebServer getWebServer(ServletContextInitializer... initializers);
}
@Override
public WebServer getWebServer(ServletContextInitializer... initializers) {
    Tomcat tomcat = new Tomcat();
    // configuration omitted for brevity
    return getTomcatWebServer(tomcat);
}
@Configuration
@ConditionalOnClass(GwService.class)
@EnableConfigurationProperties(GwProperties.class)
public class GwAutoConfiguration {
    @Bean
    @ConditionalOnMissingBean
    public GwService gwService() {
        return new GwServiceImpl();
    }
}
BackendJavaSpring BootAnnotationsdependency injectionAuto‑ConfigurationStarter
Architecture Digest
Written by

Architecture Digest

Focusing on Java backend development, covering application architecture from top-tier internet companies (high availability, high performance, high stability), big data, machine learning, Java architecture, and other popular fields.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.