spring源码之BeanPostProcessor

本文深入探讨Spring框架中BeanPostProcessor接口的作用与实现机制,包括postProcessBeforeInitialization和postProcessAfterInitialization方法的调用时机及流程。揭示了BeanPostProcessor如何在Bean初始化前后进行自定义操作,以及在Spring AOP中的应用。

spring源码之BeanPostProcessor

工厂式的钩子方法,用来对于一个新的实例化之后的bean进行自定义的修改(custom modification),比如,校验标记接口(如 Serializable), 用代理类来封闭这个bean。spring内部的Aop实现也是通过BeanPostProcessor实现的

BeanPostProcessor源码

public interface BeanPostProcessor {


	Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException;


	Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException;

}

postProcessBeforeInitialization方法

这个方法是在这个实例的bean对象还没有被初始化之前被调用的,如(是在InitializingBean接口的afterPropertiesSet方法和自定义 init-method 方法之前)被调用的,这里bean已经是被注入了属性值,而通过这个方法之后返回的bean应该是经过包装之后的bean(相对于原来的bean)

AbstractAutowireCapableBeanFactory类的方法invokeInitMethods中:

protected Object initializeBean(final String beanName, final Object bean, RootBeanDefinition mbd) {
	if (System.getSecurityManager() != null) {
		AccessController.doPrivileged(new PrivilegedAction<Object>() {
			@Override
			public Object run() {
				invokeAwareMethods(beanName, bean);
				return null;
			}
		}, getAccessControlContext());
	}
	else {
    //这个方法中就是调用InitializingBean.afterPropertiesSet方法
    //和调用 init-method的实现地方!!
		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;
}


@Override
public Object applyBeanPostProcessorsBeforeInitialization(Object existingBean, String beanName)
    throws BeansException {

  Object result = existingBean;
  for (BeanPostProcessor processor : getBeanPostProcessors()) {
    result = processor.postProcessBeforeInitialization(result, beanName);
    if (result == null) {
      return result;
    }
  }
  return result;
}
postProcessAfterInitialization方法

这个方法是在这个实例的bean对象还没有被初始化之后被调用的,通常是在InitializingBean接口的afterPropertiesSet方法和自定义 init-method 方法之后被调用。这个方法即使是在InstantiationAwareBeanPostProcessorpostProcessBeforeInstantiation方法被短路(即返回一个非non-null的对象)也会被调用,这也是和其他BeanPostProcessor中的回调方法不一样的地方

AbstractAutowireCapableBeanFactory类的方法resolveBeforeInstantiation中:

protected Object resolveBeforeInstantiation(String beanName, RootBeanDefinition mbd) {
	Object bean = null;
	if (!Boolean.FALSE.equals(mbd.beforeInstantiationResolved)) {
		// Make sure bean class is actually resolved at this point.
		if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) {
			Class<?> targetType = determineTargetType(beanName, mbd);
			if (targetType != null) {
				bean = applyBeanPostProcessorsBeforeInstantiation(targetType, beanName);
				if (bean != null) {
          //即使是在`InstantiationAwareBeanPostProcessor`的`postProcessBeforeInstantiation·方法被短路
          //postProcessAfterInitialization方法也会被执行!!!
					bean = applyBeanPostProcessorsAfterInitialization(bean, beanName);
				}
			}
		}
		mbd.beforeInstantiationResolved = (bean != null);
	}
	return bean;
}


public Object applyBeanPostProcessorsAfterInitialization(Object existingBean, String beanName)
			throws BeansException {

	Object result = existingBean;
	for (BeanPostProcessor processor : getBeanPostProcessors()) {
		result = processor.postProcessAfterInitialization(result, beanName);
		if (result == null) {
			return result;
		}
	}
	return result;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值