spring 源码个人浅浅分析(5)--- bean生命周期之BeanNameAware、BeanFactoryAware、InitializingBean

Bean生命周期之BeanNameAware、BeanFactoryAware、InitializingBean

在源码AbstractAutowireCapableBeanFactory的创建bean方法doCreate方法中。

bean已经实例化后,且populateBean方法是填充属性之后,就开始调用初始化方法initializeBean。

进入其方法。

protected Object initializeBean(final String beanName, final Object bean, RootBeanDefinition mbd) {
		if (System.getSecurityManager() != null) {
			AccessController.doPrivileged(new PrivilegedAction<Object>() {
				public Object run() {
                    //对特殊的bean处理:Aware、BeanClassloadAware、BeanFactoryAware
					invokeAwareMethods(beanName, bean);
					return null;
				}
			}, getAccessControlContext());
		}
		else {
            //对特殊的bean处理:Aware、BeanClassloadAware、BeanFactoryAware
			invokeAwareMethods(beanName, bean);
		}
		
		Object wrappedBean = bean;
		if (mbd == null || !mbd.isSynthetic()) {
            //开始调用BeanPostProcessor的初始化前处理器
			wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);
		}

		try {
            //激活用户自定义的init方法 
			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()) {
            //开始调用BeanpostProcessor的初始化后处理器
			wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);
		}
		return wrappedBean;
	}

1、进入invokeAwareMethods方法可以看到:

激活Aware接口:1、BeanNameAware。2、BeanClassLoaderAware。3、BeanFactoryAware。

Spring中提供了一些Aware相关接口,比如BeanFactoryAware、ApplicationContextAware、ResourceLoaderAware、ServletContextAware等。实现这些Aware接口的bean在被初始化之后,可以取得一些相对应的资源,而实现ApplicationContextAware的bean,在bean被初始化之后,将被注入ApplicationContext的实例等。

2、在执行bean初始化前处理器之后,开始调用init方法

进入方法invokeInitMethods(beanName, wrappedBean, mbd);

protected void invokeInitMethods(String beanName, final Object bean, RootBeanDefinition mbd)
			throws Throwable {
        //bean是否实现InitializingBean接口中的方法afterPropertiesSet
        //如果实现,则优先执行afterPropertiesSet方法
		boolean isInitializingBean = (bean instanceof InitializingBean);
		if (isInitializingBean && (mbd == null || !mbd.isExternallyManagedInitMethod("afterPropertiesSet"))) {
			if (logger.isDebugEnabled()) {
				logger.debug("Invoking afterPropertiesSet() on bean with name '" + beanName + "'");
			}
			if (System.getSecurityManager() != null) {
				try {
					AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {
						public Object run() throws Exception {
							((InitializingBean) bean).afterPropertiesSet();
							return null;
						}
					}, getAccessControlContext());
				}
				catch (PrivilegedActionException pae) {
					throw pae.getException();
				}
			}				
			else {
				((InitializingBean) bean).afterPropertiesSet();
			}
		}

		if (mbd != null) {
            //如果用户在xml配置文件中配置了属性init-method,则在执行用户自定义的initMethod
			String initMethodName = mbd.getInitMethodName();
			if (initMethodName != null && !(isInitializingBean && "afterPropertiesSet".equals(initMethodName)) &&
					!mbd.isExternallyManagedInitMethod(initMethodName)) {
				invokeCustomInitMethod(beanName, bean, mbd);
			}
		}
	}

由上面方法很容易看得出2点:

1、bean是否实现InitializingBean接口中的方法afterPropertiesSet,如果实现,则优先执行afterPropertiesSet方法,实现自己的初始化业务逻辑。

2、用户在xml配置文件中配置了初始化属性init-method

都是在初始化bean的时候执行的。执行顺序afterPropertiesSet先执行---> init-method后执行

 

即Bean的生命周期:

postProcessBeforeInstantiation(实例化前置处理器{前提是返回null})--->  postProcessAfterInstantiation(实例化后置处理器) ---> postProcessPropertyValues(实例化的属性值) ---> Aware(BeanNameAware、BeanFactoryAware...) ----> postProcessBeforeInitialization(初始化前处理器) ---> lnitializingBean接口的afterPropertiesSet ---> init-method(自定义配置的初始化init-method方法) ---> postProcessAfterInitialization(初始化后处理器)

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值