进阶SpringBoot之自动装配原理

pom.xml:

spring-boot-dependencies -> spring-boot-starter-parent

spring-boot-dependencies:核心依赖在父工程,引入依赖时不需要指定版本

启动器:SpringBoot 的启动场景

XML 复制代码
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

比如 spring-boot-starter-web,自动导入 web 环境所有依赖

SpringBoot 会将所有的功能场景都变成一个个的启动器

主程序:

@SpringBootApplication,标注这个类是一个 SpringBoot 的应用,启动类下的所有资源被导入

Application.class,反射加载这个类的对象

java 复制代码
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

//@SpringBootApplication:标注这个类是一个SpringBoot的应用,启动类下的所有资源被导入
@SpringBootApplication
public class HelloWorldApplication {

    public static void main(String[] args) {
        //将SpringBoot应用启动,反射加载这个类的对象
        SpringApplication.run(HelloWorldApplication.class, args);
    }

}

注解:(源码)

  1. @SpringBootApplication -> @SpringBootConfiguration(SpringBoot 的配置)

-> @Configuration(Spring 的配置类)-> @Component(Spring 的一个组件)

  1. @SpringBootApplication -> @EnableAutoConfiguration(自动装配)

-> @AutoConfigurationPackage(自动装配包)

-> @Import({AutoConfigurationPackages.Registrar.class})(自动配置包注册,自动注册包)

  1. @SpringBootApplication -> @EnableAutoConfiguration(自动装配)

-> @Import({AutoConfigurationImportSelector.class})(自动配置导入选择器,自动导入包的核心)

获取所有的配置:

List<String> configurations = this.getCandidateConfigurations(annotationMetadata, attributes);

获取候选的配置:

java 复制代码
    protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) {
        List<String> configurations = ImportCandidates.load(AutoConfiguration.class, this.getBeanClassLoader()).getCandidates();
        Assert.notEmpty(configurations, "No auto configuration classes found in META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports. If you are using a custom packaging, make sure that file is correct.");
        return configurations;
    }

所有的自动配置类都在这

java 复制代码
# ApplicationContext Initializers
org.springframework.context.ApplicationContextInitializer=\
org.springframework.boot.autoconfigure.SharedMetadataReaderFactoryContextInitializer,\
org.springframework.boot.autoconfigure.logging.ConditionEvaluationReportLoggingListener

# Application Listeners
org.springframework.context.ApplicationListener=\
org.springframework.boot.autoconfigure.BackgroundPreinitializer

# Environment Post Processors
org.springframework.boot.env.EnvironmentPostProcessor=\
org.springframework.boot.autoconfigure.integration.IntegrationPropertiesEnvironmentPostProcessor

# Auto Configuration Import Listeners
org.springframework.boot.autoconfigure.AutoConfigurationImportListener=\
org.springframework.boot.autoconfigure.condition.ConditionEvaluationReportAutoConfigurationImportListener

# Auto Configuration Import Filters
org.springframework.boot.autoconfigure.AutoConfigurationImportFilter=\
org.springframework.boot.autoconfigure.condition.OnBeanCondition,\
org.springframework.boot.autoconfigure.condition.OnClassCondition,\
org.springframework.boot.autoconfigure.condition.OnWebApplicationCondition

# Failure Analyzers
org.springframework.boot.diagnostics.FailureAnalyzer=\
org.springframework.boot.autoconfigure.data.redis.RedisUrlSyntaxFailureAnalyzer,\
org.springframework.boot.autoconfigure.diagnostics.analyzer.NoSuchBeanDefinitionFailureAnalyzer,\
org.springframework.boot.autoconfigure.jdbc.DataSourceBeanCreationFailureAnalyzer,\
org.springframework.boot.autoconfigure.jdbc.HikariDriverConfigurationFailureAnalyzer,\
org.springframework.boot.autoconfigure.jooq.NoDslContextBeanFailureAnalyzer,\
org.springframework.boot.autoconfigure.r2dbc.ConnectionFactoryBeanCreationFailureAnalyzer,\
org.springframework.boot.autoconfigure.r2dbc.MissingR2dbcPoolDependencyFailureAnalyzer,\
org.springframework.boot.autoconfigure.r2dbc.MultipleConnectionPoolConfigurationsFailureAnalyzer,\
org.springframework.boot.autoconfigure.r2dbc.NoConnectionFactoryBeanFailureAnalyzer,\
org.springframework.boot.autoconfigure.ssl.BundleContentNotWatchableFailureAnalyzer

# Template Availability Providers
org.springframework.boot.autoconfigure.template.TemplateAvailabilityProvider=\
org.springframework.boot.autoconfigure.freemarker.FreeMarkerTemplateAvailabilityProvider,\
org.springframework.boot.autoconfigure.mustache.MustacheTemplateAvailabilityProvider,\
org.springframework.boot.autoconfigure.groovy.template.GroovyTemplateAvailabilityProvider,\
org.springframework.boot.autoconfigure.thymeleaf.ThymeleafTemplateAvailabilityProvider,\
org.springframework.boot.autoconfigure.web.servlet.JspTemplateAvailabilityProvider

# DataSource Initializer Detectors
org.springframework.boot.sql.init.dependency.DatabaseInitializerDetector=\
org.springframework.boot.autoconfigure.flyway.FlywayMigrationInitializerDatabaseInitializerDetector

# Depends on Database Initialization Detectors
org.springframework.boot.sql.init.dependency.DependsOnDatabaseInitializationDetector=\
org.springframework.boot.autoconfigure.batch.JobRepositoryDependsOnDatabaseInitializationDetector,\
org.springframework.boot.autoconfigure.quartz.SchedulerDependsOnDatabaseInitializationDetector,\
org.springframework.boot.autoconfigure.session.JdbcIndexedSessionRepositoryDependsOnDatabaseInitializationDetector

SpringBoot 所有自动装配都是在启动的时候扫描并加载

spring.factories 所有的自动装配类都在这,但不一定生效,要判断条件是否成立

只有导入了对应的 start,就有对应的启动器,自动装配就会生效,最后配置成功

SpringBoot 在启动时,从类路径 /META-INF/spring.factories 获取指定的值

spring-boot-autoconfigure.jar 会把所有需要导入的组件,以类名的方式返回,这些组件就会被添加到容器

将这些自动配置的类导入容器,自动配置就会生效

(一个小插件 Smart Input,注释自动转换中文)

相关推荐
一灯架构5 小时前
90%的人答错!一文带你彻底搞懂ArrayList
java·后端
Y4090016 小时前
【多线程】线程安全(1)
java·开发语言·jvm
布局呆星7 小时前
SpringBoot 基础入门
java·spring boot·spring
不懂的浪漫7 小时前
mqtt-plus 架构解析(六):多 Broker 管理,如何让一个应用同时连接多个 MQTT 服务
spring boot·分布式·物联网·mqtt·架构
风吹迎面入袖凉7 小时前
【Redis】Redisson的可重入锁原理
java·redis
w6100104667 小时前
cka-2026-ConfigMap
java·linux·cka·configmap
不懂的浪漫7 小时前
mqtt-plus 架构解析(十):从内部项目到开源框架,mqtt-plus 的抽取过程与决策
spring boot·mqtt·架构·开源
语戚8 小时前
力扣 968. 监控二叉树 —— 贪心 & 树形 DP 双解法递归 + 非递归全解(Java 实现)
java·算法·leetcode·贪心算法·动态规划·力扣·
quxuexi8 小时前
网络通信安全与可靠传输:从加密到认证,从状态码到可靠传输
java·安全·web