版本关系
artifactId | 版本 | doc 地址 |
---|---|---|
spring-cloud-dependencies | 2020.0.3 | |
spring-boot-dependencies | 2.5.2 | |
spring-cloud-starter | 3.0.3 | |
spring-boot-starter-web | 2.5.2 | |
spring-webmvc | 5.3.8 | |
spring-boot-starter | 2.5.2 | |
spring-context | 5.3.8 | doc |
前言
再看 Spring 官方文档,不追求句句翻译,领会其大略,掌握其用法。
会有一些我自己加上去的内容,会以以下标题展示:
- 参考源码
- 使用示例
- 使用说明
依赖管理
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>study.hwj</groupId>
<artifactId>study-spring-framework</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<!-- 文件拷贝时的编码 -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- Maven 插件输出报告的编码格式-->
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<!-- 编译时的编码 -->
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
<boot.dependencies.version>2.5.2</boot.dependencies.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${boot.dependencies.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
Framework 模块
来自 Spring Framework 4.3.x , 5.3.8 中没有此图
目录
新功能、升级说明、支持的版本和其他主题,独立于发布节奏,在项目的 Github Wiki 上进行外部维护。
Spring 两个生命周期
Spring 应用上下文生命周期
参考 小马哥讲 Spring - 第二十章:Spring 应用上下文生命周期
Spring Bean 生命周期
参考 小马哥讲 Spring - 第九章:Spring Bean生命周期(Bean Lifecycle)
doGetBean 过程
org.springframework.beans.factory.support.AbstractBeanFactory#doGetBean
createBean
resolveBeforeInstantiation -- 实例化前
applyBeanPostProcessorsBeforeInstantiation
InstantiationAwareBeanPostProcessor.postProcessBeforeInstantiation
applyBeanPostProcessorsAfterInitialization
BeanPostProcessor.postProcessAfterInitialization
doCreateBean
createBeanInstance -- 实例化
resolveBeanClass
obtainFromSupplier
instantiateUsingFactoryMethod
autowireConstructor
instantiateBean
instantiate
applyMergedBeanDefinitionPostProcessors
MergedBeanDefinitionPostProcessor.postProcessMergedBeanDefinition
populateBean -- 属性赋值
InstantiationAwareBeanPostProcessor.postProcessAfterInstantiation
autowireByName
autowireByType
InstantiationAwareBeanPostProcessor.postProcessProperties, InstantiationAwareBeanPostProcessor.postProcessPropertyValues
applyPropertyValues
org.springframework.beans.factory.support.BeanDefinitionValueResolver#resolveValueIfNecessary
convertForProperty
org.springframework.beans.BeanWrapperImpl#convertForProperty, org.springframework.beans.TypeConverter#convertIfNecessary
initializeBean
invokeAwareMethods -- Aware 接口
BeanNameAware.setBeanName
BeanClassLoaderAware.setBeanClassLoader
BeanFactoryAware.setBeanFactory
applyBeanPostProcessorsBeforeInitialization
BeanPostProcessor.postProcessBeforeInitialization -- 初始化前置处理,例如:执行其他Aware 接口(org.springframework.context.support.ApplicationContextAwareProcessor#invokeAwareInterfaces)
invokeInitMethods -- 初始化方法
applyBeanPostProcessorsAfterInitialization
BeanPostProcessor.postProcessAfterInitialization -- 初始化后置处理,例如:AOP 增强
registerDisposableBeanIfNecessary
关键步骤
-
实例化前
InstantiationAwareBeanPostProcessor.postProcessBeforeInstantiation
-
实例化阶段
createBeanInstance
以下方法并列:
obtainFromSupplier
instantiateUsingFactoryMethod
autowireConstructor
instantiateBean
-
MergedBeanDefinitionPostProcessor.postProcessMergedBeanDefinition
-
属性赋值
populateBean
InstantiationAwareBeanPostProcessor.postProcessAfterInstantiation
autowireByName
autowireByType
InstantiationAwareBeanPostProcessor.postProcessProperties
applyPropertyValues
-
初始化阶段
initializeBean
invokeAwareMethods
BeanNameAware
BeanClassLoaderAware
BeanFactoryAware
BeanPostProcessor.postProcessBeforeInitialization
- 部分
Aware
接口ApplicationContextAwareProcessor#postProcessBeforeInitialization
EnvironmentAware
EmbeddedValueResolverAware
ResourceLoaderAware
ApplicationEventPublisherAware
MessageSourceAware
ApplicationStartupAware
ApplicationContextAware
- 执行初始化方法
@PostConstruct
:CommonAnnotationBeanPostProcessor#postProcessBeforeInitialization
- 部分
invokeInitMethods
InitializingBean#afterPropertiesSet
- 自定义初始化方法:
invokeCustomInitMethod
BeanPostProcessor.postProcessAfterInitialization
- AOP 增强:
AnnotationAwareAspectJAutoProxyCreator#postProcessAfterInitialization
- AOP 增强: