现在的位置: 首页 > 综合 > 正文

Spring源代码分析(6)—BeanPostProcessor(半路杀出个程咬金)

2014年02月17日 ⁄ 综合 ⁄ 共 2650字 ⁄ 字号 评论关闭
在前几节,我们已经讨论过了,对于beanFactory的拦截修改,我们有两个点:
一个是BeanFactoryPostProcessor;一个则是现在的BeanPostProcessor:

BeanPostProcessor:

  1. package org.springframework.beans.factory.config;
  2. import org.springframework.beans.BeansException;
  3.     Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException;
  4.     Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException;
  5. }

postProcessBeforeInitialization是在initMethod以前调用,我们可以在此根据beanName判断是不是自己要拦截的bean,然后引用到bean的实例,对其进行一系列的set操作,在这里进行拦截的自定义操作;

我们看下这个接口的方法是在哪里被调用的;

在AbstractAutowireCapableBeanFactory类中:

  1.             bean = applyBeanPostProcessorsBeforeInitialization(bean, beanName);
  2.             invokeInitMethods(beanName, bean, mergedBeanDefinition);
  3.             bean = applyBeanPostProcessorsAfterInitialization(bean, beanName);

  1. public Object applyBeanPostProcessorsBeforeInitialization(Object existingBean, String beanName)
  2.             throws BeansException {
  3.         if (logger.isDebugEnabled()) {
  4.             logger.debug("Invoking BeanPostProcessors before initialization of bean '" + beanName + "'");
  5.         }
  6.         Object result = existingBean;
  7.         for (Iterator it = getBeanPostProcessors().iterator(); it.hasNext();) {
  8.             BeanPostProcessor beanProcessor = (BeanPostProcessor) it.next();
  9.             result = beanProcessor.postProcessBeforeInitialization(result, beanName);
  10.             if (result == null) {
  11.                 throw new BeanCreationException(beanName,
  12.                         "postProcessBeforeInitialization method of BeanPostProcessor [" + beanProcessor +
  13.                         "] returned null for bean [" + result + "] with name [" + beanName + "]");
  14.             }
  15.         }
  16.         return result;
  17.     }

 

  1. public Object applyBeanPostProcessorsAfterInitialization(Object existingBean, String beanName)
  2.             throws BeansException {
  3.         if (logger.isDebugEnabled()) {
  4.             logger.debug("Invoking BeanPostProcessors after initialization of bean '" + beanName + "'");
  5.         }
  6.         Object result = existingBean;
  7.         for (Iterator it = getBeanPostProcessors().iterator(); it.hasNext();) {
  8.             BeanPostProcessor beanProcessor = (BeanPostProcessor) it.next();
  9.             result = beanProcessor.postProcessAfterInitialization(result, beanName);
  10.             if (result == null) {
  11.                 throw new BeanCreationException(beanName,
  12.                         "postProcessAfterInitialization method of BeanPostProcessor [" + beanProcessor +
  13.                         "] returned null for bean [" + result + "] with name [" + beanName + "]");
  14.             }
  15.         }
  16.         return result;
  17.     }

根据上述代码我们可以清晰的看见怎个拦截过程,在这个我们可以将其整个过程理解为一个模板模式的灵活应用;

抱歉!评论已关闭.