Deep Dive into SpringBoot Auto‑Configuration and Startup Process
This article provides a comprehensive walkthrough of SpringBoot’s auto‑configuration mechanism, covering common annotations, configuration property binding, import strategies, conditional beans, the application startup flow, and the role of META‑INF/spring.factories in loading auto‑configuration classes.
SpringBoot dramatically simplifies Java backend development by providing zero‑configuration, auto‑configured components; this article explains how the auto‑configuration works and how the application starts.
1. Frequently Used SpringBoot Annotations
@Value injects literal values or placeholders from configuration files, equivalent to XML <property> entries.
@Component
public class Person {
@Value("i am name")
private String name;
}@ConfigurationProperties binds a group of related properties to a POJO, avoiding repetitive @Value usage.
# application.properties
person.name=kundy
person.age=13
person.sex=male @Component
@ConfigurationProperties(prefix = "person")
public class Person {
private String name;
private Integer age;
private String sex;
}@Import can import regular Java classes, custom ImportSelector, or ImportBeanDefinitionRegistrar implementations, turning them into beans.
// Direct import example
@Import({Circle.class})
@Configuration
public class MainConfig { }
// ImportSelector example
public class MyImportSelector implements ImportSelector {
@Override
public String[] selectImports(AnnotationMetadata metadata) {
return new String[]{"com.example.Triangle"};
}
}
@Import({Circle.class, MyImportSelector.class})
@Configuration
public class MainConfigTwo { }@Conditional activates a configuration only when a custom condition evaluates to true.
@Configuration
@Conditional(MyCondition.class)
public class ConditionConfig {
@Bean
public ConditionBean conditionBean() { return new ConditionBean(); }
}2. SpringBoot Startup Process
The SpringApplication.run() method creates a SpringApplication instance and executes the run() method, which performs environment preparation, banner printing, application‑context creation, context preparation, refresh, and finally invokes ApplicationRunner and CommandLineRunner beans.
public ConfigurableApplicationContext run(String... args) {
// ...prepare environment, create context, refresh, call runners...
}3. Auto‑Configuration Principle
The core annotation @SpringBootApplication combines @SpringBootConfiguration, @EnableAutoConfiguration, and @ComponentScan. @EnableAutoConfiguration imports AutoConfigurationImportSelector, which reads META‑INF/spring.factories to obtain a list of auto‑configuration classes.
Each auto‑configuration class is typically annotated with conditions such as @ConditionalOnWebApplication, @ConditionalOnClass, and @ConditionalOnProperty, and may enable configuration properties via @EnableConfigurationProperties.
@Configuration
@EnableConfigurationProperties(HttpProperties.class)
@ConditionalOnWebApplication(type = Type.SERVLET)
@ConditionalOnClass(CharacterEncodingFilter.class)
@ConditionalOnProperty(prefix = "spring.http.encoding", name = "enabled", matchIfMissing = true)
public class HttpEncodingAutoConfiguration { }The corresponding HttpProperties class is bound to spring.http.* properties using @ConfigurationProperties.
@ConfigurationProperties(prefix = "spring.http")
public class HttpProperties { }Conclusion
SpringBoot’s auto‑configuration loads many candidate classes from META‑INF/spring.factories, filters them based on the current environment and classpath, and registers beans automatically, enabling developers to focus on business logic without manual wiring.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Selected Java Interview Questions
A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
