IOC-Bean生命周期

IOC-Bean的生命周期

  • bean的生命周期

抛开初始化beanFactory等前面的加载过程,单看bean的生命周期,直接进入/org/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java的594行,doCreate()方法中,找到initializeBean初始化bean instance,这也就是bean生命周期的开始.

点进去查看此方法,重点关注几个重要方法就能明白流程

	/**
	 * Initialize the given bean instance, applying factory callbacks
	 * as well as init methods and bean post processors.
	 * <p>Called from {@link #createBean} for traditionally defined beans,
	 * and from {@link #initializeBean} for existing bean instances.
	 * @param beanName the bean name in the factory (for debugging purposes)
	 * @param bean the new bean instance we may need to initialize
	 * @param mbd the bean definition that the bean was created with
	 * (can also be {@code null}, if given an existing bean instance)
	 * @return the initialized bean instance (potentially wrapped)
	 * @see BeanNameAware
	 * @see BeanClassLoaderAware
	 * @see BeanFactoryAware
	 * @see #applyBeanPostProcessorsBeforeInitialization
	 * @see #invokeInitMethods
	 * @see #applyBeanPostProcessorsAfterInitialization
	 */
	protected Object initializeBean(String beanName, Object bean, @Nullable RootBeanDefinition mbd) {
		if (System.getSecurityManager() != null) {
			AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
                //进入该invokeAwareMethods方法可以看到setBeanName,setClassLoader,setBeanFactory方法
                //重要
				invokeAwareMethods(beanName, bean);
				return null;
			}, getAccessControlContext());
		}
		else {
			invokeAwareMethods(beanName, bean);
		}

		Object wrappedBean = bean;
		if (mbd == null || !mbd.isSynthetic()) {
            //重要
			wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);
		}

		try {
            //重要
			invokeInitMethods(beanName, wrappedBean, mbd);
		}
		catch (Throwable ex) {
			throw new BeanCreationException(
					(mbd != null ? mbd.getResourceDescription() : null),
					beanName, "Invocation of init method failed", ex);
		}
		if (mbd == null || !mbd.isSynthetic()) {
            //重要
			wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);
		}

		return wrappedBean;
	}
  1. 在BeanNameAware中的setBeanName设置bean的名称

  2. 在BeanClassLoaderAware中设置bean的类加载器setBeanClassLoader,回调,提供给bean的实例

  3. 在BeanFactoryAware中,设置setBeanFactory,并且回调,提供给bean的实例

  4. 调用后置处理器 BeanPostProcessor 初始化前方法

  5. 唤醒初始化方法invokeInitMethods(其中包括调用InitializingBean对象的afterPropertiesSet方法,和invokeCustomInitMethod调用自定义初始化方法)

  6. 调用后置处理器 BeanPostProcessor 初始化后方法

  7. bean就可以使用了!

  8. 容器关闭,bean销毁,调用DisposableBean的destroy方法. 回到doGet()方法 ,在创建bean的收就已经注册了 可抛弃属性registerDisposableBeanIfNecessary


参考图片:
生命周期
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值