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

81 篇文章 0 订阅
36 篇文章 0 订阅
在前几节,我们已经讨论过了,对于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.     }
根据上述代码我们可以清晰的看见怎个拦截过程,在这个我们可以将其整个过程理解为一个模板模式的灵活应用;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值