20220507 Spring 5.3.8 官方文档【归档】

版本关系

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 中没有此图

spring overview

目录

文档地址

新功能、升级说明、支持的版本和其他主题,独立于发布节奏,在项目的 Github Wiki 上进行外部维护。

章节 blog 描述
Overview Overview 历史、设计理念、反馈、入门
Core 1. The IoC Container
2. Resources
3. Validation, Data Binding, and Type Conversion
4. Spring Expression Language (SpEL)
5. Aspect Oriented Programming with Spring
6. Spring AOP API
7. Null-safety
8. Data Buffers and Codecs
9. Logging
10. Appendix
IoC Container, Events, Resources, i18n, Validation, Data Binding, Type Conversion, SpEL, AOP
Testing Mock Objects, TestContext Framework, Spring MVC Test, WebTestClient
Data Access 1. Transaction Management
2. DAO Support
3. Data Access with JDBC
4. Data Access with R2DBC
5. Retrieving Auto-generated Keys
6. Object Relational Mapping (ORM) Data Access
7. Marshalling XML by Using Object-XML Mappers
8. Appendix
Transactions, DAO Support, JDBC, R2DBC, O/R Mapping, XML Marshalling
Web Servlet 1. Spring Web MVC
2. REST Clients
4. WebSockets
5. Other Web Frameworks
Spring MVC, WebSocket, SockJS, STOMP Messaging
Web Reactive Spring WebFlux、WebClient、WebSocket、RSocket
Integration 1. REST Endpoints
2. Remoting and Web Services
3. Enterprise JavaBeans (EJB) Integration
4. JMS (Java Message Service)
5. JMX
6. Email
7. Task Execution and Scheduling
8. Cache Abstraction
9. Appendix
Remoting, JMS, JCA, JMX, Email, Tasks, Scheduling, Caching
Languages Kotlin, Groovy, Dynamic Languages
Appendix Appendix miscellaneous topics

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

关键步骤

  1. 实例化前

    1. InstantiationAwareBeanPostProcessor.postProcessBeforeInstantiation
  2. 实例化阶段 createBeanInstance

    以下方法并列:

    • obtainFromSupplier
    • instantiateUsingFactoryMethod
    • autowireConstructor
    • instantiateBean
  3. MergedBeanDefinitionPostProcessor.postProcessMergedBeanDefinition

  4. 属性赋值 populateBean

    1. InstantiationAwareBeanPostProcessor.postProcessAfterInstantiation
    2. autowireByName
    3. autowireByType
    4. InstantiationAwareBeanPostProcessor.postProcessProperties
    5. applyPropertyValues
  5. 初始化阶段 initializeBean

    1. invokeAwareMethods
      1. BeanNameAware
      2. BeanClassLoaderAware
      3. BeanFactoryAware
    2. BeanPostProcessor.postProcessBeforeInitialization
      1. 部分 Aware 接口 ApplicationContextAwareProcessor#postProcessBeforeInitialization
        1. EnvironmentAware
        2. EmbeddedValueResolverAware
        3. ResourceLoaderAware
        4. ApplicationEventPublisherAware
        5. MessageSourceAware
        6. ApplicationStartupAware
        7. ApplicationContextAware
      2. 执行初始化方法
        1. @PostConstruct : CommonAnnotationBeanPostProcessor#postProcessBeforeInitialization
    3. invokeInitMethods
      1. InitializingBean#afterPropertiesSet
      2. 自定义初始化方法:invokeCustomInitMethod
    4. BeanPostProcessor.postProcessAfterInitialization
      1. AOP 增强:AnnotationAwareAspectJAutoProxyCreator#postProcessAfterInitialization

参考资料

posted @ 2022-06-09 21:34  流星<。)#)))≦  阅读(152)  评论(0编辑  收藏  举报