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.

Selected Java Interview Questions
Selected Java Interview Questions
Selected Java Interview Questions
Understanding Spring Boot’s Auto‑Configuration and “Convention over Configuration” Principles

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:

<code style="padding:0.5em;font-size:12px;color:white;line-height:15px;display:block;font-family:Consolas,Inconsolata,Courier,monospace;">
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
    <!-- 配置SpringMVC -->
    <!-- 1.开启SpringMVC注解模式 -->
    <!-- 简化配置: (1)自动注册DefaultAnnotationHandlerMapping,AnnotationMethodHandlerAdapter
         (2)提供一系列:数据绑定,数字和日期的format @NumberFormat, @DateTimeFormat,json默认读写支持 -->
    <mvc:annotation-driven />
    <!-- 2.静态资源默认servlet配置 (1)加入对静态资源的处理:js,gif,png (2)允许使用"/"做整体映射 -->
    <mvc:resources mapping="/resources/**" location="/resources/" />
    <mvc:default-servlet-handler />
    <!-- 3.定义视图解析器 -->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/html/" />
        <property name="suffix" value=".html" />
    </bean>
    <!-- 文件上传解析器 -->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="defaultEncoding" value="utf-8" />
        <property name="maxUploadSize" value="10485760000" />
        <!-- 最大上传文件大小 -->
        <property name="maxInMemorySize" value="20971520" />
    </bean>
    <!-- 在spring-mvc.xml文件中加入这段配置后,spring返回给页面的都是utf-8编码了 -->
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
            <list>
                <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                    <property name="supportedMediaTypes">
                        <list>
                            <value>text/html;charset=UTF-8</value>
                        </list>
                    </property>
                </bean>
            </list>
        </property>
    </bean>
    <!-- 4.扫描web相关的bean -->
    <context:component-scan base-package="com.SchoolShop.o2o.web" />
    <!-- 5.权限拦截器 -->
</beans>
</code>

web.xml:

<code style="padding:0.5em;font-size:12px;color:white;line-height:15px;display:block;font-family:Consolas,Inconsolata,Courier,monospace;">
<servlet>
  <servlet-name>spring-dispatcher</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring/spring-*.xml</param-value>
  </init-param>
</servlet>
<servlet-mapping>
  <servlet-name>spring-dispatcher</servlet-name>
  <!-- 默认匹配所有请求 -->
  <url-pattern>/</url-pattern>
</servlet-mapping>
</code>

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:

<code style="padding:0.5em;font-size:12px;color:white;line-height:15px;display:block;font-family:Consolas,Inconsolata,Courier,monospace;">
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.1.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
</code>

The 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:

<code style="padding:0.5em;font-size:12px;color:white;line-height:15px;display:block;font-family:Consolas,Inconsolata,Courier,monospace;">
<resources>
  <resource>
    <filtering>true</filtering>
    <directory>${basedir}/src/main/resources</directory>
    <includes>
      <include>**/application*.yml</include>
      <include>**/application*.yaml</include>
      <include>**/application*.properties</include>
    </includes>
  </resource>
  <resource>
    <directory>${basedir}/src/main/resources</directory>
    <excludes>
      <exclude>**/application*.yml</exclude>
      <exclude>**/application*.yaml</exclude>
      <exclude>**/application*.properties</exclude>
    </excludes>
  </resource>
</resources>
</code>

Thus, 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:

<code style="padding:0.5em;font-size:12px;color:white;line-height:15px;display:block;font-family:Consolas,Inconsolata,Courier,monospace;">
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter</artifactId>
  <version>2.2.1.RELEASE</version>
  <scope>compile</scope>
</dependency>
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-autoconfigure</artifactId>
  <version>2.2.1.RELEASE</version>
  <scope>compile</scope>
</dependency>
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-logging</artifactId>
  <version>2.2.1.RELEASE</version>
  <scope>compile</scope>
</dependency>
<dependency>
  <groupId>jakarta.annotation</groupId>
  <artifactId>jakarta.annotation-api</artifactId>
  <version>1.3.5</version>
  <scope>compile</scope>
</dependency>
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-core</artifactId>
  <version>5.2.1.RELEASE</version>
  <scope>compile</scope>
</dependency>
<dependency>
  <groupId>org.yaml</groupId>
  <artifactId>snakeyaml</artifactId>
  <version>1.25</version>
  <scope>runtime</scope>
</dependency>
</code>

Adding the appropriate starter (e.g., spring-boot-starter-web) automatically pulls in all necessary dependencies.

Main Program (Important)

<code style="padding:0.5em;font-size:12px;color:white;line-height:15px;display:block;font-family:Consolas,Inconsolata,Courier,monospace;">
//@SpringBootApplication marks a SpringBoot application
@SpringBootApplication
public class SpringbootdemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringbootdemoApplication.class, args);
    }
}
</code>

The @SpringBootApplication annotation is a composite of three core annotations:

<code style="padding:0.5em;font-size:12px;color:white;line-height:15px;display:block;font-family:Consolas,Inconsolata,Courier,monospace;">
@SpringBootConfiguration // essentially @Configuration
@EnableAutoConfiguration   // core auto‑configuration
@ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class), @Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
</code>

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:

<code style="padding:0.5em;font-size:12px;color:white;line-height:15px;display:block;font-family:Consolas,Inconsolata,Courier,monospace;">
@Component
@ConfigurationProperties(prefix = "object")
public class TestConfig {
    private String name;
    private String blogUrl;
    // getters and setters omitted for brevity
}
</code>

With the following YAML:

<code style="padding:0.5em;font-size:12px;color:white;line-height:15px;display:block;font-family:Consolas,Inconsolata,Courier,monospace;">
object:
  name: Object
  blogurl: blog.objectspace.cn
</code>

Spring 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!

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 Bootauto-configurationConvention over Configuration
Selected Java Interview Questions
Written by

Selected Java Interview Questions

A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!

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.