1. 程式人生 > >Spring原始碼:從FactoryBean獲取Bean的例項

Spring原始碼:從FactoryBean獲取Bean的例項

主程式碼

類AbstractBeanFactory

protected <T> T doGetBean(......){
  // ......
  bean = getObjectForBeanInstance(sharedInstance, name, beanName, null);
  // ......
}
protected Object getObjectForBeanInstance(
            Object beanInstance, String name, String beanName, RootBeanDefinition mbd) {

        // 若name傳遞的是如“&student”且該bean不是FactoryBean,丟擲異常
if (BeanFactoryUtils.isFactoryDereference(name) && !(beanInstance instanceof FactoryBean)) { throw new BeanIsNotAFactoryException(transformedBeanName(name), beanInstance.getClass()); } // 如果beanInstance不是FactoryBean,是一個普通的Bean,直接返回 if (!(beanInstance instanceof
FactoryBean) || BeanFactoryUtils.isFactoryDereference(name)) { return beanInstance; } Object object = null; if (mbd == null) { // 嘗試從快取中獲取Bean object = getCachedObjectForFactoryBean(beanName); } if (object == null) { // Return bean instance from factory.
FactoryBean<?> factory = (FactoryBean<?>) beanInstance; // Caches object obtained from FactoryBean if it is a singleton. if (mbd == null && containsBeanDefinition(beanName)) { mbd = getMergedLocalBeanDefinition(beanName); } boolean synthetic = (mbd != null && mbd.isSynthetic()); object = getObjectFromFactoryBean(factory, beanName, !synthetic); } return object; }

詳細分析

  1. getCachedObjectForFactoryBean這個方法放在FactoryBeanRegistrySupport類中,FactoryBeanRegistrySupport類在DefaultSingletonBeanRegistry類的基礎上(繼承於DefaultSingletonBeanRegistry類)增加了對FactoryBean的特殊處理(從命名上可以看出),下面是從快取中獲取FactoryBean:

    private final Map<String, Object> factoryBeanObjectCache = new ConcurrentHashMap<String, Object>(16);
    
    protected Object getCachedObjectForFactoryBean(String beanName) {
        Object object = this.factoryBeanObjectCache.get(beanName);
        return (object != NULL_OBJECT ? object : null);
    }

    問題來了,FactoryBean什麼時候放到快取中呢?

    是由本類(FactoryBeanRegistrySupport)的getObjectFromFactoryBean方法設定進去的,該方法內若從快取中獲取不到Bean,就會呼叫factory.getObject()獲取Bean,並put到factoryBeanObjectCache中(第一次獲取factoryBeanObjectCache一定獲取不到的)。

  2. 接下來看下containsBeanDefinition(beanName)方法,定位到DefaultListableBeanFactory類中,該類是bean載入的核心部分,是Spring註冊及載入的預設實現,功能強大。

    private final Map<String, BeanDefinition> beanDefinitionMap = new ConcurrentHashMap<String, BeanDefinition>(256);
    
    public boolean containsBeanDefinition(String beanName) {
        Assert.notNull(beanName, "Bean name must not be null");
        return this.beanDefinitionMap.containsKey(beanName);
    }

    從程式碼中可以看出,beanDefinitionMap是否包含beanName的key,beanDefinitionMap是什麼時候put進去值的時候,分析spring配置檔案的載入方式不難發現,所有被掃描的Bean都會註冊到beanDefinitionMap中,具體參考該類中registerBeanDefinition方法。

  3. 再看getMergedLocalBeanDefinition(beanName)方法,該方法的作用是獲取子Bean與父Bean合併後RootBeanDefinition,即如果beanName有父Bean,需要把父Bean與該Bean合併成一個RootBeanDefinition(屬性合併等等,參考AbstractBeanDefinition的overrideFrom方法,同時參考parent標籤的使用)

    protected RootBeanDefinition getMergedLocalBeanDefinition(String beanName) throws BeansException {
        // 嘗試從快取中獲取RootBeanDefinition
        RootBeanDefinition mbd = this.mergedBeanDefinitions.get(beanName);
        if (mbd != null) {
            return mbd;
        }
           // 若獲取失敗,呼叫以下方法來獲取RootBeanDefinition
        return getMergedBeanDefinition(beanName, getBeanDefinition(beanName));
    }
    
    protected RootBeanDefinition getMergedBeanDefinition(String beanName, BeanDefinition bd) throws BeanDefinitionStoreException {
     return getMergedBeanDefinition(beanName, bd, null);
    }
    protected RootBeanDefinition getMergedBeanDefinition(String beanName, BeanDefinition bd, BeanDefinition containingBd) throws BeanDefinitionStoreException {
     // 對快取map上鎖
     synchronized (this.mergedBeanDefinitions) {
       RootBeanDefinition mbd = null;
       // containingBd可以看作是內部Bean
       if (containingBd == null) {
         mbd = this.mergedBeanDefinitions.get(beanName);
       }
    
       if (mbd == null) {
         if (bd.getParentName() == null) {
           // 沒有父Bean
           if (bd instanceof RootBeanDefinition) {
             mbd = ((RootBeanDefinition) bd).cloneBeanDefinition();
           } else {
             mbd = new RootBeanDefinition(bd);
           }
         } else {
           // 有父Bean
           BeanDefinition pbd;
           // 獲取父Bean的名稱
           String parentBeanName = transformedBeanName(bd.getParentName());
           if (!beanName.equals(parentBeanName)) {
             pbd = getMergedBeanDefinition(parentBeanName);
           } else {
             // 獲取父Bean的BeanFactory,並從中獲取父BeanDefinition(流程同子BeanDefinition的獲取過程)
             BeanFactory parent = getParentBeanFactory();
             if (parent instanceof ConfigurableBeanFactory) {
               pbd = ((ConfigurableBeanFactory)parent).getMergedBeanDefinition(parentBeanName);
             } else {
               // 丟擲異常(父Bean工廠不是AbstractBeanFactory型別)
             }
           }
           // 深度拷貝
           mbd = new RootBeanDefinition(pbd);
           // 父子BeanDefinition屬性合併
           mbd.overrideFrom(bd);
         }
    
         // Set default singleton scope, if not configured before.
         if (!StringUtils.hasLength(mbd.getScope())) {
           mbd.setScope(RootBeanDefinition.SCOPE_SINGLETON);
         }
    
         if (containingBd != null && !containingBd.isSingleton() && mbd.isSingleton()) {
           mbd.setScope(containingBd.getScope());
         }
    
         if (containingBd == null && isCacheBeanMetadata()) {
           this.mergedBeanDefinitions.put(beanName, mbd);
         }
    
       }
       return mbd;
     }
    }
  4. 關於synthetic的意義:該BeanDefinition是不是應用(這一點可討論,感覺說是編譯器合成的比較貼切)自己擁有並生成的,一般自定義的Bean都是返回false,由應用合成的返回true,下面程式碼:若取到的Bean是自定義的返回false,若是應用自己合成的返回true

    boolean synthetic = (mbd != null && mbd.isSynthetic());
  5. 看最後一段程式碼,真正的獲取Bean,直接分析程式碼比較好說,為了分析,省略了異常

    // !synthetic = true(自定義的Bean)
    object = getObjectFromFactoryBean(factory, beanName, !synthetic); 
    protected Object getObjectFromFactoryBean(FactoryBean<?> factory, String beanName, boolean shouldPostProcess) {
     if (factory.isSingleton() && containsSingleton(beanName)) {
       synchronized (getSingletonMutex()) {
         // 從快取中獲取,factoryBeanObjectCache存放<FactoryBean名稱,Object>
         Object object = this.factoryBeanObjectCache.get(beanName);
         if (object == null) {
           // 快取中獲取不到,從factory中獲取
           object = doGetObjectFromFactoryBean(factory, beanName);
        // 有可能當執行doGetObjectFromFactoryBean函式時,Bean正在建立,所以此時再獲取一下
           Object alreadyThere = this.factoryBeanObjectCache.get(beanName);
           if (alreadyThere != null) {
             object = alreadyThere;
           } else {
             if (object != null && shouldPostProcess) {
               // 後處理Bean,不再分析
               object = postProcessObjectFromFactoryBean(object, 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) {
         // 後處理
         object = postProcessObjectFromFactoryBean(object, beanName);
       }
       return object;
     }
    }
    private Object doGetObjectFromFactoryBean(final FactoryBean<?> factory, final String beanName) throws BeanCreationException {
     Object object;
     if (System.getSecurityManager() != null) {
       AccessControlContext acc = getAccessControlContext();
       object = AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {
         @Override
         public Object run() throws Exception {
           return factory.getObject();
         }
       }, acc);
     } else {
       object = factory.getObject();
     }
    
     // Do not accept a null value for a FactoryBean that's not fully initialized yet: Many FactoryBeans just return null then.
     if (object == null && isSingletonCurrentlyInCreation(beanName)) {
       throw new BeanCurrentlyInCreationException(beanName, "FactoryBean which is currently in creation returned null from getObject");
     }
     return object;
    }

    至此,整個從FactoryBean中獲取BeanDefinition完成。