Understanding Spring Boot Auto-Configuration: From Basics to Custom Starter
This article explains why Spring Boot was created, outlines its key features such as convention over configuration and automatic wiring, dives into the inner workings of annotations like @EnableAutoConfiguration, demonstrates how SpringFactoriesLoader implements SPI, and guides you through building a simple starter component step by step.
Preface
If we want to use a traditional Spring application, we need to configure many XML files, which becomes cumbersome as the project grows; Spring Boot was created to simplify this.
What is Spring Boot
In October 2012, a request was made in Spring Jira to support container‑less web applications, which helped spark the creation of Spring Boot. Its purpose is to eliminate tedious XML configuration while retaining the core Spring framework, allowing rapid creation of micro‑service applications.
Creates standalone Spring applications.
Embeds Tomcat, Jetty or Undertow (no WAR deployment needed).
Provides sensible defaults to simplify configuration.
Automatically configures Spring and third‑party libraries.
Offers production‑ready features such as metrics, health checks, and externalized configuration.
Requires no code generation or XML configuration.
The two most important features are "convention over configuration" and "auto‑configuration".
Convention Over Configuration
Spring Boot follows conventions such as:
Placing configuration files in resources directory.
Compiled files are placed in target directory.
Packaging defaults to JAR format.
Configuration files are application.yml, application.yaml or application.properties.
Activating profiles via spring.profiles.active.
Auto‑Configuration
The core of Spring Boot is auto‑configuration. By adding the @SpringBootApplication annotation, a Spring Boot application can start with minimal setup. This annotation is a composite of three key annotations:
@SpringBootConfiguration, @EnableAutoConfiguration, @ComponentScan@SpringBootConfiguration
This is essentially @Configuration, marking the class as a configuration source for the Spring IoC container.
@ComponentScan
Defines the packages to scan for components, equivalent to <context:component-scan> in XML.
@EnableAutoConfiguration
This annotation triggers auto‑configuration. It is itself a composite of @AutoConfigurationPackage and @Import.
@Import
Used to import additional configuration classes. Example: public class TestImport {} To make TestImport available, a configuration class can import it:
@Configuration
@Import(TestImport.class)
public class MyConfiguration {}AutoConfigurationImportSelector
This selector reads META-INF/spring.factories to find classes listed under the key
org.springframework.boot.autoconfigure.EnableAutoConfiguration. Spring Boot scans all JARs for this file, aggregates the listed classes, removes duplicates, and registers them for auto‑configuration.
SPI Mechanism
SpringBoot uses SpringFactoriesLoader to load spring.factories, which follows the Service Provider Interface (SPI) pattern. SPI allows developers to program to interfaces, enabling easy swapping of implementations.
Example in Java’s DriverManager shows how a driver is loaded from META-INF/services/java.sql.Driver using ServiceLoader.
@AutoConfigurationPackage
This annotation ultimately imports a registrar that adds the package of the main application class to the auto‑configuration scan path.
Writing a Custom Starter
To create a simple starter that provides FastJSON serialization:
Create a Spring Boot project named lonelyWolf-spring-boot-starter.
Modify pom.xml to include spring-boot-starter and fastjson dependencies.
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.0</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.72</version>
</dependency>
</dependencies>Implement a serialization class:
public class JsonSerial {
public <T> String serial(T t) {
return JSONObject.toJSONString(t);
}
}Create an auto‑configuration class to expose the bean:
@Configuration
public class MyAutoConfiguration {
@Bean
public JsonSerial jsonSerial() {
return new JsonSerial();
}
}Package the starter as a JAR and add a META-INF/spring.factories file:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.lonely.wolf.note.MyAutoConfigurationWhen the starter is added as a dependency to another Spring Boot project, the JsonSerial bean becomes available for injection.
Summary
This article introduced the motivation behind Spring Boot, explained its convenience, dissected the auto‑configuration mechanism, and demonstrated how to build a simple starter component to experience the power of Spring Boot’s auto‑configuration.
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.
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.
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.
