结果,由于autowire阶段Person已经被创建了,所以结果中person的实例化阶段也被打印了:
[+] > 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=修改了pvs之后的person))
由于我不了解直接修改原始pvs会不会有副作用,所以我选择了克隆一个对象这两个阶段最后的总结#回到这个图上
文章插图
- 从BeanDefinition创建实例化Bean
- 实例化之前如果有
InstantiationAwareBeanPostProcessor
,调用before实例化方法 - 如果并没有一个实例化BeanPostProcessor接管Bean创建,那么进入正常实例化阶段
- 实例化Bean
- 调用
InstantiationAwareBeanPostProcessor
的after实例化方法 - 对pvs进行设置
- 调用
InstantiationAwareBeanPostProcessor
的postProcessProperties方法对pvs进行修改 - 实际的Bean属性设置
文章插图
主要分为几个阶段:
- Aware接口的回调
- BeanPostProcessor的before初始化回调
- 各种初始化方法的回调
- BeanPostProcessor的after初始化回调
// doCreateBean// 属性设置阶段populateBean(beanName, mbd, instanceWrapper);// 初始化阶段exposedObject = initializeBean(beanName, exposedObject, mbd);
进入initalizeBean
,可以看到里面的代码正对应着图中的每一步:protected Object initializeBean(String beanName, Object bean, @Nullable RootBeanDefinition mbd) {// 调用感知方法invokeAwareMethods(beanName, bean);// 调用BeanPostProcessor的before初始化方法Object wrappedBean = bean;if (mbd == null || !mbd.isSynthetic()) {wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);}// 调用自定义初始化方法try {invokeInitMethods(beanName, wrappedBean, mbd);}// 调用BeanPostProcessor的after初始化方法catch (Throwable ex) {}if (mbd == null || !mbd.isSynthetic()) {wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);}return wrappedBean;}
Aware感知方法的调用#进入invokeAwareMethod
方法,里面对BeanNameAware
、BeanClassLoaderAware
、BeanFactoryAware
做了检测,并调用了对应的设置方法:private void invokeAwareMethods(String beanName, Object bean) {if (bean instanceof Aware) {if (bean instanceof BeanNameAware) {((BeanNameAware) bean).setBeanName(beanName);}if (bean instanceof BeanClassLoaderAware) {ClassLoader bcl = getBeanClassLoader();if (bcl != null) {((BeanClassLoaderAware) bean).setBeanClassLoader(bcl);}}if (bean instanceof BeanFactoryAware) {((BeanFactoryAware) bean).setBeanFactory(AbstractAutowireCapableBeanFactory.this);}}}
对于BeanFactory,它只支持这些Aware,ApplicationContext会支持更多的Aware 。Aware感知方法的实战#让
Workbench
类实现BeanFactoryAware
接口并打印出创建它的BeanFactory:@Datapublic class Workbench implements BeanFactoryAware {@Autowiredprivate Person operator;@Overridepublic void setBeanFactory(BeanFactory beanFactory) throws BeansException {System.out.println("BeanFactory => " + beanFactory);}}
文章插图
需要注意的是,一旦你的Bean实现了某个Aware接口,就证明它要感知到某些框架中的东西 , 这会让它直接与框架产生耦合 。
BeanPostProcessor before初始化的调用#这里和之前的套路一样,并且
before
方法可以返回一个包装过的Bean做为代理(默认不包装,这时wrappedBean==bean) protected Object initializeBean(String beanName, Object bean, @Nullable RootBeanDefinition mbd) {invokeAwareMethods(beanName, bean);// 进行before初始化调用Object wrappedBean = bean;if (mbd == null || !mbd.isSynthetic()) {wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);}// ...}
遍历每个BeanPostProcessor , 调用before初始化方法:@Overridepublic Object applyBeanPostProcessorsBeforeInitialization(Object existingBean, String beanName)throws BeansException {Object result = existingBean;for (BeanPostProcessor processor : getBeanPostProcessors()) {Object current = processor.postProcessBeforeInitialization(result, beanName);if (current == null) {return result;}result = current;}return result;}
推荐阅读
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 详解AQS中的condition源码原理
- 怎么去巴基斯坦去巴基斯坦时需要注意什么(从中国去巴基斯坦怎么去)
- 有到过伊拉克的人吗应该怎么去(从国内怎样到伊拉克)
- 从ObjectPool到CAS指令
- 蜘蛛的丝是从哪里吐出来的?
- .net 温故知新:【8】.NET 中的配置从xml转向json
- iqoo7电池多大_iqoo7电池多少毫安
- 【lwip】08-ARP协议一图笔记及源码实现
- <一>从指令角度了解函数堆栈调用过程
- 【lwip】07-链路层收发以太网数据帧源码分析