Spring框架学习[IoC容器高级特性](五)

2014-11-24 03:11:10 · 作者: · 浏览: 3
em.getSecurityManager() != null) {
  • AccessControlContext acc = getAccessControlContext(); try {
  • //实现PrivilegedExceptionAction接口的匿名内置类 //根据JVM检查权限,然后决定BeanFactory创建实例对象
  • object = AccessController.doPrivileged(new PrivilegedExceptionAction() { public Object run() throws Exception {
  • //调用BeanFactory接口实现类的创建对象方法 return factory.getObject();
  • } }, acc);
  • } catch (PrivilegedActionException pae) {
  • throw pae.getException(); }
  • } else {
  • //调用BeanFactory接口实现类的创建对象方法 object = factory.getObject();
  • } }
  • catch (FactoryBeanNotInitializedException ex) { throw new BeanCurrentlyInCreationException(beanName, ex.toString());
  • } catch (Throwable ex) {
  • throw new BeanCreationException(beanName, FactoryBean threw exception on object creation, ex); }
  • //创建出来的实例对象为null,或者因为单态对象正在创建而返回null if (object == null && isSingletonCurrentlyInCreation(beanName)) {
  • throw new BeanCurrentlyInCreationException( beanName, FactoryBean which is currently in creation returned null from getObject);
  • } //为创建出来的Bean实例对象添加BeanPostProcessor后置处理器
  • if (object != null && shouldPostProcess) { try {
  • object = postProcessObjectFromFactoryBean(object, beanName); }
  • catch (Throwable ex) { throw new BeanCreationException(beanName, Post-processing of the FactoryBean's object failed, ex);
  • } }
  • return object; }

    从上面的源码分析中,我们可以看出,BeanFactory接口调用其实现类的getObject方法来实现创建Bean实例对象的功能。

    (4).工厂Bean的实现类getObject方法创建Bean实例对象:

    FactoryBean的实现类有非常多,比如:Proxy、RMI、JNDI、ServletContextFactoryBean等等,FactoryBean接口为Spring容器提供了一个很好的封装机制,具体的getObject有不同的实现类根据不同的实现策略来具体提供,我们分析一个最简单的AnnotationTestFactoryBean的实现源码:

    [java]
    1. public class AnnotationTestBeanFactory implements FactoryBean { private final FactoryCreatedAnnotationTestBean instance = new FactoryCreatedAnnotationTestBean();
    2. public AnnotationTestBeanFactory() { this.instance.setName(FACTORY);
    3. } //AnnotationTestBeanFactory产生Bean实例对象的实现
    4. public IJmxTestBean getObject() throws Exception { return this.instance;
    5. } public Class getObjectType() {
    6. return FactoryCreatedAnnotationTestBean.class; }
    7. public boolean isSingleton() { return true;
    8. } }

      其他的Proxy,RMI,JNDI等等,都是根据相应的策略提供getObject的实现。这里不做一一分析,这已经不是Spring的核心功能,有需要的时候再去深入研究。

      4.BeanPostProcessor后置处理器的实现:

      BeanPostProcessor后置处理器是Spring IoC容器经常使用到的一个特性,这个Bean后置处理器是一个监听器,可以监听容器触发的Bean声明周期事件。后置处理器想容器注册以后,容器中管理的Bean就具备了接收IoC容器事件回调的能力。

      BeanPostProcessor的使用非常简单,只需要提供一个实现接口BeanPostProcessor的实现类,然后在Bean的配置文件中设置即可。

      (1).BeanPostProcessor的源码如下:

      [java
      1. package org.springframework.beans.factory.config; import org.springframework.beans.BeansException;
      2. public interface BeanPostProcessor { //为在Bean的初始化前提供回调入口
      3. Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException; //为在Bean的初始化之后提供回调入口
      4. Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException; }

        这两个回调的入口都是和容器管理的Bean的生命周期事件紧密相关,可以为用户提供在Spring IoC容器初始化Bean过程中自定义的处理操作。

        (2).AbstractAutowireCapableBeanFactory类对容器生成的Bean添加后置处理器:

        BeanPostProcessor后置处理器的调用发生在Spring IoC容器完成对Bean实例对象的创建和属性的依赖注入完成之后,在对Spring依赖注入的源码分析过程中我们知道,当应用程序第一次调用getBean方法(lazy-init预实例化除外)向Spring IoC容器索取指定Bean时触发Spring IoC容器创建Bean实例对象并进行依赖注入的过程,其中真正实现创建Bean对象并进行依赖注入的方法是AbstractAutowireCapableBeanFactory类的doCreateBean方法,主要源码如下:

        [java]
        1. //真正创建Bean的方法 protected Object doCre