2025/4/6
向全栈工程师迈进!
一、自动配置
所谓的自动配置原理就是遵循约定大约配置的原则,在boot工程程序启动后,起步依赖中的一些bean对象会自动的注入到IOC容器中。
在讲解Spring Boot 中bean对象的管理的时候,我们注入bean对象的过程如下,但这种方式不是自动注入的。
并没有达到自动配置。 而自动配置是当程序在引入spring-boot-starter-web 起步依赖,boot工程在启动后,会自动往ioc容器中注入DispatcherServlet等bean对象,这就是自动配置。那自动配置的原理是什么呢?
二、自动配置原理
在这里只先引入springboot起步核心依赖,而并没有引入spring-boot-starter-web起步依赖。
<!--引入springboot起步核心依赖-->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId>
</dependency>
在没有引入spring-boot-starter-web依赖之前,我们尝试输出spring-boot-starter-web起步依赖会自动注入的bean对象,没有在pom.xml文件中添加改起步依赖时,输出发现根本没有。
package com.example.demo;import org.springframework.context.ApplicationContext;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class Demo1Application {public static void main(String[] args) {ApplicationContext context = SpringApplication.run(Demo1Application.class, args);System.out.println(context.getBean("dispatcherServlet"));}}
导入依赖spring-boot-starter-web后,再次尝试输出其bean对象,发现成功,当引入依赖后,其bean对象被自动的注入了。
2.1 查看@SpringBootApplication注解源码
是组合了以下三个注解
- @SpringBootConfiguration 其本身也是一个组合注解
- @EnableAutoConfiguration 其本身也是一个组合注解
- @ComponentScan 是Bean扫描的注解
@SpringBootConfiguration的内部代码:
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//package org.springframework.boot;import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.AliasFor;
import org.springframework.stereotype.Indexed;@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration
@Indexed
public @interface SpringBootConfiguration {@AliasFor(annotation = Configuration.class)boolean proxyBeanMethods() default true;
}
可以发现在@SpringBootConfiguration内部也是有@Configuration注解的,所以当在启动类上添加注解@SpringBootApplication其实就是也在启动类上添加了@Configuration注解,所以启动类也是一个配置类。
@EnableAutoConfiguration的内部代码:
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//package org.springframework.boot.autoconfigure;import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.context.annotation.Import;@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 {};
}
@EnableAutoConfiguration是自动配置的核心注解,该@EnableAutoConfiguration注解其实组合了@AutoConfigurationPackage和@Import两个注解,在@Import注解中导入实现类AutoConfigurationImportSelector。其AutoConfigurationImportSelector的实现类如下(代码不全):
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//package org.springframework.boot.autoconfigure;import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
........
import org.springframework.util.StringUtils;public class AutoConfigurationImportSelector implements DeferredImportSelector, BeanClassLoaderAware, ResourceLoaderAware, BeanFactoryAware, EnvironmentAware, Ordered {private static final AutoConfigurationEntry EMPTY_ENTRY = new AutoConfigurationEntry();private static final String[] NO_IMPORTS = new String[0];private static final Log logger = LogFactory.getLog(AutoConfigurationImportSelector.class);private static final String PROPERTY_NAME_AUTOCONFIGURE_EXCLUDE = "spring.autoconfigure.exclude";private ConfigurableListableBeanFactory beanFactory;private Environment environment;private ClassLoader beanClassLoader;private ResourceLoader resourceLoader;private ConfigurationClassFilter configurationClassFilter;public AutoConfigurationImportSelector() {}............private MetadataReaderFactory getMetadataReaderFactory() {try {return (MetadataReaderFactory)this.beanFactory.getBean("org.springframework.boot.autoconfigure.internalCachingMetadataReaderFactory", MetadataReaderFactory.class);} catch (NoSuchBeanDefinitionException var2) {return new CachingMetadataReaderFactory(this.resourceLoader);}}}
}
可以发现在这个实现类(AutoConfigurationImportSelector
)中,会从 META-INF/spring.factories
文件中读取所有自动配置类,它读取的 key 是:
org.springframework.boot.autoconfigure.EnableAutoConfiguration
加载所有配置类,条件匹配后注册为 Bean。
2.2 自动配置原理——举个栗子🧩
核心:
AutoConfigurationImportSelector 会从 META-INF/spring.factories 文件中读取所有自动配置类,它读取的 key 是:
org.springframework.boot.autoconfigure.EnableAutoConfiguration
。
✅ 你写了一个 Spring Boot 项目,启动类是这样的:
@SpringBootApplication
public class MyApp {public static void main(String[] args) {SpringApplication.run(MyApp.class, args);}
}
这个注解其实包含了 @EnableAutoConfiguration
,表示:
“嘿 SpringBoot,我想用你自动帮我配置一些常见功能,比如数据库、Web、Redis 等!”
🧠 那 SpringBoot 是怎么知道你要配置啥的?
这时候就轮到 AutoConfigurationImportSelector 出场了!
它是一个“选择器”,全名叫:
org.springframework.boot.autoconfigure.AutoConfigurationImportSelector
它的作用是:
🧭“我去找一个叫
spring.factories
的文件,里面写了:SpringBoot 应该自动配置哪些类。我一个个加载它们,看看哪个需要用,哪个可以跳过。”
📂 spring.factories 是什么?
它是一个纯文本配置文件,路径在:
classpath:/META-INF/spring.factories
这个文件来自哪个 jar 包?👉 就是 Spring Boot 的依赖包之一:
spring-boot-autoconfigure-xxx.jar
打开看一下这个文件长什么样:
# 配置项的 key
org.springframework.boot.autoconfigure.EnableAutoConfiguration=# 配置的值(每一行都是一个自动配置类,全限定类名)
org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,\
org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration,\
org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration,\
org.springframework.boot.autoconfigure.redis.RedisAutoConfiguration
🌟 所以:Spring Boot 会遍历所有这些类,一个个判断:
-
项目中有没有用到数据库?
-
类路径中有没有 MySQL 驱动?
-
你有没有配置
spring.datasource.url
? -
有没有你自己写的 DataSource Bean?
满足这些条件它就帮你创建这个 Bean;不满足,它就跳过。
如下是我真实项目中其spring.factories文件中的内容。
三、实现自动配置
自动配置全过程如下:
山高路远,我们慢些走,路边有花,远处有彩虹,西边有落日......