《Spring设计思想》13-bean的实例化和初始化阶段概览

前面几节终于讲完了bean的实例化过程,围绕着bean的实例化,spring做了众多的准备工作:

1-加载beanDefinition

2-mergeBeanDefinition

3-加载bean的Class

4-调用InstantiationAwareBeanPostProcessor的postProcessBeforeInstantiation方法打断实例化过程

5-实例化

6-调用InstantiationAwareBeanPostProcessor的postProcessAfterInstantiation方法打断bean的PropertyValues赋值

7-调用InstantiationAwareBeanPostProcessor的postProcessrProperties方法,修改bean的Propertyvalues的值

8-applyPropertyValue

那么接下来bean的实例化阶段已经完成。进入下一个阶段-初始化

初始化的路程也必定艰辛坎坷,这一次我们仔细观察一下initializeBean(beanName,exposeObject,mbd);

	/**
	 * Initialize the given bean instance, applying factory callbacks
	 * as well as init methods and bean post processors.
	 */
	protected Object initializeBean(final String beanName, final Object bean, @Nullable RootBeanDefinition mbd) {
		
        //调用bean实现的Aware接口方法
		invokeAwareMethods(beanName, bean);
		

		Object wrappedBean = bean;
		if (mbd == null || !mbd.isSynthetic()) {
        //   调用BeanPostProcessor的beforeInitialization方法
			wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);
		}

		try {
        // 调用初始化方法
			invokeInitMethods(beanName, wrappedBean, mbd);
		}
		// 调用beanPostProcessor的afterInitialization方法
		if (mbd == null || !mbd.isSynthetic()) {
			wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);
		}

		return wrappedBean;
	}

整个方法看起来就四个重点:

1-invokeAwareMethods(beanName,bean);--调用bean实现的三个Aware接口BeanNameAware/BeanClassLoaderAware/BeanFactoryAware

2-applyBeanPostProcessorsBeforeInitialization(wrapedBean,beanName);--执行BeanPostProcessor的postProcessBeforeInitalization方法

3-invokeInitMethod(beanName,wrappedBean,mbd);--调用Bean实现的InitializingBean接口,调用自定义的初始化方法

4-applyBeanPostPeocessorsAfrerInitialization(wrappedBean,beanName);--调用BeanPostProcessor的postProcessAfterInitialization方法

以上就是bean的初始化的大体概览,下一节我们仔细看一下invokeAwareMethods到底发生了啥

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值