Understanding Spring Boot @SpringBootApplication and Its Auto‑Configuration Mechanism

This article explains how Spring Boot's @SpringBootApplication annotation combines several meta‑annotations, how auto‑configuration classes are discovered from META‑INF/spring.factories, and how the framework creates and refreshes the application context, including the embedded Tomcat web server, with illustrative code snippets.

Top Architect
Top Architect
Top Architect
Understanding Spring Boot @SpringBootApplication and Its Auto‑Configuration Mechanism

Spring Boot's entry point is the main configuration class annotated with @SpringBootApplication, which is a composite annotation that includes @SpringBootConfiguration, @EnableAutoConfiguration and @ComponentScan. @SpringBootConfiguration is essentially @Configuration, turning the class into a configuration component that can register additional beans. @EnableAutoConfiguration activates the auto‑configuration mechanism; it imports AutoConfigurationImportSelector which loads class names from META-INF/spring.factories and registers them as beans.

The AutoConfigurationPackage annotation ensures that the package of the class annotated with @SpringBootApplication and its sub‑packages are scanned and added to the Spring container.

During startup, SpringApplication.run creates the application context, prepares the environment, and calls refreshContext. The refresh process prepares the bean factory, invokes bean‑factory post‑processors, registers bean post‑processors, initializes special beans, and finally instantiates all non‑lazy singletons.

For web applications, ServletWebServerApplicationContext overrides onRefresh to create a web server via ServletWebServerFactory. The default implementation TomcatServletWebServerFactory builds an embedded Tomcat instance, configures connectors, and prepares the servlet context.

The article also shows how to create a custom starter: define a Maven parent, add spring-boot-autoconfigure as a dependency, write a configuration properties class, an auto‑configuration class annotated with @ConditionalOnClass and @EnableConfigurationProperties, expose a bean with @ConditionalOnMissingBean, and register the auto‑configuration in spring.factories (e.g.,

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.gw.GwAutoConfiguration

).

These steps illustrate the complete flow from the high‑level @SpringBootApplication annotation down to the low‑level creation of the embedded Tomcat server and how developers can extend the mechanism with their own starters.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

javabackend-developmentSpring Bootannotationsauto-configuration
Top Architect
Written by

Top Architect

Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.

0 followers
Reader feedback

How this landed with the community

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.