Spring高手之路6——Bean生命周期的扩展点:BeanPostProcessor

文章目录

  • 1. 探索Spring的后置处理器(BeanPostProcessor)
    • 1.1 BeanPostProcessor的设计理念
    • 1.2 BeanPostProcessor的文档说明
  • 2. BeanPostProcessor的使用
    • 2.1 BeanPostProcessor的基础使用示例
    • 2.2 利用BeanPostProcessor修改Bean的初始化结果的返回值
    • 2.3 通过BeanPostProcessor实现Bean属性的动态修改
  • 3. 深度剖析BeanPostProcessor的执行时机
    • 3.1 后置处理器在Bean生命周期中的作用及执行时机
    • 3.2 图解:Bean生命周期与后置处理器的交互时序

在前一篇讲解生命周期的时候就可以讲解后置处理器了,但是内容比较多,还是分开来讲解。

1. 探索Spring的后置处理器(BeanPostProcessor)

1.1 BeanPostProcessor的设计理念

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

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

1.2 BeanPostProcessor的文档说明

  我们来看看这个方法的文档注释,从图中可以看到,BeanPostProcessor 接口定义了两个方法,postProcessBeforeInitializationpostProcessAfterInitialization

在这里插入图片描述

  postProcessBeforeInitialization方法会在任何bean初始化回调(如InitializingBeanafterPropertiesSet方法或者自定义的init-method之前被调用。也就是说,这个方法会在bean的属性已经设置完毕,但还未进行初始化时被调用。

  postProcessAfterInitialization方法在任何bean初始化回调(比如InitializingBeanafterPropertiesSet或者自定义的初始化方法)之后被调用。这个时候,bean的属性值已经被填充完毕。返回的bean实例可能是原始bean的一个包装。

在这里插入图片描述


2. BeanPostProcessor的使用

2.1 BeanPostProcessor的基础使用示例

全部代码如下:

首先定义两个简单的BeanLionElephant

Lion.java

package com.example.demo.bean;public class Lion {private String name;public String getName() {return name;}public void setName(String name) {this.name = name;}
}

Elephant.java

package com.example.demo.bean;public class Elephant {private String name;public String getName() {return name;}public void setName(String name) {this.name = name;}
}

然后定义一个简单的BeanPostProcessor,它只是打印出被处理的Bean的名字:

package com.example.demo.processor;import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;public class MyBeanPostProcessor implements BeanPostProcessor {@Overridepublic Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {System.out.println("Before initialization: " + beanName);return bean;}@Overridepublic Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {System.out.println("After initialization: " + beanName);return bean;}
}

接着我们定义一个配置类,其中包含对LionElephant类和MyBeanPostProcessor 类的Bean定义:

package com.example.demo.configuration;import com.example.demo.bean.Elephant;
import com.example.demo.bean.Lion;
import com.example.demo.processor.MyBeanPostProcessor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class AnimalConfig {@Beanpublic Lion lion() {return new Lion();}@Beanpublic Elephant elephant() {return new Elephant();}@Beanpublic MyBeanPostProcessor myBeanPostProcessor() {return new MyBeanPostProcessor();}
}

最后,我们在主程序中创建ApplicationContext对象:

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;public class Main {public static void main(String[] args) {ApplicationContext context = new AnnotationConfigApplicationContext(AnimalConfig.class);((AnnotationConfigApplicationContext)context).close();}
}

运行结果:

在这里插入图片描述

  以上代码在执行时,将先创建LionElephant对象,然后在初始化过程中和初始化后调用postProcessBeforeInitializationpostProcessAfterInitialization方法,打印出被处理的Bean的名字。

细心的小伙伴可能观察到这里有红色日志
信息: Bean 'animalConfig' of type [com.example.demo.configuration.AnimalConfig$$EnhancerBySpringCGLIB$$ee4adc7e] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)

  Spring中,BeanPostProcessor是被特殊处理的,它们会在其他普通Bean之前被实例化和初始化,这样设计的原因是BeanPostProcessor的存在可以影响其他Bean的创建和初始化过程。 Spring应用上下文中可以存在多个BeanPostProcessorSpring本身就提供了很多内置的BeanPostProcessor

  但是,如果在初始化BeanPostProcessor的过程中需要依赖其他的Bean,那么这些被依赖的Bean会先于后置处理器进行初始化。然而,由于这些被依赖的Bean是在该BeanPostProcessor初始化完成之前就已经进行了初始化,它们就会错过这个BeanPostProcessor的处理。在这个例子中,MyBeanPostProcessor就是这样的一个BeanPostProcessor,而"animalConfig"是它所依赖的Bean。所以这个日志信息就是说,'animalConfig'这个Bean在初始化的时候,没有被所有的BeanPostProcessor处理,这里它无法得到MyBeanPostProcessor的处理。

  我们只需要把实例化过程直接交给Spring容器来管理,而不是在配置类中手动进行实例化,就可以消除这个提示信息,也就是在MyBeanPostProcessor上加@Component即可。

  在第3节的例子中就使用了@Component处理这个MyBeanPostProcessor,这个提示就消失了。

2.2 利用BeanPostProcessor修改Bean的初始化结果的返回值

还是上面的例子,我们只修改一下MyBeanPostProcessor 类的方法后再次运行

package com.example.demo.processor;import com.example.demo.bean.Elephant;
import com.example.demo.bean.Lion;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;public class MyBeanPostProcessor implements BeanPostProcessor {@Overridepublic Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {System.out.println("Before initialization: " + bean);if (bean instanceof Lion) {return new Elephant();}return bean;}@Overridepublic Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {System.out.println("After initialization: " + bean);return bean;}
}

运行结果:

在这里插入图片描述

  BeanPostProcessor的两个方法都可以返回任意的Object,这意味着我们可以在这两个方法中更改返回的bean。例如,如果我们让postProcessBeforeInitialization方法在接收到Lion实例时返回一个新的Elephant实例,那么我们将会看到Lion实例变成了Elephant实例。

  那既然BeanPostProcessor的两个方法都可以返回任意的Object,那我搞点破坏返回null会怎么样,会不会因为初始化beannull而导致异常呢?

  答案是不会的,我们来看一下:

package com.example.demo.processor;import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;public class MyBeanPostProcessor implements BeanPostProcessor {@Overridepublic Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {System.out.println("Before initialization: " + bean);return null;}@Overridepublic Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {System.out.println("After initialization: " + bean);return bean;}
}

我们运行看结果

在这里插入图片描述

结果发现还是正常初始化的bean类型,不会有任何改变,我们继续调试看看是为什么

在这里插入图片描述

我们通过堆栈帧看到调用postProcessBeforeInitialization方法的上一个方法是applyBeanPostProcessorsBeforeInitialization,双击点开看一看这个方法

在这里插入图片描述

  从我这个调试图中可以看到,如果postProcessBeforeInitialization返回nullSpring仍然用原始的bean进行后续的处理,同样的逻辑在postProcessAfterInitialization也是一样。这就是为什么我们在BeanPostProcessor类的方法中返回null,原始bean实例还是存在的原因。

2.3 通过BeanPostProcessor实现Bean属性的动态修改

来看看是怎么拦截 bean 的初始化的

全部代码如下:

首先,我们定义一个Lion类:

public class Lion {private String name;public Lion() {this.name = "Default Lion";}public Lion(String name) {this.name = name;}public String getName() {return name;}public void setName(String name) {this.name = name;}@Overridepublic String toString() {return "Lion{" + "name='" + name + '\'' + '}';}
}

接下来,我们定义一个BeanPostProcessor,我们称之为MyBeanPostProcessor

package com.example.demo.processor;import com.example.demo.bean.Lion;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;public class MyBeanPostProcessor implements BeanPostProcessor {@Overridepublic Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {System.out.println("Bean的初始化之前:" + bean);return bean;}@Overridepublic Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {System.out.println("Bean的初始化之后:" + bean);if (bean instanceof Lion) {((Lion) bean).setName("Simba");}return bean;}
}

然后我们定义一个配置类,其中包含对Lion类的Bean定义和对MyBeanPostProcessor 类的Bean定义:

package com.example.demo.configuration;import com.example.demo.bean.Lion;
import com.example.demo.processor.MyBeanPostProcessor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class AnimalConfig {@Beanpublic Lion lion() {return new Lion();}@Beanpublic MyBeanPostProcessor myBeanPostProcessor() {return new MyBeanPostProcessor();}
}

最后,我们在主程序中创建ApplicationContext对象,并获取Lion对象:

package com.example.demo;import com.example.demo.bean.Lion;
import com.example.demo.configuration.AnimalConfig;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;public class DemoApplication {public static void main(String[] args) {ApplicationContext context = new AnnotationConfigApplicationContext(AnimalConfig.class);Lion lion = context.getBean("lion", Lion.class);System.out.println(lion);((AnnotationConfigApplicationContext)context).close();}
}

运行结果:

在这里插入图片描述

  上面代码在执行时,先创建一个Lion对象,然后在初始化过程中和初始化后调用postProcessBeforeInitializationpostProcessAfterInitialization方法,修改Lion的名字为"Simba",最后在主程序中输出Lion对象,显示其名字为"Simba"


3. 深度剖析BeanPostProcessor的执行时机

3.1 后置处理器在Bean生命周期中的作用及执行时机

  在这个例子中,我们将创建一个名为LionElephantBean,它会展示属性赋值和生命周期的各个步骤的执行顺序。同时,我们还将创建一个BeanPostProcessor来打印消息并显示它的执行时机。

全部代码如下:

首先,我们定义我们的Lion

package com.example.demo.bean;import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;public class Lion implements InitializingBean, DisposableBean {private String name;private Elephant elephant;public Lion() {System.out.println("1. Bean Constructor Method Invoked!");}public String getName() {return name;}public void setName(String name) {this.name = name;System.out.println("2. Bean Setter Method Invoked! name: " + name);}/*** setter注入* @param elephant*/@Resourcepublic void setElephant(Elephant elephant) {this.elephant = elephant;System.out.println("2. Bean Setter Method Invoked! elephant: " + elephant);}@PostConstructpublic void postConstruct() {System.out.println("4. @PostConstruct Method Invoked!");}@Overridepublic void afterPropertiesSet() throws Exception {System.out.println("5. afterPropertiesSet Method Invoked!");}public void customInitMethod() {System.out.println("6. customInitMethod Method Invoked!");}@PreDestroypublic void preDestroy() {System.out.println("8. @PreDestroy Method Invoked!");}@Overridepublic void destroy() throws Exception {System.out.println("9. destroy Method Invoked!");}public void customDestroyMethod() {System.out.println("10. customDestroyMethod Method Invoked!");}
}

创建Lion所依赖的Elephant

package com.example.demo.bean;import org.springframework.stereotype.Component;@Component
public class Elephant {private String name;public String getName() {return name;}public void setName(String name) {this.name = name;}
}

然后,我们定义一个简单的BeanPostProcessor

package com.example.demo.processor;import com.example.demo.bean.Lion;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;@Component
public class MyBeanPostProcessor implements BeanPostProcessor {@Overridepublic Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {if(bean instanceof Lion) {System.out.println("3. postProcessBeforeInitialization Method Invoked!");}return bean;}@Overridepublic Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {if(bean instanceof Lion) {System.out.println("7. postProcessAfterInitialization Method Invoked!");}return bean;}
}

创建一个配置类AnimalConfig

package com.example.demo.configuration;import com.example.demo.bean.Lion;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class AnimalConfig {@Bean(initMethod = "customInitMethod", destroyMethod = "customDestroyMethod")public Lion lion() {Lion lion = new Lion();lion.setName("my lion");return lion;}
}

主程序:

package com.example.demo;import com.example.demo.bean.Lion;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;public class DemoApplication {public static void main(String[] args) {System.out.println("容器初始化之前...");AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext("com.example");System.out.println("容器初始化完成");Lion bean = context.getBean(Lion.class);bean.setName("oh!!! My Bean set new name");System.out.println("容器准备关闭...");context.close();System.out.println("容器已经关闭");}
}

控制台上看到所有的方法调用都按照预期的顺序进行,这可以更好地理解Bean属性赋值和生命周期以及BeanPostProcessor的作用。

在这里插入图片描述

根据打印日志我们可以分析出

  1. 首先,Bean Constructor Method Invoked! 表明 Lion 的构造器被调用,创建了一个新的 Lion 实例。

  2. 接着,Bean Setter Method Invoked! name: my lionBean Setter Method Invoked! elephant: com.example.demo.bean.Elephant@7364985f 说明 SpringLion 实例的依赖注入。在这一步,Spring 调用了 Lionsetter 方法,为 name 属性设置了值 “my lion”,同时为 elephant 属性注入了一个 Elephant 实例。

  3. 然后,postProcessBeforeInitialization Method Invoked! 说明 MyBeanPostProcessorpostProcessBeforeInitialization 方法被调用,这是在初始化 Lion 实例之前。

  4. @PostConstruct Method Invoked! 说明 @PostConstruct 注解的方法被调用,这是在 Bean 初始化之后,但是在 Spring 执行任何进一步初始化之前。

  5. afterPropertiesSet Method Invoked! 说明 Spring 调用了 InitializingBeanafterPropertiesSet 方法

  6. customInitMethod Method Invoked! 表示调用了 Lion 实例的 init-method 方法。

  7. postProcessAfterInitialization Method Invoked! 说明 MyBeanPostProcessorpostProcessAfterInitialization 方法被调用,这是在初始化 Lion 实例之后。

然后 Spring 完成了整个初始化过程。

  1. 主程序中手动调用了 Lion 实例的 setter 方法,因此在 Bean Setter Method Invoked! name: oh!!! My Bean set new name 可见,name 属性被设置了新的值 "oh!!! My Bean set new name"

当容器准备关闭时:

  1. @PreDestroy Method Invoked! 说明 @PreDestroy 注解的方法被调用,这是在 Bean 销毁之前。

  2. destroy Method Invoked! 表示 Lion 实例开始销毁。在这一步,Spring 调用了 DisposableBeandestroy 方法。

  3. customDestroyMethod Method Invoked! 表示 Lion 实例开始销毁,调用了Lion 实例的 destroy-method 方法。

最后,Spring 完成了整个销毁过程,容器关闭。

  这个日志提供了 Spring Bean 生命周期的完整视图,显示了从创建到销毁过程中的所有步骤。

  注意:DisposableBeandestroy 方法和 destroy-method 方法调用,这个销毁过程不意味着bean实例就被立即从内存中删除了,Java的垃圾收集机制决定了对象什么时候被从内存中删除。Spring容器无法强制进行这个操作,比如解除bean之间的关联和清理缓存,这并不是Spring在销毁bean时会做的,而是由Java的垃圾回收器在一个对象不再被引用时做的事情。

  BeanPostProcessor 的执行顺序是在 Spring Bean 的生命周期中非常重要的一部分。例如,如果一个 Bean 实现了 InitializingBean 接口,那么 afterPropertiesSet 方法会在所有的 BeanPostProcessorpostProcessBeforeInitialization 方法之后调用,以确保所有的前置处理都完成了。同样,BeanPostProcessorpostProcessAfterInitialization 方法会在所有的初始化回调方法之后调用,以确保 Bean 已经完全初始化了。

  我们可以注册多个 BeanPostProcessor。在这种情况下,Spring 会按照它们的 Ordered 接口或者 @Order 注解指定的顺序来调用这些后置处理器。如果没有指定顺序,那么它们的执行顺序是不确定的。

3.2 图解:Bean生命周期与后置处理器的交互时序

  综合上面的执行结果,我们来总结一下,下面是Spring Bean生命周期的时序图,它详细地描绘了Spring Bean从实例化到准备使用的整个过程,包括Bean的实例化、属性赋值、生命周期方法的执行和后置处理器的调用。

Spring Bean生命周期

欢迎一键三连~

有问题请留言,大家一起探讨学习

----------------------Talk is cheap, show me the code-----------------------

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/14390.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

途乐证券|有色金属板块崛起涨超2%,云南锗业两连板

周三(7月5日),A股三大股指震荡整理。截至上午收盘,上证指数跌幅达0.51%,报3228.68点;深证成指和创业板指跌幅分别为0.53%和0.59%;沪深两市合计成交额5310.1.6亿元,总体来看,两市个股跌多涨少。 …

使用Stable Diffusion生成艺术二维码

在数字艺术的世界中,二维码已经从单纯的信息承载工具转变为可以展示艺术表达的媒介。这是通过使用Stable Diffusion的技术实现的,它可以将任何二维码转化为独特的艺术作品。接下来,我们将一步步教你如何使用Stable Diffusion生成艺术二维码。…

【备战秋招】每日一题:2022.11.3-华为机试-去除多余空格

为了更好的阅读体检,可以查看我的算法学习网 在线评测链接:P1058 题目描述 塔子哥最近接到导师的一个任务,需要他帮忙去除文本多余空格,但不去除配对单引号之间的多余空格。给出关键词的起始和结束下标,去除多余空格后刷新关键词…

【JavaScript】ES6新特性(5)

16. Promise Promise 是异步编程的一种解决方案,比传统的解决方案回调函数, 更合理和更强大 ES6 将其写进了语言标准,统一了用法,原生提供了Promise对象 指定回调函数方式更灵活易懂解决异步 回调地狱 的问题 16.1 回调地狱 当一个回调函数…

并发编程 - Event Bus 设计模式

文章目录 Pre设计CodeBus接口自定义注解 Subscribe同步EventBus异步EventBusSubscriber注册表RegistryEvent广播Dispatcher 测试简单的Subscriber同步Event Bus异步Event Bus Pre 我们在日常的工作中,都会使用到MQ这种组件, 某subscriber在消息中间件上…

AIGC - Stable Diffusion 的 AWPortrait 1.1 模型与 Prompts 设置

欢迎关注我的CSDN:https://spike.blog.csdn.net/ 本文地址:https://spike.blog.csdn.net/article/details/131565908 AWPortrait 1.1 网址:https://www.liblibai.com/modelinfo/721fa2d298b262d7c08f0337ebfe58f8 介绍:AWPortrai…

使用LiteSpeed缓存插件将WordPress优化到100%的得分

页面速度优化应该是每个网站所有者的首要任务,因为它直接影响WordPress SEO。此外,网站加载的时间越长,其跳出率就越高。这可能会阻止您产生转化并为您的网站带来流量。 使用正确的工具和配置,缓存您的网站可以显着提高其性能。因…

Spring系列3 -- 更简单的读取和存储对象

前言 上一篇章总结了,Spring的创建与使用,通过创建Maven项目配置Spring的环境依赖,创建Spring框架的项目,然后通过在Resource目录下创建Spring-config.xml配置文件,添加<bean></bean>标签将我们需要的bean对象注入到容器中,然后通过ApplicationContext获取Spring上…

web服务端接收多用户并发上传同一文件,保证文件副本只存在一份(附go语言实现)

背景 对于一个文件服务器来说&#xff0c;对于同一文件&#xff0c;应该只保存一份在服务器上。基于这个原则&#xff0c;引发出本篇内容。 本篇仅阐述文件服务器在同一时间接收同一文件的并发问题&#xff0c;这种对于小体量的服务来说并不常见&#xff0c;但是最好还是要留…

文本分析-使用Python做词频统计分析

&#x1f935;‍♂️ 个人主页&#xff1a;艾派森的个人主页 ✍&#x1f3fb;作者简介&#xff1a;Python学习者 &#x1f40b; 希望大家多多支持&#xff0c;我们一起进步&#xff01;&#x1f604; 如果文章对你有帮助的话&#xff0c; 欢迎评论 &#x1f4ac;点赞&#x1f4…

【滑动窗口】209. 长度最小的子数组

209. 长度最小的子数组 解题思路 滑动窗口设置前后指针滑动窗口内的元素之和总是大于或者等于s滑动窗口的起始位置: 如果窗口的值大于等于s 窗口向前移动窗口结束位置:for循环的j class Solution {public int minSubArrayLen(int target, int[] nums) {int left 0;// 滑动窗口…

学习系统编程No.28【多线程概念实战】

引言&#xff1a; 北京时间&#xff1a;2023/6/29/15:33&#xff0c;刚刚更新完博客&#xff0c;目前没什么状态&#xff0c;不好趁热打铁&#xff0c;需要去睡一会会&#xff0c;昨天睡的有点迟&#xff0c;然后忘记把7点到8点30之间的4个闹钟关掉了&#xff0c;恶心了我自己…