首页 > 其他分享 >Spring的后置处理器BeanPostProcessor接口的执行流程

Spring的后置处理器BeanPostProcessor接口的执行流程

时间:2023-12-07 11:45:31浏览次数:32  
标签:初始化 自定义 后置 BeanPostProcessor Spring bean 单例

BeanPostProcessor的设计目标主要是提供一种扩展机制,让开发者可以在Spring Bean的初始化阶段进行自定义操作。这种设计理念主要体现了Spring的一种重要原则,即“开放封闭原则”。开放封闭原则强调软件实体(类、模块、函数等等)应该对于扩展是开放的,对于修改是封闭的。在这里,Spring容器对于Bean的创建、初始化、销毁等生命周期进行了管理,但同时开放了BeanPostProcessor这种扩展点,让开发者可以在不修改Spring源码的情况下,实现对Spring Bean生命周期的自定义操作,这种设计理念大大提升了Spring的灵活性和可扩展性。

BeanPostProcessor不是Spring Bean生命周期的一部分,但它是在Spring Bean生命周期中起重要作用的组件

BeanPostProcessor 接口定义了两个方法,postProcessBeforeInitialization和postProcessAfterInitialization:

  • postProcessBeforeInitialization方法会在任何bean初始化回调(如InitializingBean的afterPropertiesSet方法或者自定义的init-method)之前被调用。也就是说,这个方法会在bean的属性已经设置完毕,但还未进行初始化时被调用。
  • postProcessAfterInitialization方法在任何bean初始化回调(比如InitializingBean的afterPropertiesSet或者自定义的初始化方法)之后被调用。这个时候,bean的属性值已经被填充完毕。返回的bean实例可能是原始bean的一个包装。

整体的调用流程如下:

  • AbstractApplicationContext#refresh 核心方法入口
  • AbstractApplicationContext#finishBeanFactoryInitialization 初始化所有(非懒加载)单例bean
  • DefaultListableBeanFactory#preInstantiateSingletons 初始化所有(非懒加载)单例bean

  • AbstractBeanFactory#getBean 根据单例bean名称获取实例
  • AbstractBeanFactory#doGetBean 根据单例bean名称获取实例

(1)DefaultSingletonBeanRegistry#getSingleton 通过bean名称从单例缓存中获取单例对象(可能获取到的实现FactoryBean接口的对象)

(2)若从单例缓存中获取不到对象,则创建bean实例,创建完后也存在单例缓存中

  • AbstractAutowireCapableBeanFactory#createBean 创建bean实例
  • AbstractAutowireCapableBeanFactory#doCreateBean 创建bean实例

  • AbstractAutowireCapableBeanFactory#initializeBean 初始化bean

其中applyBeanPostProcessorsBeforeInitialization方法会调用所有实现了BeanPostProcessor接口的postProcessBeforeInitialization方法

invokeInitMethods方法调用所有实现InitializingBean接口的afterPropertiesSet方法以及自定义的初始化方法

applyBeanPostProcessorsAfterInitialization在初始化之后调用所有实现BeanPostProcessor接口的postProcessAfterInitialization方法

我们总结下BeanPostProcessor的源码上下文方法:

populateBean(beanName, mbd, instanceWrapper); // 给bean进行属性赋值
initializeBean(beanName, exposedObject, mbd)
{
    applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);//bean的初始化前
    invokeInitMethods(beanName, wrappedBean, mbd); // 先执行InitializingBean接口的afterPropertiesSet方法和用户自定义的初始化
    applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);//bean的初始化后
}

 

标签:初始化,自定义,后置,BeanPostProcessor,Spring,bean,单例
From: https://www.cnblogs.com/xfeiyun/p/17880931.html

相关文章

  • springboot监听器&拦截器【转】
    1.监听器https://www.cnblogs.com/elnimo/p/15641367.htmlCommandLineRunnerhttps://blog.csdn.net/qq_34531925/article/details/82527066https://www.cnblogs.com/yanxiaoguo/p/16167221.html2.拦截器https://blog.csdn.net/qq_50652600/article/details/127250413......
  • Spring入门03Spring核心概念、DI、IOC入门案例
    Spring核心概念IoC/DIIoC容器Bean IOC入门案例IoC入门案例思路分析  IoC入门案例实现   DI入门案例 DI入门案例思路分析DI入门案例实现  ......
  • Spring MVC 的路径匹配策略
    spring.mvc.pathmatch.matching-strategy=ant_path_matcher是一个配置项,用于设置SpringMVC的路径匹配策略。在这个例子中,它设置为使用AntPathMatcher(Ant风格的路径匹配器)。AntPathMatcher是一种基于Ant构建工具的路径匹配算法,它可以支持更灵活的路径模式匹配。通过将......
  • Spring Boot学习随笔-SpringBoot的引言,回顾传统SSM开发
    学习视频:【编程不良人】2021年SpringBoot最新最全教程第一章、传统SSM开发回顾以及问题Spring+SpringMVC+MybatisSSM实现一个简单功能员工添加、查询…SSM项目简单实现项目需求分析—>概要设计—>(库表设计)—>详细设计—>(验证库表正确性)—>编码(环境搭建+......
  • SpringBoot高级开发(8)SpringBoot使用Lettuce设置多个RedisTemplate实例
    copy from:https://xiaomozhang.github.io/2021/02/07/spring-boot-lettuce-multi-instance/项目pom配置使用SpringBoot集成Redis,只需要将spring-boot-starter-data-redis和commons-pool2加到依赖即可 xml<dependency><groupId>org.springframework.boot</groupId>......
  • 【SpringBootWeb入门-6】请求响应-请求参数-数组集合参数&Json参数&路径参数
    这篇我们接着上一篇的请求参数来讲解另外几个常见参数的接收以及封装:数组集合参数、Json参数、路径参数。数组集合参数1、数组参数:请求参数名与形参数组名称相同且请求参数为多个,定义数组类型形参即可接收参数在Postman接口测试新建测试,获取请求数组参数type。然后新建参数处......
  • SpringBoot集成Jasypt实现数据加密
    1、环境说明JDK1.8+SpringBoot2.7 2、添加pom依赖<dependency><groupId>com.github.ulisesbocchio</groupId><artifactId>jasypt-spring-boot-starter</artifactId><version>3.0.3</version></dependency> 3......
  • Command line is too long. Shorten command line for xxx or also for Spring Boot d
    Commandlineistoolong.ShortencommandlineforxxxoralsoforSpringBootdefaultconfiguration主要是命令行太长了,导致项目启动不成功目录一、情景再现:二、分析原因:三、解决方法: 1、点击启动类编译文件:2、修改命令行的方式,shortencommandline 选项选择......
  • @SpringBootTest 和 @RunWith 注解不能识别 单元测试第一步引入maven依赖
    @SpringBootTest和@RunWith注解不能识别单元测试第一步引入maven依赖一、背景    最近在预研 Rocketmq,在写小例子的时候,需要编写测试代码,突然间发现我的 @SpringBootTest 和 @RunWith 这两个注解不能识别,于是展开了我的问题排查过程。问题截图如下:二、问题排......
  • @RunWith注解找不到,怎么办? spring-boot-starter-test 2.5.5 版本只需要在类上加上@Spr
    @RunWith注解找不到,怎么办?spring-boot-starter-test2.5.5版本只需要在类上加上@SpringBootTest即可,不需要再加@RunWith()注解了。1、新版spring-boot-starter-test不再集成junit,而是junit-jupiter在这里,先说明我使用的版本SpringBoot2.5.5spring-boot-starter-test2.5......