1. 程式人生 > >Spring原始碼學習之路---IOC實現原理(三)

Spring原始碼學習之路---IOC實現原理(三)

原文地址:https://blog.csdn.net/zuoxiaolong8810/article/details/8548478

          上一章我們已經初步認識了BeanFactory和BeanDefinition,一個是IOC的核心工廠介面,一個是IOC的bean定義介面,上章提到說我們無法讓BeanFactory持有一個Map<String,Object>來完成bean工廠的功能,是因為spring的初始化是可以控制的,可以到用的時候才將bean例項化供開發者使用,除非我們將bean的lazy-init屬性設定為true,初始化bean工廠時採用延遲載入。

          那麼知道了上述兩個介面,我相信不少人甚至不看原始碼都已經猜到spring是如何做的了。沒錯,就是讓bean工廠持有一個Map<String,BeanDefinition>,這樣就可以在任何時候我們想用哪個bean,取到它的bean定義,我們就可以創造出一個新鮮的例項。

          介面當然不可能持有這樣一個物件,那麼這個物件一定是在BeanFactory的某個實現類或者抽象實現類當中所持有的,我經過跋山涉水,終於把它給找出來了,來看DefaultListableBeanFactory。

  1. /* 
  2.  * Copyright 2002-2010 the original author or authors. 
  3.  * 
  4.  * Licensed under the Apache License, Version 2.0 (the "License"); 
  5.  * you may not use this file except in compliance with the License.
     
  6.  * You may obtain a copy of the License at 
  7.  * 
  8.  *      http://www.apache.org/licenses/LICENSE-2.0 
  9.  * 
  10.  * Unless required by applicable law or agreed to in writing, software 
  11.  * distributed under the License is distributed on an "AS IS" BASIS, 
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     
  13.  * See the License for the specific language governing permissions and 
  14.  * limitations under the License. 
  15.  */
  16. package org.springframework.beans.factory.support;  
  17. import java.io.NotSerializableException;  
  18. import java.io.ObjectStreamException;  
  19. import java.io.Serializable;  
  20. import java.lang.annotation.Annotation;  
  21. import java.lang.ref.Reference;  
  22. import java.lang.ref.WeakReference;  
  23. import java.lang.reflect.ParameterizedType;  
  24. import java.lang.reflect.Type;  
  25. import java.security.AccessController;  
  26. import java.security.PrivilegedAction;  
  27. import java.util.ArrayList;  
  28. import java.util.Arrays;  
  29. import java.util.Collection;  
  30. import java.util.HashMap;  
  31. import java.util.LinkedHashMap;  
  32. import java.util.LinkedHashSet;  
  33. import java.util.List;  
  34. import java.util.Map;  
  35. import java.util.Set;  
  36. import java.util.concurrent.ConcurrentHashMap;  
  37. import javax.inject.Provider;  
  38. import org.springframework.beans.BeansException;  
  39. import org.springframework.beans.FatalBeanException;  
  40. import org.springframework.beans.TypeConverter;  
  41. import org.springframework.beans.factory.BeanCreationException;  
  42. import org.springframework.beans.factory.BeanCurrentlyInCreationException;  
  43. import org.springframework.beans.factory.BeanDefinitionStoreException;  
  44. import org.springframework.beans.factory.BeanFactory;  
  45. import org.springframework.beans.factory.BeanFactoryAware;  
  46. import org.springframework.beans.factory.BeanFactoryUtils;  
  47. import org.springframework.beans.factory.CannotLoadBeanClassException;  
  48. import org.springframework.beans.factory.FactoryBean;  
  49. import org.springframework.beans.factory.NoSuchBeanDefinitionException;  
  50. import org.springframework.beans.factory.ObjectFactory;  
  51. import org.springframework.beans.factory.SmartFactoryBean;  
  52. import org.springframework.beans.factory.config.BeanDefinition;  
  53. import org.springframework.beans.factory.config.BeanDefinitionHolder;  
  54. import org.springframework.beans.factory.config.ConfigurableBeanFactory;  
  55. import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;  
  56. import org.springframework.beans.factory.config.DependencyDescriptor;  
  57. import org.springframework.core.annotation.AnnotationUtils;  
  58. import org.springframework.util.Assert;  
  59. import org.springframework.util.ObjectUtils;  
  60. import org.springframework.util.StringUtils;  
  61. /** 
  62.  * Default implementation of the 
  63.  * {@link org.springframework.beans.factory.ListableBeanFactory} and 
  64.  * {@link BeanDefinitionRegistry} interfaces: a full-fledged bean factory 
  65.  * based on bean definition objects. 
  66.  * 
  67.  * <p>Typical usage is registering all bean definitions first (possibly read 
  68.  * from a bean definition file), before accessing beans. Bean definition lookup 
  69.  * is therefore an inexpensive operation in a local bean definition table, 
  70.  * operating on pre-built bean definition metadata objects. 
  71.  * 
  72.  * <p>Can be used as a standalone bean factory, or as a superclass for custom 
  73.  * bean factories. Note that readers for specific bean definition formats are 
  74.  * typically implemented separately rather than as bean factory subclasses: 
  75.  * see for example {@link PropertiesBeanDefinitionReader} and 
  76.  * {@link org.springframework.beans.factory.xml.XmlBeanDefinitionReader}. 
  77.  * 
  78.  * <p>For an alternative implementation of the 
  79.  * {@link org.springframework.beans.factory.ListableBeanFactory} interface, 
  80.  * have a look at {@link StaticListableBeanFactory}, which manages existing 
  81.  * bean instances rather than creating new ones based on bean definitions. 
  82.  * 
  83.  * @author Rod Johnson 
  84.  * @author Juergen Hoeller 
  85.  * @author Sam Brannen 
  86.  * @author Costin Leau 
  87.  * @since 16 April 2001 
  88.  * @see StaticListableBeanFactory 
  89.  * @see PropertiesBeanDefinitionReader 
  90.  * @see org.springframework.beans.factory.xml.XmlBeanDefinitionReader 
  91.  */
  92. publicclass DefaultListableBeanFactory extends AbstractAutowireCapableBeanFactory  
  93.         implements ConfigurableListableBeanFactory, BeanDefinitionRegistry, Serializable {  
  94.     privatestatic Class javaxInjectProviderClass = null;  
  95.     static {  
  96.         ClassLoader cl = DefaultListableBeanFactory.class.getClassLoader();  
  97.         try {  
  98.             javaxInjectProviderClass = cl.loadClass("javax.inject.Provider");  
  99.         }  
  100.         catch (ClassNotFoundException ex) {  
  101.             // JSR-330 API not available - Provider interface simply not supported then.
  102.         }  
  103.     }  
  104.     /** Map from serialized id to factory instance */
  105.     privatestaticfinal Map<String, Reference<DefaultListableBeanFactory>> serializableFactories =  
  106.             new ConcurrentHashMap<String, Reference<DefaultListableBeanFactory>>();  
  107.     /** Optional id for this factory, for serialization purposes */
  108.     private String serializationId;  
  109.     /** Whether to allow re-registration of a different definition with the same name */
  110.     privateboolean allowBeanDefinitionOverriding = true;  
  111.     /** Whether to allow eager class loading even for lazy-init beans */
  112.     privateboolean allowEagerClassLoading = true;  
  113.     /** Resolver to use for checking if a bean definition is an autowire candidate */
  114.     private AutowireCandidateResolver autowireCandidateResolver = new SimpleAutowireCandidateResolver();  
  115.     /** Map from dependency type to corresponding autowired value */
  116.     privatefinal Map<Class, Object> resolvableDependencies = new HashMap<Class, Object>();  
  117.     /** Map of bean definition objects, keyed by bean name */
  118.     privatefinal Map<String, BeanDefinition> beanDefinitionMap = new ConcurrentHashMap<String, BeanDefinition>();  
  119.     /** List of bean definition names, in registration order */
  120.     privatefinal List<String> beanDefinitionNames = new ArrayList<String>();  
  121.     /** Whether bean definition metadata may be cached for all beans */
  122.     privateboolean configurationFrozen = false;  
  123.     /** Cached array of bean definition names in case of frozen configuration */
  124.     private String[] frozenBeanDefinitionNames;  
  125. }  

              註明下,這裡我省略了下面N多行原始碼,原始碼太長,而且太多的話容易混亂,切勿認為此類就這麼多了。

              看它名字就知道,這是一個預設的bean工廠實現類,也就是說,如果你需要的功能非常單一,這個實現類已經足夠可以滿足你了,而以後如果你想要對spring的容器擴充套件,那麼只需要擴充套件或者持有這個物件即可。

  1. /** Map of bean definition objects, keyed by bean name */
  2.  final Map<String, BeanDefinition> beanDefinitionMap = new ConcurrentHashMap<String, BeanDefinition>();  

              看到這一行,其實已經證明了我們的猜測,即使英文不太好,也能看懂它所註釋的意思是bean定義的MAP物件,採用beanName作為key值。

              走到這裡,思路已經很明確了,bean工廠的初始化其實就是往這個Map裡填充東西。只要把我們XML檔案中定義的bean都填充到這裡,其實這個工廠就已經可以工作了。

              那麼從現在來看,我們需要什麼才能把Map填充呢?也就是初始化bean工廠呢,或者說建立IOC容器。我首先列出來以下幾步。

             1.需要一個File指向我們的XML檔案(本文的配置檔案都已XML為例,因為這是我們最熟悉的),專業點可以叫資源定位,簡單點可以說我們需要一些工具來完成找到XML檔案的所在位置。

             2.需要一個Reader來讀取我們的XML檔案,專業點叫DOM解析,簡單點說,就是把XML檔案的各種定義都給拿出來。

             3.需要將讀出來的資料都設定到Map當中。

             這三部總結起來就是定位、解析、註冊。我們首先按照這個思路來試一下