《系列二》-- 7、后置处理器-PostProcessor
阅读之前要注意的东西:本文就是主打流水账式的源码阅读,主导的是一个参考,主要内容需要看官自己去源码中验证。全系列文章基于 spring 源码 5.x 版本。
写在开始前的话:
阅读spring 源码实在是一件庞大的工作,不说全部内容,单就最基本核心部分包含的东西就需要很长时间去消化了:
- beans
- core
- context
实际上我在博客里贴出来的还只是一部分内容,更多的内容,我放在了个人,fork自 spring 官方源码仓了; 而且对源码的学习,必须是要跟着实际代码层层递进的,不然只是干巴巴的文字味同嚼蜡。
这个仓设置的公共仓,可以直接拉取。
什么是后置处理器
这里引入spring 的一个 重要部件 PostProcessor, 我习惯把它叫做:后置处理器;
通俗来说,就是:定义一个 XxxPostProcessor 接口,定义一组行为;而后在具体的bean加载过程中,我们可以在 BeanFactory 初始化时,根据自己的实际需要,向BeanFactory 中注入相关的 PostProcessor;
而后在某些特殊节点(时机),获取某一类的 "后置处理器" 并全部执行之即可。
这里需要注意的是:后置处理器,一般需要被理解成,某某行为-后置处理器;它可能是对某某行为返回结果的校验,也可能是对其的包装,(希望你了解设计模式之:包装器模式)。
spring 源码中已知的,顶级PostProcessor

全局一共6个,那么我们通过,其中跟我们当前分析的代码关联度最高的, BeanPostProssor 来做个介绍:
package org.springframework.beans.factory.config;
import org.springframework.beans.BeansException;
import org.springframework.lang.Nullable;
/**
* Factory hook that allows for custom modification of new bean instances,
* e.g. checking for marker interfaces or wrapping them with proxies.
*
* <p>ApplicationContexts can autodetect BeanPostProcessor beans in their
* bean definitions and apply them to any beans subsequently created.
* Plain bean factories allow for programmatic registration of post-processors,
* applying to all beans created through this factory.
*
* <p>Typically, post-processors that populate beans via marker interfaces
* or the like will implement {@link #postProcessBeforeInitialization},
* while post-processors that wrap beans with proxies will normally
* implement {@link #postProcessAfterInitialization}.
*/
public interface BeanPostProcessor {
/**
* Apply this BeanPostProcessor to the given new bean instance <i>before</i> any bean
* initialization callbacks (like InitializingBean's {@code afterPropertiesSet}
* or a custom init-method). The bean will already be populated with property values.
* The returned bean instance may be a wrapper around the original.
* <p>The default implementation returns the given {@code bean} as-is.
* @param bean the new bean instance
* @param beanName the name of the bean
* @return the bean instance to use, either the original or a wrapped one;
*/
@Nullable
default Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
/**
* Apply this BeanPostProcessor to the given new bean instance <i>after</i> any bean
* initialization callbacks (like InitializingBean's {@code afterPropertiesSet}
* or a custom init-method). The bean will already be populated with property values.
* The returned bean instance may be a wrapper around the original.
* <p>In case of a FactoryBean, this callback will be invoked for both the FactoryBean
* instance and the objects created by the FactoryBean (as of Spring 2.0). The
* post-processor can decide whether to apply to either the FactoryBean or created
* objects or both through corresponding {@code bean instanceof FactoryBean} checks.
* <p>This callback will also be invoked after a short-circuiting triggered by a
* in contrast to all other BeanPostProcessor callbacks.
* <p>The default implementation returns the given {@code bean} as-is.
* @param bean the new bean instance
* @param beanName the name of the bean
* @return the bean instance to use, either the original or a wrapped one;
*/
@Nullable
default Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
}
可以看接口中的这俩方法名:
postProcessBeforeInitialization: 后置处理器 of 初始化前
所有bean在"初始化前"都会执行,实现了接口BeanPostProcessor 的后置处理器,所实现的 postProcessBeforeInitialization 方法。
该方法返回的可能是该bean的实例,也可能是被代理包装后的该beanpostProcessAfterInitialization: 后置处理器 of 初始化后
所有bean在 "初始化后" 都会执行,实现了接口BeanPostProcessor 的后置处理器,所实现的 postProcessBeforeInitialization 方法。
该方法返回的可能是该bean的实例,也可能是被代理包装后的该bean
我这里的翻译较为缩略,英文尚可的伙计可以自取源码中的注释。
其它 "后置处理器"
其实大同小异,其它的就不再展开介绍。
《系列二》-- 7、后置处理器-PostProcessor的更多相关文章
- <四>JMeter数据库连接/后置处理器/断言简介
一.数据库连接 1.右键线程组添加--配置元件--JDB Cconnection Configuration 2.配置如下: URL为数据路连接地址,用户名密码为数据库用户名和密码 3.添加一个JDB ...
- Spring点滴五:Spring中的后置处理器BeanPostProcessor讲解
BeanPostProcessor接口作用: 如果我们想在Spring容器中完成bean实例化.配置以及其他初始化方法前后要添加一些自己逻辑处理.我们需要定义一个或多个BeanPostProcesso ...
- spring学习四:Spring中的后置处理器BeanPostProcessor
BeanPostProcessor接口作用: 如果我们想在Spring容器中完成bean实例化.配置以及其他初始化方法前后要添加一些自己逻辑处理.我们需要定义一个或多个BeanPostProcesso ...
- 二、Jmeter 后置处理器(BeanShell PostProcessor)
1.新建JDBC Request,如下图所示: 重要的参数说明: Variable Name:数据库连接池的名字,需要与JDBC Connection Configuration的Variable N ...
- 2.3 spring5源码系列---内置的后置处理器PostProcess加载源码
本文涉及主题 1. BeanFactoryPostProcessor调用过程源码剖析 2. 配置类的解析过程源码 3. 配置类@Configuration加与不加的区别 4. 重复beanName的覆 ...
- Spring Ioc源码分析系列--Ioc容器BeanFactoryPostProcessor后置处理器分析
Spring Ioc源码分析系列--Ioc容器BeanFactoryPostProcessor后置处理器分析 前言 上一篇文章Spring Ioc源码分析系列--Ioc源码入口分析已经介绍到Ioc容器 ...
- Spring Ioc源码分析系列--Ioc容器注册BeanPostProcessor后置处理器以及事件消息处理
Spring Ioc源码分析系列--Ioc容器注册BeanPostProcessor后置处理器以及事件消息处理 前言 上一篇分析了BeanFactoryPostProcessor的作用,那么这一篇继续 ...
- JMeter学习-009-JMeter 后置处理器实例之 - 正则表达式提取器(二)多参数获取
前文简述了通过后置处理器 - 正则表达式提取器 获取 HTTP请求 响应结果中的特定数据,未看过的亲,敬请参阅 JMeter学习-008-JMeter 后置处理器实例之 - 正则表达式提取器(一). ...
- Jmeter学习笔记(二十)——后置处理器XPath Extractor使用
一.背景 在使用过程某些操作步骤与其相邻步骤存在一定的依赖关系,需要需要将上一个请求的响应结果作为下一个请求的参数. Jmeter中后置处理器正则表达式提取器和XPath Extractor都可以将页 ...
- Spring(二)--FactoryBean、bean的后置处理器、数据库连接池、引用外部文件、使用注解配置bean等
实验1:配置通过静态工厂方法创建的bean [通过静态方法提供实例对象,工厂类本身不需要实例化!] 1.创建静态工厂类 public class StaticFactory { private st ...
随机推荐
- [转帖]ssh_exporter
https://github.com/treydock/ssh_exporter SSH exporter The SSH exporter attempts to make an SSH conne ...
- [转帖]记一次vcsa6修复过程
一. 某天发现一台vmware vCenter Server Appliance services 6偶尔能登陆了,但极不稳定,连shell都偶尔能进...... 然后利用各种手段想方设法进到she ...
- [转帖]ebpf 月报 - 2023 年 1 月
https://segmentfault.com/a/1190000043355631 本刊物旨在为中文用户提供及时.深入.有态度的 ebpf 资讯. 如果你吃了鸡蛋觉得好吃,还想认识下蛋的母鸡,欢迎 ...
- [转帖]关于iostat的问题,svctm数据不可信
使用FIO对磁盘进行压力测试,使用1个线程对磁盘进行随机读,设置单次read的数据块分别为128KB和1M,数据如下: (1)单次IO数据块为128KB (2)单次IO数据块为1M 从上面的数据可以看 ...
- [转帖]Zen4架构+5nm制程+96核心 第四代AMD EPYC处理器强势来袭
https://new.qq.com/rain/a/20221111A098QE00 不得不承认,技术的持续突破和迭代,使得AMD处理器在近年来得到了"喷气机式"的增长,无论是 ...
- 【K哥爬虫普法】网盘用的好,“艳照门”跑不了
我国目前并未出台专门针对网络爬虫技术的法律规范,但在司法实践中,相关判决已屡见不鲜,K哥特设了"K哥爬虫普法"专栏,本栏目通过对真实案例的分析,旨在提高广大爬虫工程师的法律意识,知 ...
- 【JS 逆向百例】浏览器插件 Hook 实战,亚航加密参数分析
关注微信公众号:K哥爬虫,QQ交流群:808574309,持续分享爬虫进阶.JS/安卓逆向等技术干货! 声明 本文章中所有内容仅供学习交流,抓包内容.敏感网址.数据接口均已做脱敏处理,严禁用于商业用途 ...
- linux时间和当前时间相关8小时问题
依次执行如下的代码: 1.更改时区 cp /usr/share/zoneinfo/GMT /etc/localtime ln -sf /usr/share/zoneinfo/Asia/Shanghai ...
- druid和druid-spring-boot-starter区别,以及springboot项目中提示报错Cannot resolve configuration property 'spring.datasource.xxxx' 和hikari配置属性
一.druid和druid-spring-boot-starter区别分析 作用是一样的,都是连接池提供连接,里边的配置参数都是一样的: druid-spring-boot-starter只是在dru ...
- Python 探索性数据分析工具(PandasGUI,Pandas Profiling,Sweetviz,dtale)以及学术论文快速作图science.mplstyle
如果探索的数据集侧重数据展示,可以选PandasGUI:如果只是简单了解基本统计指标,可以选择Pandas Profiling和Sweetviz:如果需要做深度的数据探索,那就选择dtale. 1. ...