@Nullableprotected Object applyBeanPostProcessorsBeforeInstantiation(Class<?> beanClass, String beanName) {for (InstantiationAwareBeanPostProcessor bp : getBeanPostProcessorCache().instantiationAware) {Object result = bp.postProcessBeforeInstantiation(beanClass, beanName);if (result != null) {return result;}}return null;}
这不就是对所有的InstantiationAwareBeanPostProcessor
进行遍历调用吗 , 取第一个返回结果的Processor的结果 。大家说,这里用到了什么设计模式??
InstantiationAwareBeanPostProcessor before hook的使用#下面 , 我们自己创建一个实现类,它的功能就是打印所有进来的Bean名字和类型:
public class MyInstantiationProcessor implements InstantiationAwareBeanPostProcessor {@Overridepublic Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {System.out.println("[+] > MyInstantiationProcessor before bean instantiation : " + beanName + " => " + beanClass.getName());return null;}}
【从BeanFactory源码看Bean的生命周期】下面,我们把它设置到BeanFactory中,然后尝试获取Bean:
factory.addBeanPostProcessor(new MyInstantiationProcessor());Workbench workbench = factory.getBean(Workbench.class);System.out.println(workbench);
输出:
[+] > MyInstantiationProcessor before bean instantiation : workbench => top.yudoge.springserials.basic.beanfactory.beans.Workbench[+] > MyInstantiationProcessor before bean instantiation : person => top.yudoge.springserials.basic.beanfactory.beans.PersonWorkbench(operator=Person(name=Yudoge))
因为Workbench
对象依赖Person
对象,所以引起了两个对象的连锁创建 , 最后一行我们得到了Workbench
对象 。
下面我们尝试让BeanPostProcessor针对Person类返回一个另外的Bean,而不是null
:
public class MyInstantiationProcessor implements InstantiationAwareBeanPostProcessor {@Overridepublic Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {System.out.println("[+] > MyInstantiationProcessor before bean instantiation : " + beanName + " => " + beanClass.getName());if (beanClass.getName().equals(Person.class.getName())) {return new Person("我是MyInstantiationProcessor返回的Person");}return null;}}
运行:
[+] > MyInstantiationProcessor before bean instantiation : workbench => top.yudoge.springserials.basic.beanfactory.beans.Workbench[+] > MyInstantiationProcessor before bean instantiation : person => top.yudoge.springserials.basic.beanfactory.beans.PersonWorkbench(operator=Person(name=我是MyInstantiationProcessor返回的Person))
成功的返回了我们创建出来的Person 。
对目前的InstantiationAwareBeanPostProcessor
的before阶段做一个总结:
- BeanFactory会在创建Bean之前调用所有这种Processor的before方法
- 如果在before中返回了一个对象,那么这个对象就会代替原来的Bean,并且该bean的初始化后(不是实例化后)方法会被立即调用
- 否则,就是before中返回null , 这时进入正常的Bean创建流程
这个总结只是实际实例化Bean#所以,当AbstractAutowireCapableBeanFactory
中的实现方式,是否有其它BeanFactory以其它方式实现 , 暂不明确
InstantiationAwareBeanPostProcessor
没有返回一个替代对象时,进入正常的Bean创建流程,开始实例化Bean 。回到
AbstractAutowireBeanFactory
的createBean
方法:@Overrideprotected Object createBean(String beanName, RootBeanDefinition mbd, @Nullable Object[] args)throws BeanCreationException {RootBeanDefinition mbdToUse = mbd;try {Object bean = resolveBeforeInstantiation(beanName, mbdToUse);if (bean != null) {return bean;}}catch (Throwable ex) { }try {// 创建BeanObject beanInstance = doCreateBean(beanName, mbdToUse, args);return beanInstance;}catch (BeanCreationException | ImplicitlyAppearedSingletonException ex) {throw ex;}catch (Throwable ex) {}}
这个doCreateBean
方法就是用来实际实例化Bean的:protected Object doCreateBean(String beanName, RootBeanDefinition mbd, @Nullable Object[] args)throws BeanCreationException {BeanWrapper instanceWrapper = null;// 如果是Singleton,从缓存中拿原来的Beanif (mbd.isSingleton()) {instanceWrapper = this.factoryBeanInstanceCache.remove(beanName);}if (instanceWrapper == null) {// 实例化BeaninstanceWrapper = createBeanInstance(beanName, mbd, args);}// 从BeanWrapper中拿出实际的Bean对象Object bean = instanceWrapper.getWrappedInstance();// ...}
向populateBean方法中试探——InstantiationAwareBeanPostProcessor的after方法#
推荐阅读
- 详解AQS中的condition源码原理
- 怎么去巴基斯坦去巴基斯坦时需要注意什么(从中国去巴基斯坦怎么去)
- 有到过伊拉克的人吗应该怎么去(从国内怎样到伊拉克)
- 从ObjectPool到CAS指令
- 蜘蛛的丝是从哪里吐出来的?
- .net 温故知新:【8】.NET 中的配置从xml转向json
- iqoo7电池多大_iqoo7电池多少毫安
- 【lwip】08-ARP协议一图笔记及源码实现
- <一>从指令角度了解函数堆栈调用过程
- 【lwip】07-链路层收发以太网数据帧源码分析