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

相关推荐
AI木马人3 分钟前
20.人工智能实战:大模型项目如何从 Demo 走向生产?一套可落地的上线验收清单与工程治理方案
java·开发语言·人工智能
CandyU25 分钟前
Unity —— 反射
java·开发语言
楼田莉子7 分钟前
仿照Muduo的高并发服务器:EventLoop模块及与TimeWheel模块联调
java·开发语言
小雅痞9 分钟前
[Java][Leetcode middle] 3. 无重复字符的最长子串
java·开发语言·leetcode
SamDeepThinking10 分钟前
为什么你做技术方案总是漏掉边界情况
java·后端·程序员
逻辑驱动的ken20 分钟前
Java高频面试考点场景题21
java·开发语言·面试·职场和发展·求职招聘
你好潘先生21 分钟前
Next.js + Spring Boot 实现 AI 多模型并行对话系统(架构设计与关键实现)
spring boot·向量检索·next.js·pgvector·ai对话·多模型对比·sse流式输出
苍煜22 分钟前
SpringBoot单体应用到分布式下的数据库锁、事务、Redis事务、分布式锁、分布式事务协调
数据库·spring boot·分布式
Dylan的码园25 分钟前
springBoot与Web后端基础
前端·spring boot·后端
番茄去哪了28 分钟前
单体转微服务:正确的拆分思路与实战原则(上)
java·微服务·架构