Spring IOC学习(3)——FactoryBeanRegistrySupport

FactoryBeanRegistrySupport:在DefaultSingletonBeanRegistry的基础上,补充了对FactoryBean实例的处理能力

Spring IOC学习(3)——FactoryBeanRegistrySupport

/** Cache of singleton objects created by FactoryBeans: FactoryBean name --> object 
 * FactoryBean创建的实例缓存;FactoryBean Name->FactoryBean创建的bean对象
 */
 private final Map<String, Object> factoryBeanObjectCache = new ConcurrentHashMap<String, Object>(16);

 /**
  * Obtain an object to expose from the given FactoryBean, if available
  * in cached form. Quick check for minimal synchronization.
  * @param beanName the name of the bean
  * @return the object obtained from the FactoryBean,
  * or {@code null} if not available
  * 根据工厂bean的名字,从缓存中获取对应工厂bean创建的对象;缓存中没有则返回null
  */
 protected Object getCachedObjectForFactoryBean(String beanName) {
  Object object = this.factoryBeanObjectCache.get(beanName);
  return (object != NULL_OBJECT ? object : null);
 }

/**
  * 获取FactoryBean对应的产品
  * Obtain an object to expose from the given FactoryBean.
  * @param factory the FactoryBean instance
  * @param beanName the name of the bean
  * @param shouldPostProcess whether the bean is subject to post-processing
  * @return the object obtained from the FactoryBean
  * @throws BeanCreationException if FactoryBean object creation failed
  * @see org.springframework.beans.factory.FactoryBean#getObject()
  */
 protected Object getObjectFromFactoryBean(FactoryBean<?> factory, String beanName, boolean shouldPostProcess) {
  if (factory.isSingleton() && containsSingleton(beanName)) {
   //要获取的bean是单例bean,且已注册单例缓存中存在对应的工厂bean
   synchronized (getSingletonMutex()) {
    //尝试从缓存中获取;获取到则直接返回
    Object object = this.factoryBeanObjectCache.get(beanName);
    if (object == null) {
     //缓存中没有,则通过工厂bean新建对象
     object = doGetObjectFromFactoryBean(factory, beanName);
     // Only post-process and store if not put there already during getObject() call above
     // (e.g. because of circular reference processing triggered by custom getBean calls)
     Object alreadyThere = this.factoryBeanObjectCache.get(beanName);
     if (alreadyThere != null) {
      object = alreadyThere;
     }
     else {
      if (object != null && shouldPostProcess) {
       if (isSingletonCurrentlyInCreation(beanName)) {
        // Temporarily return non-post-processed object, not storing it yet..
        return object;
       }
       beforeSingletonCreation(beanName);
       try {
        object = postProcessObjectFromFactoryBean(object, beanName);
       }
       catch (Throwable ex) {
        throw new BeanCreationException(beanName,
          "Post-processing of FactoryBean's singleton object failed", ex);
       }
       finally {
        afterSingletonCreation(beanName);
       }
      }
      if (containsSingleton(beanName)) {
       this.factoryBeanObjectCache.put(beanName, (object != null ? object : NULL_OBJECT));
      }
     }
    }
    return (object != NULL_OBJECT ? object : null);
   }
  }
  else {
   //要获取的bean不是单例的,则每次都新创建一个返回
   Object object = doGetObjectFromFactoryBean(factory, beanName);
   if (object != null && shouldPostProcess) {
    try {
     //创建完bean之后,要做一些后置处理
     object = postProcessObjectFromFactoryBean(object, beanName);
    }
    catch (Throwable ex) {
     throw new BeanCreationException(beanName, "Post-processing of FactoryBean's object failed", ex);
    }
   }
   return object;
  }
 }

/**
  * Post-process the given object that has been obtained from the FactoryBean.
  * The resulting object will get exposed for bean references.
  * <p>The default implementation simply returns the given object as-is.
  * Subclasses may override this, for example, to apply post-processors.
  * @param object the object obtained from the FactoryBean.
  * @param beanName the name of the bean
  * @return the object to expose
  * @throws org.springframework.beans.BeansException if any post-processing failed
  * 后置处理,子类重写具体逻辑
  */
 protected Object postProcessObjectFromFactoryBean(Object object, String beanName) throws BeansException {
  return object;
 }

org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory子类

 /**
  * Applies the {@code postProcessAfterInitialization} callback of all
  * registered BeanPostProcessors, giving them a chance to post-process the
  * object obtained from FactoryBeans (for example, to auto-proxy them).
  * @see #applyBeanPostProcessorsAfterInitialization
  * 重写逻辑
  */
 @Override
 protected Object postProcessObjectFromFactoryBean(Object object, String beanName) {
  return applyBeanPostProcessorsAfterInitialization(object, beanName);
 }

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

  Object result = existingBean;
  for (BeanPostProcessor processor : getBeanPostProcessors()) {
   //拿出容器内的所有BeanPostProcessor,对新建的bean执行后置处理
   result = processor.postProcessAfterInitialization(result, beanName);
   if (result == null) {
    return result;
   }
  }
  return result;
 }

 

上一篇:[WPF] Caliburn Micro学习三 Binding


下一篇:Spring 的 getBean 方法源码解析