Sping源码(九)—— Bean的初始化(非懒加载)— Bean的创建方式(自定义BeanPostProcessor)

序言

之前文章有介绍采用FactoryBean的方式创建对象,以及使用反射创建对象。
这篇文章继续介绍Spring中创建Bean的形式之一——自定义BeanPostProcessor。
之前在介绍BeanPostProcessor的文章中有提到,BeanPostProcessor接口的实现中有一个InstantiationAwareBeanPostProcessor接口
而在Spirng源码中的doGetBean方法中就有针对该接口的判断逻辑,如果有类实现了InstantiationAwareBeanPostProcessor则可以在对应的方法中进行Bean对象的创建。
在这里插入图片描述
源码
去除无用代码,我们这里主要看resolveBeforeInstantiation方法。如果通过resolveBeforeInstantiation创建了Bean示例,则return该Bean 不在继续往下走其他流程。

protected Object createBean(String beanName, RootBeanDefinition mbd, @Nullable Object[] args)throws BeanCreationException {RootBeanDefinition mbdToUse = mbd;//锁定class,根据设置的class属性或者根据className来解析classClass<?> resolvedClass = resolveBeanClass(mbd, beanName);if (resolvedClass != null && !mbd.hasBeanClass() && mbd.getBeanClassName() != null) {mbdToUse = new RootBeanDefinition(mbd);mbdToUse.setBeanClass(resolvedClass);}try {// Give BeanPostProcessors a chance to return a proxy instead of the target bean instance.// 给BeanPostProcessors一个机会来返回代理来替代真正的实例,Object bean = resolveBeforeInstantiation(beanName, mbdToUse);if (bean != null) {return bean;}}}

resolveBeforeInstantiation
如果满足if条件,则会走applyBeanPostProcessorsBeforeInstantiation方法进行对象的创建。

protected Object resolveBeforeInstantiation(String beanName, RootBeanDefinition mbd) {Object bean = null;//如果beforeInstantiationResolved值为null或者true,那么表示尚未被处理,进行后续的处理if (!Boolean.FALSE.equals(mbd.beforeInstantiationResolved)) {// Make sure bean class is actually resolved at this point.// 确保Bean确实在此处进行处理// 判断当前mbd是否是合成的,只有在实现aop的时候synthetic的值才为true,并且是否实现了InstantiationAwareBeanPostProcessor接口if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) {//确定目标类型Class<?> targetType = determineTargetType(beanName, mbd);if (targetType != null) {bean = applyBeanPostProcessorsBeforeInstantiation(targetType, beanName);if (bean != null) {bean = applyBeanPostProcessorsAfterInitialization(bean, beanName);}}}mbd.beforeInstantiationResolved = (bean != null);}return bean;}

applyBeanPostProcessorsBeforeInstantiation
获取所有实现了BeanPostProcessors的类,并强转成InstantiationAwareBeanPostProcessor 类型,调用postProcessBeforeInstantiation方法进行Bean的创建。

protected Object applyBeanPostProcessorsBeforeInstantiation(Class<?> beanClass, String beanName) {for (BeanPostProcessor bp : getBeanPostProcessors()) {if (bp instanceof InstantiationAwareBeanPostProcessor) {InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp;Object result = ibp.postProcessBeforeInstantiation(beanClass, beanName);if (result != null) {return result;}}}return null;}

测试类

我们针对上面的源码流程,扩展自定义的BeanPostProcessor进行类的创建。我们这里依然采用上篇文章提到的Cglib动态代理的方式创建我们的对象。

BeforeInstantiation
BeforeInstantiation 是我们想要最终创建的实例对象。

public class BeforeInstantiation {public void doSomeThing(){System.out.println("执行do some thing....");}
}

MyMethodInterceptor
拦截器类,对目标方法进行中间拦截,上篇文章在讲lookup-method标签时,里面采用Cglib动态代理生成对象的固定写法,创建Enhancer对象并设置CallBack。
因为我们这里也采用Cglib的方式进行对象的创建,所以也需要设置CallBack。

public class MyMethodInterceptor implements MethodInterceptor {@Overridepublic Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {System.out.println("目标方法执行之前:" + method);Object o1 = methodProxy.invokeSuper(o, objects);System.out.println("目标方法执行之后:" + method);return o1;}
}

MyInstantiationAwareBeanPostProcessor
自定义InstantiationAwareBeanPostProcessor接口的扩展类,调用postProcessBeforeInstantiation方法,采用Cglib的固定写法实现对BeforeInstantiation 对象的创建。
其余方法因为用不到,所以直接return对应类型,没有具体业务逻辑。

public class MyInstantiationAwareBeanPostProcessor implements InstantiationAwareBeanPostProcessor {@Overridepublic Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {if (beanClass == BeforeInstantiation.class) {Enhancer enhancer = new Enhancer();enhancer.setSuperclass(beanClass);enhancer.setCallback(new MyMethodInterceptor());BeforeInstantiation beforeInstantiation = (BeforeInstantiation) enhancer.create();return beforeInstantiation;}return null;}@Overridepublic Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {return bean;}@Overridepublic Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {return bean;}@Overridepublic boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {return true;}@Overridepublic PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName) throws BeansException {return pvs;}@Overridepublic PropertyValues postProcessPropertyValues(PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeansException {return pvs;}

resolveBeforeInstantiation.xml
xml中声明MyInstantiationAwareBeanPostProcessor对象和BeforeInstantiation对象。

<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><bean id="myInstantiationAwareBeanPostProcessor" class="org.springframework.resolveBeforeInstantiation.MyInstantiationAwareBeanPostProcessor" /><bean id="beforeInstantiation" class="org.springframework.resolveBeforeInstantiation.BeforeInstantiation"/>
</beans>

main
执行refresh()主流程调用registerBeanPostProcessors方法创建MyInstantiationAwareBeanPostProcessor后,会调用addBeanPostProcessor方法将hasInstantiationAwareBeanPostProcessors()变量设置为true,当beforeInstantiation创建时,if 判断 整体为 true,调用我们自定义的扩展类创建beforeInstantiation对象。

	public static void main(String[] args) {ApplicationContext ac = new ClassPathXmlApplicationContext("resolveBeforeInstantiation.xml");BeforeInstantiation beanInstantiation = (BeforeInstantiation)ac.getBean("beforeInstantiation");beanInstantiation.doSomething();}

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

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

相关文章

[文献解读]:斯坦福最新研究-HumanPlus:人形机器人跟踪和模仿人类

摘要 制造具有与人类相似外形的机器人的关键论点之一是&#xff0c;我们可以利用大量人类数据进行训练。然而&#xff0c;由于人形机器人感知和控制的复杂性、人形机器人与人类在形态和驱动方面仍然存在的物理差距&#xff0c;以及人形机器人缺乏从自我中心视觉学习自主技能的…

马克·雷伯特访谈:机器人的未来及波士顿动力的创新之路

引言 机器人技术作为现代科技的前沿领域&#xff0c;始终吸引着大量的关注与研究。波士顿动力公司作为这一领域的领军者&#xff0c;其创始人兼前CEO马克雷伯特&#xff08;Marc Raibert&#xff09;近日在主持人莱克斯弗里德曼&#xff08;Lex Fridman&#xff09;的播客节目…

如何用 ChatGPT DALL-E3绘画(10个案例)

如何用ChatGPT绘画——10个案例&#xff08;附提示词&#xff09; DALL•E 3可以在ChatGPT plus里直接使用了。 如果想免费使用&#xff0c;可以用新必应免费使用。 上次有个朋友问&#xff1a;DALL•E 3 有什么用。 这里用十个案例&#xff0c;来解释一下这个问题。 1.创…

爱心代码来喽

今天给大家分享一个爱心代码&#xff0c;送给我的粉丝们。愿你们天天开心&#xff0c;事事顺利&#xff0c;学业和事业有成。 下面是运行代码&#xff1a; #include<stdio.h> #include<Windows.h> int main() { system(" color 0c"); printf(&q…

简单说一下STL中的map容器的特点、底层实现和应用场景【面试】

特点&#xff1a; 基于红黑树&#xff1a;std::map利用红黑树的自平衡特性&#xff0c;确保操作的平衡性。有序容器&#xff1a;元素根据键的顺序自动排序&#xff0c;排序依据是预定义的键比较函数。唯一键值&#xff1a;容器保证每个键的唯一性&#xff0c;不允许重复键存在…

mysql中对时间的操作

SQL 语句中对时间的操作主要包括以下几种: 提取时间元素 YEAR(date_column): 提取年份MONTH(date_column): 提取月份DAY(date_column): 提取日HOUR(time_column): 提取小时MINUTE(time_column): 提取分钟SECOND(time_column): 提取秒 时间计算 DATE_ADD(date, INTERVAL expr un…

Web前端:深入剖析前端专业的核心领域

Web前端&#xff1a;深入剖析前端专业的核心领域 在数字化时代&#xff0c;Web前端作为前端专业的重要组成部分&#xff0c;扮演着至关重要的角色。它不仅涉及到网页的展示和交互&#xff0c;还关乎用户体验和产品的整体质量。那么&#xff0c;Web前端是否等同于前端专业&…

对接钉钉Stream模式考勤打卡相关事件的指南

钉钉之前的accessToken是公司级别的&#xff0c;现在的accessToken是基于应用的&#xff0c;接口的权限也是基于应用的。所以第一步是在钉钉开放平台&#xff08;https://open-dev.dingtalk.com/&#xff09;创建一个应用。 创建好应用之后&#xff0c;因为我们后续还需要调用钉…

ssm题库分享

ssm题库分享 1、&#xff08;2分&#xff09;以下哪个选项描述了在 SSM 框架集成中&#xff0c;MyBatis 的作用&#xff1f; A、实现容器管理和事务控制等功能 B、实现数据访问和对象映射等功能 C、实现 Inversion of Control 和依赖注入等功能 D、实现面向切面编程和声明…

docker拉取镜像太慢解决方案

前言 这是我在这个网站整理的笔记,有错误的地方请指出&#xff0c;关注我&#xff0c;接下来还会持续更新。 作者&#xff1a;神的孩子都在歌唱 创建daemon.json文件,输入以下信息 vim /etc/docker/daemon.json{"registry-mirrors": ["https://9cpn8tt6.mirror…

“Dream Machine“震撼登场!免费推出的AI电影级巨制在网络上引爆热潮

"巅峰初现&#xff01;视频AI新星‘梦幻制造者’华美登场&#xff01; 在视频生成技术的赛道上&#xff0c;Luma AI昨日骄傲地揭开了其旗舰创新——梦幻制造者&#xff08;Dream Machine&#xff09;的神秘面纱&#xff0c;凭借无与伦比的文本到视频及图像到视频转换技术…

如何准备迎接等保测评

准备迎接等保测评的步骤 1. 确定信息系统的等级 根据信息系统的重要性、涉密程度、业务影响等因素&#xff0c;确定信息系统的安全保护等级。这是进行等保测评的第一步&#xff0c;也是整个过程的基础。 2. 系统备案 在确定等级后&#xff0c;需要向属地公安机关部门提交《…

【尚庭公寓SpringBoot + Vue 项目实战】看房预约管理(十三)

【尚庭公寓SpringBoot Vue 项目实战】看房预约管理&#xff08;十三&#xff09; 文章目录 【尚庭公寓SpringBoot Vue 项目实战】看房预约管理&#xff08;十三&#xff09;1、业务说明2、代码开发2.1、根据条件分页查询预约信息2.2、根据ID更新预约状态 1、业务说明 看房预约…

RaphaelScriptHelper(拉斐尔脚本助手)山海经钓鱼脚本开发

缘起 大家周末好,我在玩的游戏山海经出了钓鱼玩法,一天180体力,钓一次鱼差不多得十几秒,也就是用完体力得一直集中精力的操作半个小时,瞬间感觉肝度太高了。因为我玩这个游戏就是有碎片时间点几下,所以我打算自己动手搞一个自动化脚本来解放一下自己的双手。 目前写手机…

Python 踩坑记 -- 调优

前言 继续解决问题 慢 一个服务运行有点慢&#xff0c;当然 Python 本身不快&#xff0c;如果再编码不当那这个可能就是量级上的劣化。 整个 Code 主线逻辑 1700&#xff0c;各依赖封装 3000&#xff0c;主线逻辑也是很久远的痕迹&#xff0c;长函数都很难看清楚一个 if els…

设计通用灵活的LabVIEW自动测试系统

为了在不同客户案例中灵活使用不同设备&#xff08;如采集卡、Modbus模块&#xff09;且保持功能一致的LabVIEW自动测试系统&#xff0c;需要采用模块化的软件架构、配置文件管理、标准化接口和良好的升级维护策略。本文从软件架构、模块化设计、配置管理、升级维护、代码管理和…

Centos实现Mysql8.4安装及主主同步

8.4的Msyql在同步的时候与之前的版本有很大不同&#xff0c;这里记录一下安装流程 Mysql安装 官网下载 选择自己的版本&#xff0c;选第一个 复制下载链接 在服务器上创建一个msyql目录 使用命令下载,链接换自己的 wget https://dev.mysql.com/get/mysql84-community-relea…

【WEEK16】Learning Objectives and Summaries【Spring Boot】【English Version】

Learning Objectives: Learning SpringBoot Learning Content: Reference video tutorials【狂神说Java】SpringBoot最新教程IDEA版通俗易懂Dubbo and Zookeeper Integration Learning time and outputs: Week16 TUE~FRI 2024.6.11【WEEK16】 【DAY2】Dubbo和Zookeeper集成第…

整理好了!2024年最常见 20 道并发编程面试题(九)

十七、请解释synchronized和volatile关键字在Java中的作用。 在Java中&#xff0c;synchronized和volatile是两个用于控制线程同步和内存可见性的关键字。它们各自有不同的用途和作用范围。 synchronized关键字 synchronized关键字主要用于实现线程同步&#xff0c;确保多个…

python IP 端口 socket tcp 介绍

IP 端口 介绍 1、IP IP地址是分配给网络设备上网使用的数字标签&#xff0c;它能够标识网络中唯一的一台设备 windows环境可以使用 ipconfig 来查看自己的iplinux环境可以使用 ifconfig 来查看自己的ip 2、端口 端口是传输数据的通道&#xff0c;每个操作系统上都有 65535个…