Understanding Spring Boot’s Auto‑Configuration and “Convention over Configuration” Principles
This article explains how Spring Boot simplifies Java application development by providing out‑of‑the‑box starters, automatic configuration via spring.factories, and convention‑over‑configuration rules, covering the underlying annotations, Maven structure, starter dependencies, main class, and the use of @ConfigurationProperties to bind YAML settings.
Preface
Spring, literally meaning “spring” in Chinese, once brought a springtime to Java developers, but as projects grew its XML configuration became increasingly cumbersome, often taking two hours to configure for five minutes of coding.
SpringBoot Introduction
Source: Baidu Encyclopedia
Spring Boot, provided by the Pivotal team, is a new framework designed to simplify the initial setup and development of Spring applications. It uses a specific configuration approach that eliminates the need for boilerplate XML.
Features of SpringBoot
Creates standalone Spring applications and can produce executable JARs and WARs via Maven or Gradle plugins.
Embeds Tomcat, Jetty, or other servlet containers.
Provides auto‑configuration "starter" POMs to simplify Maven configuration.
Attempts to auto‑configure the Spring container as much as possible.
Offers ready‑made features such as metrics, health checks, and externalized configuration.
No code generation and no XML configuration required.
My Understanding
SpringBoot gives the impression of a project that starts Spring with minimal effort. Previously, launching a Spring project required many XML files; with SpringBoot, no XML is needed, allowing developers to focus on business logic.
It adopts JavaConfig style configuration, using @EnableXXXX annotations instead of direct XML, resulting in cleaner code and widespread adoption.
The simplification stems from two design strategies: Out‑of‑the‑box and Convention over Configuration .
Out‑of‑the‑Box Principle
To experience this, use Spring Initializr: https://start.spring.io/ . Fill in the project name, select required dependencies, and generate a runnable SpringBoot project.
After generating, import the project into an IDE (Eclipse or IDEA) and run it.
Full project structure:
Start the application:
Access http://localhost:8080/ to see the successful startup.
Out‑of‑the‑Box Mechanism Analysis
Comparison with SSM Configuration
In the out‑of‑the‑box approach we implicitly include a Spring MVC component without any explicit configuration, unlike the traditional SSM setup.
spring‑web.xml:
text/html;charset=UTF-8web.xml:
spring-dispatcher
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:spring/spring-*.xml
spring-dispatcher
/Compared with the SSM configuration, SpringBoot eliminates the need for these XML files, demonstrating the convenience of the out‑of‑the‑box approach.
From pom.xml
Every SpringBoot project has a parent dependency:
org.springframework.boot
spring-boot-starter-parent
2.2.1.RELEASEThe parent brings a large set of dependencies and version management, so developers do not need to specify versions for SpringBoot starters.
Inside the parent, the spring-boot-dependencies module defines core dependencies and resource handling:
true
${basedir}/src/main/resources
**/application*.yml
**/application*.yaml
**/application*.properties
${basedir}/src/main/resources
**/application*.yml
**/application*.yaml
**/application*.propertiesThus, the parent provides a ready‑made dependency set and resource configuration.
Starter
The starter is a dependency that bundles all required libraries for a particular scenario. For example, the basic starter:
org.springframework.boot
spring-boot-starter
2.2.1.RELEASE
compile
org.springframework.boot
spring-boot-autoconfigure
2.2.1.RELEASE
compile
org.springframework.boot
spring-boot-starter-logging
2.2.1.RELEASE
compile
jakarta.annotation
jakarta.annotation-api
1.3.5
compile
org.springframework
spring-core
5.2.1.RELEASE
compile
org.yaml
snakeyaml
1.25
runtimeAdding the appropriate starter (e.g., spring-boot-starter-web ) automatically pulls in all necessary dependencies.
Main Program (Important)
//@SpringBootApplication marks a SpringBoot application
@SpringBootApplication
public class SpringbootdemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootdemoApplication.class, args);
}
}The @SpringBootApplication annotation is a composite of three core annotations:
@SpringBootConfiguration // essentially @Configuration
@EnableAutoConfiguration // core auto‑configuration
@ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class), @Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })The two crucial parts are @SpringBootConfiguration (which is just @Configuration) and @EnableAutoConfiguration .
Auto‑Configuration Mechanism
@EnableAutoConfiguration imports AutoConfigurationImportSelector , which loads candidate configuration classes from META-INF/spring.factories using SpringFactoriesLoader.loadFactoryNames . It asserts that the list is not empty, otherwise it throws an error.
The spring.factories file contains fully‑qualified names of all auto‑configuration classes, such as WebMvcAutoConfiguration . Only those classes whose conditions (e.g., @ConditionalOnClass ) are satisfied are actually applied.
Thus, when a starter is added to pom.xml , the corresponding auto‑configuration classes become active, achieving the “out‑of‑the‑box” effect.
Convention over Configuration
SpringBoot defines many conventions: Configuration file locations: file:./config/ , file:./ , classpath:/config/ , classpath:/ (in this order). Default configuration file names: application.yml , application.yaml , application.properties . Component scanning starts from the package of the main class and its sub‑packages.
Reading yml with @ConfigurationProperties
Properties defined in application.yml can be bound to POJOs using @ConfigurationProperties :
@Component
@ConfigurationProperties(prefix = "object")
public class TestConfig {
private String name;
private String blogUrl;
// getters and setters omitted for brevity
}With the following YAML:
object:
name: Object
blogurl: blog.objectspace.cnSpring injects the values automatically, as demonstrated in a test class that autowires TestConfig and prints the properties.
Therefore, @ConfigurationProperties bridges YAML configuration to Java beans, completing the convention‑driven configuration model.
Author: 工匠初心 (cnblogs.com/LiaHon/p/11257805.html)
Instead of searching for interview questions online, follow us now!
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.