下图是BeanPostProcess的关系结构图:
[img]
[/img]
InstantiationAwareBeanPostProcessor 该接口非常重要,在Spring的Aop自动代理的实现中就需要使用该接口,原因很简单,我们的织入我们需要的我们的一些列的逻辑到目标中,所以我们返回的应该目标对象的代理对象。
public abstract class AbstractAutoProxyCreator extends ProxyConfig implements SmartInstantiationAwareBeanPostProcessor, BeanClassLoaderAware, BeanFactoryAware, Ordered, AopInfrastructureBean { ............................................................. public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException { Object cacheKey = getCacheKey(beanClass, beanName); if (!this.targetSourcedBeans.contains(cacheKey)) { if (this.advisedBeans.contains(cacheKey) || this.nonAdvisedBeans.contains(cacheKey)) { return null; } if (isInfrastructureClass(beanClass) || shouldSkip(beanClass, beanName)) { this.nonAdvisedBeans.add(cacheKey); return null; } } // Create proxy here if we have a custom TargetSource. // Suppresses unnecessary default instantiation of the target bean: // The TargetSource will handle target instances in a custom fashion. //获取目标源 TargetSource targetSource = getCustomTargetSource(beanClass, beanName); if (targetSource != null) { this.targetSourcedBeans.add(beanName); Object[] specificInterceptors = getAdvicesAndAdvisorsForBean(beanClass, beanName, targetSource); //重点看这句,创建代理对象,通过ProxyFactory(aop的代码编程方式) Object proxy = createProxy(beanClass, beanName, specificInterceptors, targetSource); this.proxyTypes.put(cacheKey, proxy.getClass()); //然后返回代理对象 return proxy; } return null; } }
以InstantiationAwareBeanPostProcessor为例(其他的都一样类似)
public class InstantiationAwareBeanPostProcessorImp implements InstantiationAwareBeanPostProcessor { @Override public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { System.out.println("postProcessBeforeInitialization" + bean + " beanName:" + beanName); return bean; } @Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { // TODO Auto-generated method stub return bean; } /** * */ @Override public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException { System.out.println("postProcessBeforeInstantiation(class)" + beanClass + " beanName:" + beanName); //此处修改了Bean本身所需要的实例对象,阻断了Spring的创建Bean执行流 程 return new InstantiationAwareBeanPostProcessorImp(); } @Override public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException { // TODO Auto-generated method stub return false; } @Override public PropertyValues postProcessPropertyValues(PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeansException { System.out.println("postProcessPropertyValues"); return null; } }
当Spring在执行创建Bean的时候,会先去执行InstantiationAwareBeanPostProcessor 该接口里面的postProcessBeforeInstantiation如果返回的不是null则调用BeanPostProcessor.postProcessAfterInitialization方法,如上例我们修改了bean本省应该返回的实例,只要这2个方法不返回NULL,Spring就会停止后面的Bean的创建,直接返回该实例(return new InstantiationAwareBeanPostProcessorImp())
@Override protected Object createBean(final String beanName, final RootBeanDefinition mbd, final Object[] args) throws BeanCreationException { if (logger.isDebugEnabled()) { logger.debug("Creating instance of bean '" + beanName + "'"); } // Make sure bean class is actually resolved at this point. resolveBeanClass(mbd, beanName); // Prepare method overrides. try { mbd.prepareMethodOverrides(); } catch (BeanDefinitionValidationException ex) { throw new BeanDefinitionStoreException(mbd.getResourceDescription(), beanName, "Validation of method overrides failed", ex); } try { // Give BeanPostProcessors a chance to return a proxy instead of the target bean instance. //在这里执行InstantiationAwareBeanPostProcessor里面的方法, // 如果返回值不为NULL直接退出 Object bean = resolveBeforeInstantiation(beanName, mbd); if (bean != null) { return bean; } } catch (Throwable ex) { throw new BeanCreationException(mbd.getResourceDescription(), beanName, "BeanPostProcessor before instantiation of bean failed", ex); } //否则去执行后续的创建过程 Object beanInstance = doCreateBean(beanName, mbd, args); if (logger.isDebugEnabled()) { logger.debug("Finished creating instance of bean '" + beanName + "'"); } return beanInstance; }