进阶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,注释自动转换中文)

相关推荐
朝新_1 小时前
【多线程初阶】阻塞队列 & 生产者消费者模型
java·开发语言·javaee
立莹Sir1 小时前
Calendar类日期设置进位问题
java·开发语言
XMYX-02 小时前
Spring Boot + Prometheus 实现应用监控(基于 Actuator 和 Micrometer)
spring boot·后端·prometheus
季鸢3 小时前
Java设计模式之状态模式详解
java·设计模式·状态模式
@yanyu6664 小时前
springboot实现查询学生
java·spring boot·后端
ascarl20104 小时前
准确--k8s cgroup问题排查
java·开发语言
magic 2454 小时前
Lombok 的 @Data 注解失效,未生成 getter/setter 方法引发的HTTP 406 错误
java
爱敲代码的憨仔4 小时前
分布式协同自动化办公系统-工作流引擎-流程设计
java·flowable·oa
酷爱码4 小时前
Spring Boot项目中JSON解析库的深度解析与应用实践
spring boot·后端·json
纪元A梦4 小时前
分布式拜占庭容错算法——PBFT算法深度解析
java·分布式·算法