IOC容器原理

来源:互联网 发布:员工管理系统c语言 编辑:程序博客网 时间:2024/06/07 23:14

IOC介绍

  相信大多数人在学习spring时 IOC 和 Bean 算得上是最常听到的两个名词,IOC在学习Spring当中出现频率如此之高必然有其原因。如果我们做一个比喻的话,把Bean说成Spring中主角的话,那么IOC便是这个主角进行演出的舞台,没有IOC作为Bean的承载,那么Bean便不会在编程中大放异彩。作为Spring核心组件的重要一员,了解其内部实现对我们编程和窥探Spring内幕是相当有帮助的,下面一步步从源码的角度来剖析IOC究竟是怎样实现的。

接口设计

  首先我们先通过一张接口设计图了解下,IOC容器整体的设计架构 
IOC整体架构

源码剖析

BeanFacory

  通过上面的接口设计图我们可以看到,在Spring当中最基本的IOC容器接口是BeanFactory,这个接口当中定义作为一个IOC容器最基本的一些操作和功能,下来看看它的源码:

package org.springframework.beans.factory;import org.springframework.beans.BeansException;import org.springframework.core.ResolvableType;/** * BeanFactory作为最原始同时也最重要的Ioc容器,它主要的功能是为依赖注入 (DI) 提供支持,BeanFactory 和相关的接口.这里定义的只是一系列的接口方法,通过这一系列的BeanFactory接口,可以使用不同的Bean的检索方法很方便地从Ioc容器中得到需要的Bean,从而忽略具体的Ioc容器的实现,从这个角度上看,这些检索方法代表的是最为基本的容器入口。 * * @author Rod Johnson * @author Juergen Hoeller * @author Chris Beams * @since 13 April 2001 */public interface BeanFactory {    /**     * 转定义符"&" 用来引用实例,或把它和工厂产生的Bean区分开,就是说,如果一个FactoryBean的名字为a,那么,&a会得到那个Factory     *     * FactoryBean和BeanFactory 是在Spring中使用最为频繁的类,它们在拼写上很相似。一个是Factory,也就是Ioc容器或对象工厂;一个     * 是Bean。在Spring中,所有的Bean都是由BeanFactory(也就是Ioc容器)来进行管理的。但对FactoryBean而言,这个Bean不是简单的Be     * an,而是一个能产生或者修饰对象生成的工厂Bean,它的实现与设计模式中的工厂模式和修饰器模式类似。     */    String FACTORY_BEAN_PREFIX = "&";    /**     * 五个不同形式的getBean方法,获取实例     * @param name 检索所用的Bean名     * @return Object(<T> T) 实例对象     * @throws BeansException 如果Bean不能取得     */    Object getBean(String name) throws BeansException;    <T> T getBean(String name, Class<T> requiredType) throws BeansException;    <T> T getBean(Class<T> requiredType) throws BeansException;    Object getBean(String name, Object... args) throws BeansException;    <T> T getBean(Class<T> requiredType, Object... args) throws BeansException;    /**     * 让用户判断容器是否含有指定名字的Bean.     * @param name 搜索所用的Bean名     * @return boolean 是否包含其中     */    boolean containsBean(String name);    /**     * 查询指定名字的Bean是否是Singleton类型的Bean.     * 对于Singleton属性,可以在BeanDefinition指定.     * @param name 搜索所用的Bean名     * @return boolean 是否包是Singleton     * @throws NoSuchBeanDefinitionException 没有找到Bean     */    boolean isSingleton(String name) throws NoSuchBeanDefinitionException;    /**     * 查询指定名字的Bean是否是Prototype类型的。     * 与Singleton属性一样,可以在BeanDefinition指定.     * @param name 搜索所用的Bean名     * @return boolean 是否包是Prototype     * @throws NoSuchBeanDefinitionException 没有找到Bean     */    boolean isPrototype(String name) throws NoSuchBeanDefinitionException;    /**     * 查询指定了名字的Bean的Class类型是否是特定的Class类型.     * @param name 搜索所用的Bean名     * @param typeToMatch 匹配类型     * @return boolean 是否是特定类型     * @throws NoSuchBeanDefinitionException 没有找到Bean     */    boolean isTypeMatch(String name, ResolvableType typeToMatch) throws NoSuchBeanDefinitionException;    boolean isTypeMatch(String name, Class<?> typeToMatch) throws NoSuchBeanDefinitionException;    /**     * 查询指定名字的Bean的Class类型.     * @param name 搜索所用的Bean名     * @return 指定的Bean或者null(没有找到合适的Bean)     * @throws NoSuchBeanDefinitionException 没有找到Bean     */    Class<?> getType(String name) throws NoSuchBeanDefinitionException;    /**     * 查询指定了名字的Bean的所有别名,这些都是在BeanDefinition中定义的     * @param name 搜索所用的Bean名     * @return 指定名字的Bean的所有别名 或者一个空的数组     */    String[] getAliases(String name);}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88

  通过上面的接口说明我们看到在BeanFactory里面只对IOC容器最基本的行为做了定义,而不关心Bean是怎样定义和加载的。如果我们想要知道一个工厂具体生产对象的过程,就需要去看这个接口的实现类,在Spring当中实现这个接口的有很多子类,下面我们来看看其一个常用的实现类XmlBeanFacotry的代码吧。

XmlBeanFactory

package org.springframework.beans.factory.xml;import org.springframework.beans.BeansException;import org.springframework.beans.factory.BeanFactory;import org.springframework.beans.factory.support.DefaultListableBeanFactory;import org.springframework.core.io.Resource;/** * XmlBeanFactory是BeanFactory的最简单实现类 *  * XmlBeanFactory的功能是建立在DefaultListableBeanFactory这个基本容器的基础上的,并在这个基本容器的基础上实行了其他诸如 * XML读取的附加功能。XmlBeanFactory使用了DefaultListableBeanFactory作为基础类,DefaultListableBeanFactory是一个很重 * 要的Ioc实现,会在下一章进行重点论述。 * * @author Rod Johnson * @author Juergen Hoeller * @author Chris Beams * @since 15 April 2001 */public class XmlBeanFactory extends DefaultListableBeanFactory {    private final XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(this);    /**     * 根据给定来源,创建一个XmlBeanFactory     * @param resource  Spring中对与外部资源的抽象,最常见的是对文件的抽象,特别是XML文件。而且Resource里面通常     * 是保存了Spring使用者的Bean定义,比如applicationContext.xml在被加载时,就会被抽象为Resource来处理。     * @throws BeansException 载入或者解析中发生错误     */    public XmlBeanFactory(Resource resource) throws BeansException {        this(resource, null);    }    /**     * 根据给定来源和BeanFactory,创建一个XmlBeanFactory     * @param resource  Spring中对与外部资源的抽象,最常见的是对文件的抽象,特别是XML文件。而且Resource里面通常     * 是保存了Spring使用者的Bean定义,比如applicationContext.xml在被加载时,就会被抽象为Resource来处理。     * @param parentBeanFactory 父类的BeanFactory     * @throws BeansException 载入或者解析中发生错误     */    public XmlBeanFactory(Resource resource, BeanFactory parentBeanFactory) throws BeansException {        super(parentBeanFactory);        this.reader.loadBeanDefinitions(resource);    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47

  我们可以看到要构造XmlBeanFactory对象的话需要一个Resource对象,这个Resource是什么呢?

简单的讲Resource接口是为了提供更强的访问底层资源能力的抽象,它是spring访问资源最基本的接口。

  通过XmlBeanFactory我们也可以大致可以猜到,对于该类的要传的Resource对象便是一个xml的文件流。那么,这下我们知道这个BeanFactory的简单实现类的用法,也知道了构造这个对象的的参数,先动手实践创建一个最原始的Ioc容器吧。

利用XmlBeanFactory实现最原始Ioc容器

准备工作

1、实体类User.Java

public class User{    private String name;    private int age;    getter();    setter();}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

2、XML资源文件beans.xml

    <bean id="user1" name="user1" class="com.yanxiao.ioc.User">        <property name="name" value="yanxiao"></property>        <property name="age" value="20"></property>    </bean>
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

最原始IOC容器的使用

package com.yanxiao.ioc;import org.springframework.beans.factory.BeanFactory;import org.springframework.beans.factory.xml.XmlBeanFactory;import org.springframework.core.io.ClassPathResource;/** * Created by yanxiao on 2016/7/28. */@SuppressWarnings("deprecation")public class MySimpleBeanFactory {    public static void main(String[] args) {        ClassPathResource resource = new ClassPathResource("META-INF/beans.xml");        BeanFactory beanFactory = new XmlBeanFactory(resource);        User user = beanFactory.getBean("user1", User.class);        System.out.println(user.getName()+" : "+user.getAge());    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

跟踪上面的代码流程

  使用单步调试了解上面代码的主要内部调用流程,这里没有跟踪getBean()方法的过程,该步骤后设计Bean的解析过程,后面我们再说这个

调用顺序类名方法名1XmlBeanFactory构造方法(Resource)2DefaultListableBeanFactory构造方法(BeanFactory)3AbstractAutowireCapableBeanFactory构造方法(BeanFactory)4AbstractAutowireCapableBeanFactorysetParentFactory(BeanFactory)5XmlBeanDefinitionReaderloadBeanDefinitions(Resource)6AbstractBeanDefinitionReadergetResourceLoader()

通过上面的流程,我们可以简单的将使用IOC容器的步骤概括如下:

    1.创建Ioc配置文件的抽象资源,这个抽象资源包含了BeanDefinition的定义信息    2.创建一个BeanFactory,这里使用了DefaultListableBeanFactory    3.创建一个载入BeanDefinition的读取器,这里使用XmlBeanDefinitionReader来载入XML文件形式的BeanDefinition    4.然后将上面定位好的Resource,通过一个回调配置给BeanFactory    5.从定位好的资源位置读入配置信息,具体的解析过程由XmlBeanDefinitionReader完成    6.完成整个载入和注册Bean定义之后,需要的Ioc容器就初步建立起来了

  我们对这个内部流程有了了解了以后,下面可以在实践当中进行应用,我们之前写的MySimpleBeanFactory代码便可以更改为下面的形式:

public static void main(String[] args) {        ClassPathResource resource = new ClassPathResource("META-INF/beans.xml");        DefaultListableBeanFactory factory = new DefaultListableBeanFactory();        XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(factory);        reader.loadBeanDefinitions(resource);        User user = factory.getBean("user", User.class);        System.out.println(user.getName()+" : "+user.getAge());    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

  两者的作用是等效的。

  我们现在对IOC大体上有了初步了解后,下面对一些关键点进行细节上的一些分析。首先,从我们第一步创建的ClassPathResource对象说起,重新回过头来认识Spring当中的Resource。

Resource接口体系

  在上面的文章我们也简单提到过 Resource 接口的作用,其实Spring使用自己的抽象结构对整个框架中所用到资源进行统一的封装,封装这个接口的原因其实大家也大致可以想到,我们在编程当中所遇到的资源文件类型多种多样有File、InputStream、UrlResource、ContextResource等等,面对如此多的资源类型框架内部要是不设计出一套成熟合理的资源封装体系的话,无疑对框架的实现和开发者的编程来说造成很多的复杂性。面向对象编程当中的封装和继承特性,在Spring当中可谓应用的淋漓尽致,有时间自己可以花点功夫体会Spring框架这一套设计体系对我们的以后编写可扩展和可用代码有很大的好处。 
  继续展开上面Resource接口的介绍,该接口作为资源的一个原始封装,它继承自InputStreamResource接口,InputStreamResource只有InputStream getInputStream()这样一个方法,通过这次继承赋予了通过Resource对象获得InputStram流的功能。下面看看接口的源码:

InputStreamResource接口

package org.springframework.core.io;import java.io.IOException;import java.io.InputStream;/** * @author Juergen Hoeller * @since 20.01.2004 */public interface InputStreamSource {    /**     * 返回InputStream的类,比如File、Classpath下的资源和Byte Array等     * @return InputStream 返回一个新的InputStream的对象     * @throws IOException     */    InputStream getInputStream() throws IOException;}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

Resource接口

package org.springframework.core.io;import java.io.File;import java.io.IOException;import java.net.URI;import java.net.URL;/** * Resource接口抽象了所有Spring内部使用到的底层资源:File、URL、Classpath等。 * 同时,对于来源不同的资源文件,Resource也有不同实现:文件(FileSystemResource)、Classpath资源(ClassPathResource)、 * URL资源(UrlResource)、InputStream资源(InputStreamResource)、Byte数组(ByteArrayResource)等等。 * * @author Juergen Hoeller * @since 28.12.2003 */public interface Resource extends InputStreamSource {    /**     * 判断资源是否存在     * @return boolean 是否存在     */    boolean exists();    /**     * 判断资源是否可读     * @return boolean 是否可读     */    boolean isReadable();    /**     * 是否处于开启状态     * @return boolean 是否开启     */    boolean isOpen();    /**     * 得到URL类型资源,用于资源转换     * @return URL 得到URL类型     * @throws IOException 如果资源不能打开则抛出异常     */    URL getURL() throws IOException;    /**     * 得到URI类型资源,用于资源转换     * @return URI 得到URI类型     * @throws IOException 如果资源不能打开则抛出异常     */    URI getURI() throws IOException;    /**     * 得到File类型资源,用于资源转换     * @return File 得到File类型     * @throws IOException 如果资源不能打开则抛出异常     */    File getFile() throws IOException;    /**     * 获取资源长度     * @return long 资源长度     * @throws IOException 如果资源不能打开则抛出异常     */    long contentLength() throws IOException;    /**     * 获取lastModified属性     * @return long 获取lastModified     * @throws IOException 如果资源不能打开则抛出异常     */    long lastModified() throws IOException;    /**     * 创建一个相对的资源方法     * @param relativePath 相对路径     * @return Resource 返回一个新的资源     * @throws IOException 如果资源不能打开则抛出异常     */    Resource createRelative(String relativePath) throws IOException;    /**     * 获取文件名称     * @return String 文件名称或者null     */    String getFilename();    /**     * 得到错误处理信息,主要用于错误处理的信息打印     * @return String 错误资源信息     */    String getDescription();}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90

ClassPathResource

  再看看我们上面代码当中所用到的Resource接口的实现类ClassPathResource的部分需要重点关注的源码

package org.springframework.core.io;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStream;import java.net.URL;import org.springframework.util.Assert;import org.springframework.util.ClassUtils;import org.springframework.util.ObjectUtils;import org.springframework.util.StringUtils;/** * @author Juergen Hoeller * @author Sam Brannen * @since 28.12.2003 * @see ClassLoader#getResourceAsStream(String) * @see Class#getResourceAsStream(String) */public class ClassPathResource extends AbstractFileResolvingResource {    private final String path;    private ClassLoader classLoader;    private Class<?> clazz;    /**    通过资源的路径创建一个ClassPathResource对象     */    public ClassPathResource(String path) {        this(path, (ClassLoader) null);    }    /**     上面构造方法的具体实现     */    public ClassPathResource(String path, ClassLoader classLoader) {        Assert.notNull(path, "Path must not be null");        String pathToUse = StringUtils.cleanPath(path);        //去掉路径最前面的 /         if (pathToUse.startsWith("/")) {            pathToUse = pathToUse.substring(1);        }        this.path = pathToUse;        this.classLoader = (classLoader != null ? classLoader : ClassUtils.getDefaultClassLoader());    }    /**     * Create a new {@code ClassPathResource} for {@code Class} usage.     * The path can be relative to the given class, or absolute within     * the classpath via a leading slash.     * @param path relative or absolute path within the class path     * @param clazz the class to load resources with     * @see java.lang.Class#getResourceAsStream     */    public ClassPathResource(String path, Class<?> clazz) {        Assert.notNull(path, "Path must not be null");        this.path = StringUtils.cleanPath(path);        this.clazz = clazz;    }    /**    获得InputStram的具体实现     * This implementation opens an InputStream for the given class path resource.     * @see java.lang.ClassLoader#getResourceAsStream(String)     * @see java.lang.Class#getResourceAsStream(String)     */    @Override    public InputStream getInputStream() throws IOException {        InputStream is;        if (this.clazz != null) {            is = this.clazz.getResourceAsStream(this.path);        }        else if (this.classLoader != null) {            is = this.classLoader.getResourceAsStream(this.path);        }        else {            is = ClassLoader.getSystemResourceAsStream(this.path);        }        if (is == null) {            throw new FileNotFoundException(getDescription() + " cannot be opened because it does not exist");        }        return is;    }    /**    获得文件名     * This implementation returns the name of the file that this class path     * resource refers to.     * @see org.springframework.util.StringUtils#getFilename(String)     */    @Override    public String getFilename() {        return StringUtils.getFilename(this.path);    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102

DefaultListableBeanFactory类

  我们已经把资源层面上的接口和实现了解了,下面分析我们之前代码当中的DefaultListableBeanFactory factory = new DefaultListableBeanFactory();这句。在这里我们有引入了一个新类DefaultListableBeanFactory,从IOC类图中可以看到这个是BeanFactory的一个默认实现类,其实例对象可以作为一个独立使用的IOC容器,可以认为该对象是容纳Bean对象的一个容器,我们定义好的Bean对象经过Spring的载入和加载等处理后,最终交由改工厂进行管理,我们需要得到Bean对象时便可以向它获取。

  我们调用其无参构造方法创建了factory对象,看看底层都做了哪些事情。

@SuppressWarnings("serial")public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFactory        implements ConfigurableListableBeanFactory, BeanDefinitionRegistry, Serializable {    /**     * Create a new DefaultListableBeanFactory.     */    public DefaultListableBeanFactory() {        super();//调用了父类的无参构造方法    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

接下来跟踪父类AbstractAutowireCapableBeanFactory中源码

AbstractAutowireCapableBeanFactory类

public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFactory        implements AutowireCapableBeanFactory {    /**     * Create a new AbstractAutowireCapableBeanFactory.     */    public AbstractAutowireCapableBeanFactory() {        super();//父类AbstractBeanFactory的无参构造方法,代码在下面↓↓↓        ignoreDependencyInterface(BeanNameAware.class);        ignoreDependencyInterface(BeanFactoryAware.class);        ignoreDependencyInterface(BeanClassLoaderAware.class);    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

AbstractBeanFactory类

public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport implements ConfigurableBeanFactory {    /**     * Create a new AbstractBeanFactory.     */    public AbstractBeanFactory() {        //然而这里并没有做什么    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

BeanDefinition的载入、解析和注册

  上面我们创建好了 DefaultListableBeanFactory 对象,已经有了容纳Bean的容器了,但我们通过刚才分析源码可以看到现在我们还没有对该factory进行其他实用性的操作,里面还没有任何东西。那么,我们定义在配置文件定义的Bean是如何进入Spring容器当中的,就得继续分析下面的代码了。我们先分析XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(factory);的实现。 
  从XmlBeanDefinitionReader类名中我们又见到一个新名词BeanDefinition,我们先了解下Spring当中BeanDefinition的作用。

BeanDefinition类似于Resource接口的功能,起到的作用就是对所有的Bean进行一层抽象的统一,把形式各样的对象统一封装成一个便于Spring内部进行协调管理和调度的数据结构,BeanDefinition屏蔽了不同对象对于Spring框架的差异。

  接着我们看XmlBeanDefinitionReader类中关键部分的源码

XmlBeanDefinitionReader类

public int loadBeanDefinitions(Resource resource) throws BeanDefinitionStoreException {        //对Resource参数首先做一层封装处理,主要是为了获得getReader()这个方法的实现,源码在下面↓↓↓↓        return loadBeanDefinitions(new EncodedResource(resource));    }public int loadBeanDefinitions(EncodedResource encodedResource) throws BeanDefinitionStoreException {        Assert.notNull(encodedResource, "EncodedResource must not be null");        if (logger.isInfoEnabled()) {                logger.info("Loading XML bean definitions from " + encodedResource.getResource());        }        Set<EncodedResource> currentResources = this.resourcesCurrentlyBeingLoaded.get();        if (currentResources == null) {                currentResources = new HashSet<EncodedResource>(4);                this.resourcesCurrentlyBeingLoaded.set(currentResources);        }        if (!currentResources.add(encodedResource)) {                throw new BeanDefinitionStoreException(                        "Detected cyclic loading of " + encodedResource + " - check your import definitions!");        }        // 调用DefaultResourceLoader的getResources方法完成具体的Resource定位         try {                //从EncodedResource中获取已经封装的Resource对象并再次从Resource中获取inputStream                 InputStream inputStream = encodedResource.getResource().getInputStream();                try {                        InputSource inputSource = new InputSource(inputStream);                        if (encodedResource.getEncoding() != null) {                                inputSource.setEncoding(encodedResource.getEncoding());                        }                        return doLoadBeanDefinitions(inputSource, encodedResource.getResource());    //真正的逻辑核心                }                finally {                        inputStream.close();                  }        }        catch (IOException ex) {                throw new BeanDefinitionStoreException(                                "IOException parsing XML document from " + encodedResource.getResource(), ex);        }        finally {                currentResources.remove(encodedResource);                if (currentResources.isEmpty()) {                        this.resourcesCurrentlyBeingLoaded.remove();                }        }}/** *  1.获取XML文件的验证模式,为正确加载XML做好准备 *  2.从定位好的资源位置处加载XML,对XML进行解析获得Document对象 *  3.返回的Document,在工厂中注册Bean */protected int doLoadBeanDefinitions(InputSource inputSource, Resource resource)            throws BeanDefinitionStoreException {        try {                // 对XML文件进行解析获得Document对象, 这个解析过程由DefaultDocumentLoader完成                  Document doc = doLoadDocument(inputSource, resource);                    // 启动对BeanDefinition解析的详细过程, 解析过程中会使用到Spring的Bean配置规则                return registerBeanDefinitions(doc, resource);        }        catch (BeanDefinitionStoreException ex) {                throw ex;        }        catch (SAXParseException ex) {                throw new XmlBeanDefinitionStoreException(resource.getDescription(),                                "Line " + ex.getLineNumber() + " in XML document from " + resource + " is invalid", ex);        }        catch (SAXException ex) {                throw new XmlBeanDefinitionStoreException(resource.getDescription(),                                "XML document from " + resource + " is invalid", ex);        }        catch (ParserConfigurationException ex) {                throw new BeanDefinitionStoreException(resource.getDescription(),                                "Parser configuration exception parsing XML from " + resource, ex);        }        catch (IOException ex) {                throw new BeanDefinitionStoreException(resource.getDescription(),                                "IOException parsing XML document from " + resource, ex);        }        catch (Throwable ex) {                throw new BeanDefinitionStoreException(resource.getDescription(),                                "Unexpected exception parsing XML document from " + resource, ex);        }}protected Document doLoadDocument(InputSource inputSource, Resource resource) throws Exception {        //loadDocument直接用于注册Document,getValidationModeForResource方法作用于XML的加载        return this.documentLoader.loadDocument(inputSource, getEntityResolver(), this.errorHandler,                getValidationModeForResource(resource), isNamespaceAware());}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91

EncodedResource类

//Resource的封装正是为了使用该getReader方法的功能public Reader getReader() throws IOException {        if (this.charset != null) {                return new InputStreamReader(this.resource.getInputStream(), this.charset);        }        else if (this.encoding != null) {                return new InputStreamReader(this.resource.getInputStream(), this.encoding);        }        else {                return new InputStreamReader(this.resource.getInputStream());        }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

XML的验证

上面的源码涉及到了对XML加载的过程,而对于一个XML的加载可以将其分为三步 
1.读取XML文件的验证模式,即判读其使用的是DTD还是XSD 
2.依据上面的验证模式对XML文件进行验证 
3.加载XML文件 
上面的过程在源码的体现如下:

XmlBeanDefinitionReader类

public static final int VALIDATION_AUTO = XmlValidationModeDetector.VALIDATION_AUTO;public static final int VALIDATION_XSD = XmlValidationModeDetector.VALIDATION_XSD;protected int getValidationModeForResource(Resource resource) {        int validationModeToUse = getValidationMode();        //如果手动指定了验证模式则使用指定的验证模式        if (validationModeToUse != VALIDATION_AUTO) {                return validationModeToUse;        }        //如果没有指定,则交由Spring进行判断        int detectedMode = detectValidationMode(resource);         if (detectedMode != VALIDATION_AUTO) {                return detectedMode;        }        return VALIDATION_XSD;}protected int detectValidationMode(Resource resource) {        if (resource.isOpen()) {                throw new BeanDefinitionStoreException(                        "Passed-in Resource [" + resource + "] contains an open stream: " +                        "cannot determine validation mode automatically. Either pass in a Resource " +                        "that is able to create fresh streams, or explicitly specify the validationMode " +                        "on your XmlBeanDefinitionReader instance.");        }        InputStream inputStream;        try {                inputStream = resource.getInputStream();        }        catch (IOException ex) {                throw new BeanDefinitionStoreException(                        "Unable to determine validation mode for [" + resource + "]: cannot open InputStream. " +                        "Did you attempt to load directly from a SAX InputSource without specifying the " +                        "validationMode on your XmlBeanDefinitionReader instance?", ex);        }        try {                return this.validationModeDetector.detectValidationMode(inputStream);    //自动检测在detectValidationMode完成        }        catch (IOException ex) {                throw new BeanDefinitionStoreException("Unable to determine validation mode for [" +                        resource + "]: an error occurred whilst reading from the InputStream.", ex);        }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46

XmlValidationModeDetector类

//对XML的验证模式进行枚举public static final int VALIDATION_NONE = 0;public static final int VALIDATION_AUTO = 1;public static final int VALIDATION_DTD = 2;public static final int VALIDATION_XSD = 3;private static final String DOCTYPE = "DOCTYPE";//XML验证模式的自动检测实现方法public int detectValidationMode(InputStream inputStream) throws IOException {        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));        try {                boolean isDtdValidated = false;                String content;                while ((content = reader.readLine()) != null) {                        content = consumeCommentTokens(content);                        if (this.inComment || !StringUtils.hasText(content)) {                                continue;                        }                        if (hasDoctype(content)) {                                isDtdValidated = true;                                break;                        }                        if (hasOpeningTag(content)) {                                break;                        }                }                return (isDtdValidated ? VALIDATION_DTD : VALIDATION_XSD);        }        catch (CharConversionException ex) {                return VALIDATION_AUTO;        }        finally {                reader.close();        }}private boolean hasDoctype(String content) {        return content.contains(DOCTYPE);}private boolean hasOpeningTag(String content) {        if (this.inComment) {                return false;        }        int openTagIndex = content.indexOf('<');        return (openTagIndex > -1 && (content.length() > openTagIndex + 1) &&                Character.isLetter(content.charAt(openTagIndex + 1)));}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55

解析XML获得Document对象

  通过以上的验证准备,就可以对XML文件进行加载了。而加载的过程XmlBeanDefinitionReader并没有自己去完成,而是由DocumentLoader接口去完成的。跟踪源码我们发现具体的实现是DefaultDocumentLoader.loadDocument(),源码体现如下

XmlBeanDefinitionReader类

//自己并没有实现loadDocment的过程,而是调用documentLoader成员的loadDocumentprotected Document doLoadDocument(InputSource inputSource, Resource resource) throws Exception {        //documentLoader的类型为DefaultDocumentLoader        return this.documentLoader.loadDocument(inputSource, getEntityResolver(), this.errorHandler,                getValidationModeForResource(resource), isNamespaceAware());    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

接着看DefaultDocumentLoader中的方法实现

DefaultDocumentLoader类

public Document loadDocument(InputSource inputSource, EntityResolver entityResolver,                ErrorHandler errorHandler, int validationMode, boolean namespaceAware) throws Exception {        DocumentBuilderFactory factory = createDocumentBuilderFactory(validationMode, namespaceAware);        if (logger.isDebugEnabled()) {                logger.debug("Using JAXP provider [" + factory.getClass().getName() + "]");        }        DocumentBuilder builder = createDocumentBuilder(factory, entityResolver, errorHandler);        return builder.parse(inputSource);}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

  通过这个方法便可以获得XML的Document对象了。

获得BeanDefinition对象

  现在我们已经获得XML解析后的Document对象,接下来只要在对该Document结构进行分析便可以知道Bean在XML中是如何定义的,也就能将其转换为BeanDefinition对象。 
  这个过程调用的是XmlBeanDefinitionReader类的registerBeanDefinitions(Document doc, Resource resource)方法,代码如下:

XmlBeanDefinitionReader类

public int registerBeanDefinitions(Document doc, Resource resource) throws BeanDefinitionStoreException {        BeanDefinitionDocumentReader documentReader = createBeanDefinitionDocumentReader();        int countBefore = getRegistry().getBeanDefinitionCount();        // 具体的解析过程在BeanDefinitionDocumentReader的registerBeanDefinitions方法中完成        documentReader.registerBeanDefinitions(doc, createReaderContext(resource));        return getRegistry().getBeanDefinitionCount() - countBefore;    }protected BeanDefinitionDocumentReader createBeanDefinitionDocumentReader() {        return BeanDefinitionDocumentReader.class.cast(BeanUtils.instantiateClass(this.documentReaderClass));    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

DefaultBeanDefinitionDocumentReader类

public static final String BEAN_ELEMENT = BeanDefinitionParserDelegate.BEAN_ELEMENT;public static final String NESTED_BEANS_ELEMENT = "beans";public static final String ALIAS_ELEMENT = "alias";public static final String ALIAS_ATTRIBUTE = "alias";public void registerBeanDefinitions(Document doc, XmlReaderContext readerContext) {        this.readerContext = readerContext;        logger.debug("Loading bean definitions");        Element root = doc.getDocumentElement();    // 获得Document的根元素        doRegisterBeanDefinitions(root);}protected void doRegisterBeanDefinitions(Element root) {        BeanDefinitionParserDelegate parent = this.delegate;        this.delegate = createDelegate(getReaderContext(), root, parent);        if (this.delegate.isDefaultNamespace(root)) {                String profileSpec = root.getAttribute(PROFILE_ATTRIBUTE);                if (StringUtils.hasText(profileSpec)) {                        String[] specifiedProfiles = StringUtils.tokenizeToStringArray(                                profileSpec, BeanDefinitionParserDelegate.MULTI_VALUE_ATTRIBUTE_DELIMITERS);                        if (!getReaderContext().getEnvironment().acceptsProfiles(specifiedProfiles)) {                                return;                        }                }        }        preProcessXml(root);        //这里我们看到XmlBeanDefinitionReader对具体元素属性的解析是通过调用parseBeanDefinitions完成的        parseBeanDefinitions(root, this.delegate);        postProcessXml(root);        this.delegate = parent;}protected void parseBeanDefinitions(Element root, BeanDefinitionParserDelegate delegate) {        if (delegate.isDefaultNamespace(root)) {                NodeList nl = root.getChildNodes();                for (int i = 0; i < nl.getLength(); i++) {                        Node node = nl.item(i);                        if (node instanceof Element) {                                Element ele = (Element) node;                                if (delegate.isDefaultNamespace(ele)) {                                        parseDefaultElement(ele, delegate);                                    }                                else {                                        delegate.parseCustomElement(ele);    /                                }                        }                }        }        else {                delegate.parseCustomElement(root);    // 解析自定义元素根节点          }}private void parseDefaultElement(Element ele, BeanDefinitionParserDelegate delegate) {        if (delegate.nodeNameEquals(ele, IMPORT_ELEMENT)) {    // 解析import元素                importBeanDefinitionResource(ele);        }        else if (delegate.nodeNameEquals(ele, ALIAS_ELEMENT)) {    // 解析alias元素                processAliasRegistration(ele);        }        else if (delegate.nodeNameEquals(ele, BEAN_ELEMENT)) {    // 解析bean元素                processBeanDefinition(ele, delegate);        }        else if (delegate.nodeNameEquals(ele, NESTED_BEANS_ELEMENT)) {    // 解析内嵌beans元素, 作为根节点递归解析                doRegisterBeanDefinitions(ele);        }}protected void processBeanDefinition(Element ele, BeanDefinitionParserDelegate delegate) {        // 具体的解析委托给BeanDefinitionParserDelegate来完成          // BeanDefinitionHolder是BeanDefinition的封装类, 封装了BeanDefinition、Bean的名字和别名, 用它来完成向IoC容器注册.        BeanDefinitionHolder bdHolder = delegate.parseBeanDefinitionElement(ele);        if (bdHolder != null) {                bdHolder = delegate.decorateBeanDefinitionIfRequired(ele, bdHolder);                try {                        BeanDefinitionReaderUtils.registerBeanDefinition(bdHolder, getReaderContext().getRegistry());                }                catch (BeanDefinitionStoreException ex) {                        getReaderContext().error("Failed to register bean definition with name '" +                                bdHolder.getBeanName() + "'", ele, ex);                }                getReaderContext().fireComponentRegistered(new BeanComponentDefinition(bdHolder));        }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
看了上面的代码发现其实最后解析的重任交给了processBeanDefinition这个方法,而这个方法里面的实现过程在BeanDefinitionParserDelegate这个类当中。

BeanDefinitionParserDelegate类

public class BeanDefinitionParserDelegate {    public static final String BEANS_NAMESPACE_URI = "http://www.springframework.org/schema/beans";    public static final String MULTI_VALUE_ATTRIBUTE_DELIMITERS = ",; ";    /**     * Value of a T/F attribute that represents true.     * Anything else represents false. Case seNsItive.     */    public static final String TRUE_VALUE = "true";    public static final String FALSE_VALUE = "false";    public static final String DEFAULT_VALUE = "default";    public static final String DESCRIPTION_ELEMENT = "description";    public static final String AUTOWIRE_NO_VALUE = "no";    public static final String AUTOWIRE_BY_NAME_VALUE = "byName";    public static final String AUTOWIRE_BY_TYPE_VALUE = "byType";    public static final String AUTOWIRE_CONSTRUCTOR_VALUE = "constructor";    public static final String AUTOWIRE_AUTODETECT_VALUE = "autodetect";    public static final String DEPENDENCY_CHECK_ALL_ATTRIBUTE_VALUE = "all";    public static final String DEPENDENCY_CHECK_SIMPLE_ATTRIBUTE_VALUE = "simple";    public static final String DEPENDENCY_CHECK_OBJECTS_ATTRIBUTE_VALUE = "objects";    public static final String NAME_ATTRIBUTE = "name";    public BeanDefinitionHolder parseBeanDefinitionElement(Element ele) {        return parseBeanDefinitionElement(ele, null);}public BeanDefinitionHolder parseBeanDefinitionElement(Element ele, BeanDefinition containingBean) {        // 解析Bean定义资源文件中的<Bean>元素,主要处理<Bean>元素的id,name和aliase属性        String id = ele.getAttribute(ID_ATTRIBUTE);        String nameAttr = ele.getAttribute(NAME_ATTRIBUTE);        List<String> aliases = new ArrayList<String>();        if (StringUtils.hasLength(nameAttr)) {                String[] nameArr = StringUtils.tokenizeToStringArray(nameAttr, MULTI_VALUE_ATTRIBUTE_DELIMITERS);                aliases.addAll(Arrays.asList(nameArr));        }        String beanName = id;        if (!StringUtils.hasText(beanName) && !aliases.isEmpty()) {                beanName = aliases.remove(0);                if (logger.isDebugEnabled()) {                        logger.debug("No XML 'id' specified - using '" + beanName +                                "' as bean name and " + aliases + " as aliases");                }        }        if (containingBean == null) {                checkNameUniqueness(beanName, aliases, ele);        }        // 对<bean>元素进行详细解析        AbstractBeanDefinition beanDefinition = parseBeanDefinitionElement(ele, beanName, containingBean);        if (beanDefinition != null) {                if (!StringUtils.hasText(beanName)) {                        try {                                if (containingBean != null) {                                        beanName = BeanDefinitionReaderUtils.generateBeanName(                                        beanDefinition, this.readerContext.getRegistry(), true);                                }                                else {                                        beanName = this.readerContext.generateBeanName(beanDefinition);                                        String beanClassName = beanDefinition.getBeanClassName();                                        if (beanClassName != null &&                                                beanName.startsWith(beanClassName) && beanName.length() > beanClassName.length() &&                                                !this.readerContext.getRegistry().isBeanNameInUse(beanClassName)) {                                                        aliases.add(beanClassName);                                        }                                }                                if (logger.isDebugEnabled()) {                                        logger.debug("Neither XML 'id' nor 'name' specified - " +                                                "using generated bean name [" + beanName + "]");                                }                        }                        catch (Exception ex) {                                error(ex.getMessage(), ele);                                return null;                        }                }                String[] aliasesArray = StringUtils.toStringArray(aliases);                return new BeanDefinitionHolder(beanDefinition, beanName, aliasesArray);        }        return null;}public AbstractBeanDefinition parseBeanDefinitionElement(                Element ele, String beanName, BeanDefinition containingBean) {        this.parseState.push(new BeanEntry(beanName));        String className = null;        if (ele.hasAttribute(CLASS_ATTRIBUTE)) {                className = ele.getAttribute(CLASS_ATTRIBUTE).trim();        }        try {                String parent = null;                if (ele.hasAttribute(PARENT_ATTRIBUTE)) {                        parent = ele.getAttribute(PARENT_ATTRIBUTE);                }                // 根据<Bean>元素配置的class名称和parent属性值创建BeanDefinition, 为载入Bean定义信息做准备                  AbstractBeanDefinition bd = createBeanDefinition(className, parent);                parseBeanDefinitionAttributes(ele, beanName, containingBean, bd);                bd.setDescription(DomUtils.getChildElementValueByTagName(ele, DESCRIPTION_ELEMENT));                // 对<Bean>元素的meta(元数据)、lookup-method、replaced-method等子元素进行解析                  parseMetaElements(ele, bd);                parseLookupOverrideSubElements(ele, bd.getMethodOverrides());                parseReplacedMethodSubElements(ele, bd.getMethodOverrides());                parseConstructorArgElements(ele, bd);    // 解析<Bean>元素的构造方法参数                  parsePropertyElements(ele, bd);    // 解析<Bean>元素的<property>属性                 parseQualifierElements(ele, bd);                bd.setResource(this.readerContext.getResource());                bd.setSource(extractSource(ele));                return bd;        }        catch (ClassNotFoundException ex) {                error("Bean class [" + className + "] not found", ele, ex);        }        catch (NoClassDefFoundError err) {                error("Class that bean class [" + className + "] depends on not found", ele, err);        }        catch (Throwable ex) {                error("Unexpected failure during bean definition parsing", ele, ex);        }        finally {                this.parseState.pop();        }        return null;}}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154

  上面的过程就不仔细分析了,感兴趣的可以自行阅读BeanDefinitionParserDelegate的实现,主要逻辑就是根据不同的标签调用相应的处理流程对完成对BeanDefinition的解析。这步处理完成了,离我们的目标也就不远啦。

BeanDefinition的注册

  我们配置的Bean的信息经过解析在Spring内部已经转换为BeanDefinition这种统一的结构,但这些数据还不能供IoC容器直接使用,需要在IoC容器中对这些BeanDefinition数据进行注册。 
  注册过程调用的是DefaultListableBeanFactory的registerBeanDefinition方法

DefaultListableBeanFactory类

public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFactory        implements ConfigurableListableBeanFactory, BeanDefinitionRegistry, Serializable {     //注册完成的BeanDefinition,都会以beanName为key,beanDefinition为value交由改Map进行管理    /** Map of bean definition objects, keyed by bean name */    private final Map<String, BeanDefinition> beanDefinitionMap = new ConcurrentHashMap<String, BeanDefinition>(256);    public void registerBeanDefinition(String beanName, BeanDefinition beanDefinition)            throws BeanDefinitionStoreException {        Assert.hasText(beanName, "Bean name must not be empty");        Assert.notNull(beanDefinition, "BeanDefinition must not be null");        // 对解析得到的BeanDefinition校验        if (beanDefinition instanceof AbstractBeanDefinition) {                try {                        ((AbstractBeanDefinition) beanDefinition).validate();                }                catch (BeanDefinitionValidationException ex) {                        throw new BeanDefinitionStoreException(beanDefinition.getResourceDescription(), beanName,                                "Validation of bean definition failed", ex);                }        }        BeanDefinition oldBeanDefinition;        oldBeanDefinition = this.beanDefinitionMap.get(beanName);        if (oldBeanDefinition != null) {                if (!isAllowBeanDefinitionOverriding()) {                        throw new BeanDefinitionStoreException(beanDefinition.getResourceDescription(), beanName,                                "Cannot register bean definition [" + beanDefinition + "] for bean '" + beanName +                                "': There is already [" + oldBeanDefinition + "] bound.");                }                else if (oldBeanDefinition.getRole() < beanDefinition.getRole()) {                        if (this.logger.isWarnEnabled()) {                                this.logger.warn("Overriding user-defined bean definition for bean '" + beanName +                                        "' with a framework-generated bean definition: replacing [" +                                        oldBeanDefinition + "] with [" + beanDefinition + "]");                        }                }                else if (!beanDefinition.equals(oldBeanDefinition)) {                        if (this.logger.isInfoEnabled()) {                                this.logger.info("Overriding bean definition for bean '" + beanName +                                        "' with a different definition: replacing [" + oldBeanDefinition +                                        "] with [" + beanDefinition + "]");                        }                }                else {                        if (this.logger.isDebugEnabled()) {                                this.logger.debug("Overriding bean definition for bean '" + beanName +                                        "' with an equivalent definition: replacing [" + oldBeanDefinition +                                        "] with [" + beanDefinition + "]");                        }                }                this.beanDefinitionMap.put(beanName, beanDefinition);        }        else {                if (hasBeanCreationStarted()) {                        synchronized (this.beanDefinitionMap) {                                this.beanDefinitionMap.put(beanName, beanDefinition);                                List<String> updatedDefinitions = new ArrayList<String>(this.beanDefinitionNames.size() + 1);                                updatedDefinitions.addAll(this.beanDefinitionNames);                                updatedDefinitions.add(beanName);                                this.beanDefinitionNames = updatedDefinitions;                                if (this.manualSingletonNames.contains(beanName)) {                                        Set<String> updatedSingletons = new LinkedHashSet<String>(this.manualSingletonNames);                                        updatedSingletons.remove(beanName);                                        this.manualSingletonNames = updatedSingletons;                                }                        }                }                else {                        this.beanDefinitionMap.put(beanName, beanDefinition);                        this.beanDefinitionNames.add(beanName);                        this.manualSingletonNames.remove(beanName);                }                this.frozenBeanDefinitionNames = null;        }        if (oldBeanDefinition != null || containsSingleton(beanName)) {                resetBeanDefinition(beanName);        }}}
原创粉丝点击