spring源码学习之路---IOC实现原理

来源:互联网 发布:matlab 矩阵cell 编辑:程序博客网 时间:2024/06/06 05:27

上一章我们已经初步认识了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。

 

[java] view plaincopy
  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.   
  17. package org.springframework.beans.factory.support;  
  18.   
  19. import java.io.NotSerializableException;  
  20. import java.io.ObjectStreamException;  
  21. import java.io.Serializable;  
  22. import java.lang.annotation.Annotation;  
  23. import java.lang.ref.Reference;  
  24. import java.lang.ref.WeakReference;  
  25. import java.lang.reflect.ParameterizedType;  
  26. import java.lang.reflect.Type;  
  27. import java.security.AccessController;  
  28. import java.security.PrivilegedAction;  
  29. import java.util.ArrayList;  
  30. import java.util.Arrays;  
  31. import java.util.Collection;  
  32. import java.util.HashMap;  
  33. import java.util.LinkedHashMap;  
  34. import java.util.LinkedHashSet;  
  35. import java.util.List;  
  36. import java.util.Map;  
  37. import java.util.Set;  
  38. import java.util.concurrent.ConcurrentHashMap;  
  39. import javax.inject.Provider;  
  40.   
  41. import org.springframework.beans.BeansException;  
  42. import org.springframework.beans.FatalBeanException;  
  43. import org.springframework.beans.TypeConverter;  
  44. import org.springframework.beans.factory.BeanCreationException;  
  45. import org.springframework.beans.factory.BeanCurrentlyInCreationException;  
  46. import org.springframework.beans.factory.BeanDefinitionStoreException;  
  47. import org.springframework.beans.factory.BeanFactory;  
  48. import org.springframework.beans.factory.BeanFactoryAware;  
  49. import org.springframework.beans.factory.BeanFactoryUtils;  
  50. import org.springframework.beans.factory.CannotLoadBeanClassException;  
  51. import org.springframework.beans.factory.FactoryBean;  
  52. import org.springframework.beans.factory.NoSuchBeanDefinitionException;  
  53. import org.springframework.beans.factory.ObjectFactory;  
  54. import org.springframework.beans.factory.SmartFactoryBean;  
  55. import org.springframework.beans.factory.config.BeanDefinition;  
  56. import org.springframework.beans.factory.config.BeanDefinitionHolder;  
  57. import org.springframework.beans.factory.config.ConfigurableBeanFactory;  
  58. import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;  
  59. import org.springframework.beans.factory.config.DependencyDescriptor;  
  60. import org.springframework.core.annotation.AnnotationUtils;  
  61. import org.springframework.util.Assert;  
  62. import org.springframework.util.ObjectUtils;  
  63. import org.springframework.util.StringUtils;  
  64.   
  65. /** 
  66.  * Default implementation of the 
  67.  * {@link org.springframework.beans.factory.ListableBeanFactory} and 
  68.  * {@link BeanDefinitionRegistry} interfaces: a full-fledged bean factory 
  69.  * based on bean definition objects. 
  70.  * 
  71.  * <p>Typical usage is registering all bean definitions first (possibly read 
  72.  * from a bean definition file), before accessing beans. Bean definition lookup 
  73.  * is therefore an inexpensive operation in a local bean definition table, 
  74.  * operating on pre-built bean definition metadata objects. 
  75.  * 
  76.  * <p>Can be used as a standalone bean factory, or as a superclass for custom 
  77.  * bean factories. Note that readers for specific bean definition formats are 
  78.  * typically implemented separately rather than as bean factory subclasses: 
  79.  * see for example {@link PropertiesBeanDefinitionReader} and 
  80.  * {@link org.springframework.beans.factory.xml.XmlBeanDefinitionReader}. 
  81.  * 
  82.  * <p>For an alternative implementation of the 
  83.  * {@link org.springframework.beans.factory.ListableBeanFactory} interface, 
  84.  * have a look at {@link StaticListableBeanFactory}, which manages existing 
  85.  * bean instances rather than creating new ones based on bean definitions. 
  86.  * 
  87.  * @author Rod Johnson 
  88.  * @author Juergen Hoeller 
  89.  * @author Sam Brannen 
  90.  * @author Costin Leau 
  91.  * @since 16 April 2001 
  92.  * @see StaticListableBeanFactory 
  93.  * @see PropertiesBeanDefinitionReader 
  94.  * @see org.springframework.beans.factory.xml.XmlBeanDefinitionReader 
  95.  */  
  96. public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFactory  
  97.         implements ConfigurableListableBeanFactory, BeanDefinitionRegistry, Serializable {  
  98.   
  99.     private static Class javaxInjectProviderClass = null;  
  100.   
  101.     static {  
  102.         ClassLoader cl = DefaultListableBeanFactory.class.getClassLoader();  
  103.         try {  
  104.             javaxInjectProviderClass = cl.loadClass("javax.inject.Provider");  
  105.         }  
  106.         catch (ClassNotFoundException ex) {  
  107.             // JSR-330 API not available - Provider interface simply not supported then.  
  108.         }  
  109.     }  
  110.   
  111.   
  112.     /** Map from serialized id to factory instance */  
  113.     private static final Map<String, Reference<DefaultListableBeanFactory>> serializableFactories =  
  114.             new ConcurrentHashMap<String, Reference<DefaultListableBeanFactory>>();  
  115.   
  116.     /** Optional id for this factory, for serialization purposes */  
  117.     private String serializationId;  
  118.   
  119.     /** Whether to allow re-registration of a different definition with the same name */  
  120.     private boolean allowBeanDefinitionOverriding = true;  
  121.   
  122.     /** Whether to allow eager class loading even for lazy-init beans */  
  123.     private boolean allowEagerClassLoading = true;  
  124.   
  125.     /** Resolver to use for checking if a bean definition is an autowire candidate */  
  126.     private AutowireCandidateResolver autowireCandidateResolver = new SimpleAutowireCandidateResolver();  
  127.   
  128.     /** Map from dependency type to corresponding autowired value */  
  129.     private final Map<Class, Object> resolvableDependencies = new HashMap<Class, Object>();  
  130.   
  131.     /** Map of bean definition objects, keyed by bean name */  
  132.     private final Map<String, BeanDefinition> beanDefinitionMap = new ConcurrentHashMap<String, BeanDefinition>();  
  133.   
  134.     /** List of bean definition names, in registration order */  
  135.     private final List<String> beanDefinitionNames = new ArrayList<String>();  
  136.   
  137.     /** Whether bean definition metadata may be cached for all beans */  
  138.     private boolean configurationFrozen = false;  
  139.   
  140.     /** Cached array of bean definition names in case of frozen configuration */  
  141.     private String[] frozenBeanDefinitionNames;  
  142.   
  143.   
  144. }  

              注明下,这里我省略了下面N多行源码,源码太长,而且太多的话容易混乱,切勿认为此类就这么多了。

              看它名字就知道,这是一个默认的bean工厂实现类,也就是说,如果你需要的功能非常单一,这个实现类已经足够可以满足你了,而以后如果你想要对spring的容器扩展,那么只需要扩展或者持有这个对象即可。

[java] view plaincopy
  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当中。

             这三部总结起来就是定位、解析、注册。我们首先按照这个思路来试一下。

             直接上代码,我们还使用原来的Person类作为一个Bean。

[java] view plaincopy
  1. package com.springframework.beans.test;  
  2.   
  3. public class Person {  
  4.   
  5.     public void work(){  
  6.         System.out.println("I am working");  
  7.     }  
  8. }  

            我们还需要写一个简单的XML文件,beans.xml。

[html] view plaincopy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">  
  5.  <bean id="person" class="com.springframework.beans.test.Person"></bean>  
  6. </beans>  

           下面是我们根据上述的思路写一段程序,来看看会发生什么情况。

[java] view plaincopy
  1. package com.springframework.beans.test;  
  2.   
  3. import org.springframework.beans.factory.support.DefaultListableBeanFactory;  
  4. import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;  
  5. import org.springframework.core.io.ClassPathResource;  
  6.   
  7.   
  8. public class TestDefaultListableBeanFactory {  
  9.   
  10.     public static void main(String[] args) {  
  11.         ClassPathResource classPathResource = new ClassPathResource("beans.xml");  
  12.         DefaultListableBeanFactory defaultListableBeanFactory = new DefaultListableBeanFactory();  
  13.         XmlBeanDefinitionReader xmlBeanDefinitionReader = new XmlBeanDefinitionReader(defaultListableBeanFactory);  
  14.         xmlBeanDefinitionReader.loadBeanDefinitions(classPathResource);  
  15.         System.out.println("numbers: " + defaultListableBeanFactory.getBeanDefinitionCount());  
  16.         ((Person)defaultListableBeanFactory.getBean("person")).work();  
  17.     }  
  18. }  

            以下是输出结果。
 

           可以看到,结果与我们期望的是一样的,成功的解析了XML文件,并注册了一个bean定义,而且我们使用getBean方法也成功得到了Person的实例。

           上述这段程序当中可以看出,bean工厂的初始化一共使用了四行程序。

           第一行完成了我们的第一步,即资源定位,采用classpath定位,因为我的beans.xml文件是放在src下面的。

           第二行创建了一个默认的bean工厂。

           第三行创建了一个reader,从名字就不难看出,这个reader是用来读取XML文件的。这一步要多说一句,其中将我们创建的defaultListableBeanFactory作为参数传给了reader的构造函数,这里是为了第四步读取XML文件做准备。

           第四行使用reader解析XML文件,并将读取的bean定义回调设置到defaultListableBeanFactory当中。其实回调这一步就相当于我们上述的注册这一步。

           这个时候defaultListableBeanFactory已经被正确初始化了,我们已经可以使用它的一些方法了,比如上面所使用的获取bean个数,以及获得一个bean实例的方法。

           但是我相信真正的开发当中,没有人会采用这样的方式去创造一个bean工厂,我们可以有更简单的方式。上面的四步,我们肯定希望一步就可以完成它。是的,这不是在做梦,就像下面这样。

[java] view plaincopy
  1. package com.springframework.beans.test;  
  2.   
  3. import org.springframework.context.ApplicationContext;  
  4. import org.springframework.context.support.FileSystemXmlApplicationContext;  
  5.   
  6. public class TestApplicationContext {  
  7.   
  8.     public static void main(String[] args) {  
  9.         ApplicationContext applicationContext = new FileSystemXmlApplicationContext("classpath:beans.xml");  
  10.         System.out.println("numbers: " + applicationContext.getBeanDefinitionCount());  
  11.         ((Person)applicationContext.getBean("person")).work();  
  12.     }  
  13. }  

          接下来就是见证奇迹的时刻了,输出结果如下图。        

          我们果然一步就完成了上面四步所做的事情。而且仔细看会发现日志信息当中,第二次采用FileSystemXmlApplicationContext时,日志信息多了许多,分别在上面的前面多了两行,后面多了两行,这说明别看我们是一步,但其实这里比上面做了更多的事情。

          具体我们在new一个FileSystemXmlApplicationContext对象的时候,spring到底做了哪些事情呢,这个自然要去跟随源码去看个究竟。

0 0
原创粉丝点击