InitializingBean 接口的 afterPropertiesSet 方法执行

前言

这篇讲了spring 源码 InitializingBean 接口的 afterPropertiesSet 方法执行描述 ,主要是对spring源码 做笔记,需要读者下载spring源码 编译执行 辅助。

InitializingBean 接口afterPropertiesSet 执行时间,源码分析

Bean初始化过程中 doCreateBean 中的 initializeBean(beanName, exposedObject, mbd);

initializeBean 方法内的 invokeInitMethods方法
1. 如果bean 对应的Class实现了InitializingBean 接口 ,invoke afterPropertiesSet 方法
2. 如果Bean class不为NullBean 执行Bean initMethodName 初始化化方法

initializeBean方法源码:

/**
 把实例化后的bean初始化,请求工厂回调 初始化方法和 bean 后置处理器
*/	
protected Object initializeBean(String beanName, Object bean, @Nullable RootBeanDefinition mbd) {
		if (System.getSecurityManager() != null) {
			AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
				invokeAwareMethods(beanName, bean);
				return null;
			}, getAccessControlContext());
		}
		else {
			invokeAwareMethods(beanName, bean);
		}

		Object wrappedBean = bean;
		if (mbd == null || !mbd.isSynthetic()) {
            //  初始化Bean之前 请求应用Bean后置处理器
            //  1. 执行Bean后置处理器postProcessBeforeInitialization
            //  比如 执行Aware 子类中的postProcessBeforeInitialization
			wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);
		}

		try {
            // 调用 初始化化方法
            // 如果bean 对应的Class实现了InitializingBean 接口 ,invoke afterPropertiesSet 方法
            // 如果Bean class不为NullBean   执行Bean initMethodName 初始化化方法
			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()) {
            // 初始化化Bean之后 请求bean后置处理器
            //
			wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);
		}

		return wrappedBean;
	}

invokeInitMethods 源码

从下面源码可以看出,如果该bean实例 是InitializingBean 类型的话,执行afterPropertiesSet 方法

	protected void invokeInitMethods(String beanName, Object bean, @Nullable RootBeanDefinition mbd)
			throws Throwable {

		boolean isInitializingBean = (bean instanceof InitializingBean);
		if (isInitializingBean && (mbd == null || !mbd.isExternallyManagedInitMethod("afterPropertiesSet"))) {
			if (logger.isTraceEnabled()) {
				logger.trace("Invoking afterPropertiesSet() on bean with name '" + beanName + "'");
			}
			if (System.getSecurityManager() != null) {
				try {
					AccessController.doPrivileged((PrivilegedExceptionAction<Object>) () -> {
						((InitializingBean) bean).afterPropertiesSet();
						return null;
					}, getAccessControlContext());
				}
				catch (PrivilegedActionException pae) {
					throw pae.getException();
				}
			}
			else {
				((InitializingBean) bean).afterPropertiesSet();
			}
		}

		if (mbd != null && bean.getClass() != NullBean.class) {
			String initMethodName = mbd.getInitMethodName();
			if (StringUtils.hasLength(initMethodName) &&
					!(isInitializingBean && "afterPropertiesSet".equals(initMethodName)) &&
					!mbd.isExternallyManagedInitMethod(initMethodName)) {
				invokeCustomInitMethod(beanName, bean, mbd);
			}
		}
	}

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐