How SpringBoot Auto‑Configuration Works: From @SpringBootApplication to Conditional Annotations

This article explains how SpringBoot simplifies Spring’s XML configuration by providing convention‑over‑configuration and automatic setup, walks through the key annotations such as @SpringBootApplication, @EnableAutoConfiguration, and @ComponentScan, and details the internal loading mechanism using META‑INF/spring.factories and conditional annotations.

Java High-Performance Architecture
Java High-Performance Architecture
Java High-Performance Architecture
How SpringBoot Auto‑Configuration Works: From @SpringBootApplication to Conditional Annotations

SpringBoot was created to simplify the cumbersome XML configuration of Spring, offering convention‑over‑configuration and automatic setup for many libraries, while still being built on the core Spring framework.

SpringBoot的特点

约定大于配置
自动装配

内嵌容器,创建独立的 Spring 应用

内置 JUnit、SpringBoot Test 等测试框架,简化测试

提供度量、运行状况检查和外部化配置等生产特性

无需生成代码或 XML 配置即可启动服务

启动类

The entry point is a class annotated with @SpringBootApplication that contains a main method invoking SpringApplication.run:

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

@SpringBootApplication

This meta‑annotation combines three annotations: @SpringBootConfiguration (a thin wrapper around @Configuration) @EnableAutoConfiguration (enables SpringBoot’s auto‑configuration) @ComponentScan (scans the package of the annotated class for components)

@SpringBootConfiguration

Marks the class as a Spring configuration class:

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration
@Indexed
public @interface SpringBootConfiguration {
    @AliasFor(annotation = Configuration.class)
    boolean proxyBeanMethods() default true;
}

@ComponentScan

Provides the same functionality as the XML element <context:component-scan>, allowing explicit package definitions via basePackages or basePackageClasses.

@EnableAutoConfiguration

Triggers the import of auto‑configuration classes. Its source looks like:

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import({AutoConfigurationImportSelector.class})
public @interface EnableAutoConfiguration {
    String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration";
    Class<?>[] exclude() default {};
    String[] excludeName() default {};
}

@AutoConfigurationPackage

Registers the package of the main configuration class for component scanning:

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Import({Registrar.class})
public @interface AutoConfigurationPackage {
    String[] basePackages() default {};
    Class<?>[] basePackageClasses() default {};
}

SpringFactoriesLoader

Loads factory class names from all META-INF/spring.factories files on the classpath. The key

org.springframework.boot.autoconfigure.EnableAutoConfiguration

maps to a list of candidate auto‑configuration classes.

public static List<String> loadFactoryNames(Class<?> factoryType, @Nullable ClassLoader classLoader) {
    ClassLoader classLoaderToUse = (classLoader != null ? classLoader : SpringFactoriesLoader.class.getClassLoader());
    String factoryTypeName = factoryType.getName();
    return loadSpringFactories(classLoaderToUse).getOrDefault(factoryTypeName, Collections.emptyList());
}

The loadSpringFactories method reads every META-INF/spring.factories resource, parses the properties, and builds a map of factory type to implementation class names, removing duplicates.

Conditional Annotations

Auto‑configuration classes are filtered by various @Conditional annotations, such as:

@ConditionalOnClass
@ConditionalOnMissingClass
@ConditionalOnBean
@ConditionalOnMissingBean
@ConditionalOnSingleCandidate
@ConditionalOnExpression
@ConditionalOnProperty
@ConditionalOnResource
@ConditionalOnJndi
@ConditionalOnJava
@ConditionalOnWebApplication
@ConditionalOnNotWebApplication

@Import Support

@Import

can import configuration via:

Classes annotated with @Configuration Implementations of ImportSelector Implementations of

ImportBeanDefinitionRegistrar

流程总结图

SpringBoot auto‑configuration flow diagram
SpringBoot auto‑configuration flow diagram
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.

JavaannotationsSpringBootauto-configurationSpring Framework
Java High-Performance Architecture
Written by

Java High-Performance Architecture

Sharing Java development articles and resources, including SSM architecture and the Spring ecosystem (Spring Boot, Spring Cloud, MyBatis, Dubbo, Docker), Zookeeper, Redis, architecture design, microservices, message queues, Git, etc.

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.