Spring源码:IOC原理解析(一)

版权声明:本文为博主原创文章,转载请注明出处,欢迎交流学习!java

       IOC(Inversion of Control),即控制反转,意思是将对象的建立和依赖关系交给第三方容器处理,咱们要用的时候告诉容器咱们须要什么而后直接去拿就好了。举个例子,咱们有一个工厂,它生产各类产品,当你须要某个产品,好比你须要一辆汽车,你就告诉工厂你须要一辆汽车,工厂就会直接返回给你一辆汽车,而不须要你本身经过付出劳动来获得这辆汽车,你也不用关心工厂是如何生产这辆汽车。对应到咱们的程序中就是,IOC容器会帮咱们建立和管理对象,当你告诉容器你须要某个对象时,容器会把这个对象返回给你,而不须要本身去new出一个对象来,对象的建立和管理会由容器自动进行,直接从容器中拿来用就能够了。IOC能够说是Spring最核心的思想,它使咱们的开发变得简单(对象之间的依赖关系能够经过配置文件或者注解来创建),对于这种优秀的设计思想,咱们固然有必要研究一下它的底层实现原理。spring

       首先咱们来关注一个接口,源码以下:     app

package org.springframework.beans.factory;

import org.springframework.beans.BeansException;
import org.springframework.core.ResolvableType;
import org.springframework.lang.Nullable;

/**
 * 
 * @author Rod Johnson
 * @author Juergen Hoeller
 * @author Chris Beams
 * @since 13 April 2001
 * 
 */
public interface BeanFactory {

    
    String FACTORY_BEAN_PREFIX = "&";


    
    Object getBean(String name) throws BeansException;

    
    <T> T getBean(String name, @Nullable Class<T> requiredType) throws BeansException;

    
    Object getBean(String name, Object... args) throws BeansException;

    
    <T> T getBean(Class<T> requiredType) throws BeansException;

    
    <T> T getBean(Class<T> requiredType, Object... args) throws BeansException;


    
    boolean containsBean(String name);

    
    boolean isSingleton(String name) throws NoSuchBeanDefinitionException;

    
    boolean isPrototype(String name) throws NoSuchBeanDefinitionException;

    
    boolean isTypeMatch(String name, ResolvableType typeToMatch) throws NoSuchBeanDefinitionException;

    
    boolean isTypeMatch(String name, @Nullable Class<?> typeToMatch) throws NoSuchBeanDefinitionException;

    
    @Nullable
    Class<?> getType(String name) throws NoSuchBeanDefinitionException;

    
    String[] getAliases(String name);

}

     

       这个接口即是spring核心的bean工厂定义,它是IOC容器的顶层接口,spring中全部bean工厂都直接或间接的继承或实现了这个接口。咱们平时使用的最多的ApplicationContext接口也继承了BeanFactory接口,所以它具备BeanFactory接口的全部功能,这里顺便提一下,从BeanFactory获取bean时,实例化BeanFactory容器并不会实例化所配置的bean,只有当使用某个bean(getBean)时,才会实时的实例化该bean;从ApplicationContext获取bean时,实例化ApplicationContext容器时会一并实例化容器中的全部的bean。ide

       从BeanFactory的源码能够看出,它实现的核心功能就是根据名称或类型来返回一个bean实例。一个工厂若是要具有这种功能,结合工厂模式的思想,咱们能够试想一下它须要具有如下几个条件:函数

       一、持有各类bean的定义,只有拿到了bean的定义信息,才能根据这些信息进行实例化;工具

       二、持有各类bean之间的依赖关系,若是一个类中持有对另外一个类的引用,那么在对该类进行实例化时,必须根据类之间的依赖关系对相关类也进行实例化,所以,工厂必须得到类之间的依赖关系,不然没法正确实例化;学习

       三、以上两种信息都依赖于咱们的配置信息定义,好比xml配置文件,工厂须要一个工具来读取配置文件的信息。测试

       以上是咱们设想IOC的实现思路,只要知足以上三个条件,就能构造一个工厂,生产各类bean。可是咱们仍是有一些疑问,好比在第一个条件中,咱们如何持有bean的定义呢?咱们先来看另一个接口:        ui

package org.springframework.beans.factory.config;

import org.springframework.beans.BeanMetadataElement;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.core.AttributeAccessor;
import org.springframework.lang.Nullable;

/**
 * 一个BeanDefinition描述一个bean实例具备的属性值,构造函数参数值,以及具体实现的进一步信息。
 * 
 * A BeanDefinition describes a bean instance, which has property values,
 * constructor argument values, and further information supplied by
 * concrete implementations.
 *
 * @author Juergen Hoeller
 * @author Rob Harrop
 * @since 19.03.2004
 */
public interface BeanDefinition extends AttributeAccessor, BeanMetadataElement {

    
    String SCOPE_SINGLETON = ConfigurableBeanFactory.SCOPE_SINGLETON;

    
    String SCOPE_PROTOTYPE = ConfigurableBeanFactory.SCOPE_PROTOTYPE;


    int ROLE_APPLICATION = 0;

    
    int ROLE_SUPPORT = 1;

    
    int ROLE_INFRASTRUCTURE = 2;


    void setParentName(@Nullable String parentName);

    
    @Nullable
    String getParentName();

    
    void setBeanClassName(@Nullable String beanClassName);

    
    @Nullable
    String getBeanClassName();

    
    void setScope(@Nullable String scope);

    
    @Nullable
    String getScope();

    
    void setLazyInit(boolean lazyInit);

    
    boolean isLazyInit();

    /**
     * Set the names of the beans that this bean depends on being initialized.
     * The bean factory will guarantee that these beans get initialized first.
     */
    void setDependsOn(String... dependsOn);

    /**
     * Return the bean names that this bean depends on.
     */
    @Nullable
    String[] getDependsOn();

    
    void setAutowireCandidate(boolean autowireCandidate);

    
    boolean isAutowireCandidate();

    
    void setPrimary(boolean primary);

    
    boolean isPrimary();

    
    void setFactoryBeanName(@Nullable String factoryBeanName);

    
    @Nullable
    String getFactoryBeanName();

    
    void setFactoryMethodName(@Nullable String factoryMethodName);

    
    @Nullable
    String getFactoryMethodName();

    
    ConstructorArgumentValues getConstructorArgumentValues();

    
    MutablePropertyValues getPropertyValues();


    boolean isSingleton();

    
    boolean isPrototype();

    
    boolean isAbstract();

    
    int getRole();

    
    @Nullable
    String getDescription();

    
    @Nullable
    String getResourceDescription();

    
    @Nullable
    BeanDefinition getOriginatingBeanDefinition();

}

       

       BeanDefinition,顾名思义即是spring中的bean定义接口,spring的工厂里持有的就是此接口定义的内容。从源码能够看出,这个接口继承了两个另外两个接口,一个是AttributeAccessor接口,继承这个接口就意味着BeanDefinition接口拥有了处理属性的能力,另一个接口是BeanMetedataElement,它能够得到bean的配置定义的元素,对于xml文件来讲就是会持有bean的标签。从源码中咱们能够看出,BeanDefinition接口定义了两个方法,分别是void setDependsOn(String... dependsOn)和String[] getDependsOn(),从方法的说明能够看出,这两个方法就是设置依赖的bean的名称和获取依赖的bean的名称,这就意味着只要咱们有一个BeanDefinition,就能获得获得bean的定义信息和bean之间的依赖关系,从而能够生产一个完整的bean实例。this

       从上面两个接口,咱们大体能够猜出spring是如何持有bean的定义信息及依赖关系了,没错,就是让bean工厂持有一个Map<String,BeanDefinition>,String型的beanName做为key,BeanDefinition型的bean定义做为value,这样就能生产一个bean实例。BeanFactory接口固然不能持有这个map对象,那么必定是在它的某个实现类里所持有的,咱们找到了这个实现类,来看看源码:

       

package org.springframework.beans.factory.support;

import java.io.IOException;
import java.io.NotSerializableException;
import java.io.ObjectInputStream;
import java.io.ObjectStreamException;
import java.io.Serializable;
import java.lang.annotation.Annotation;
import java.lang.ref.Reference;
import java.lang.ref.WeakReference;
import java.lang.reflect.Method;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.IdentityHashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import javax.inject.Provider;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.TypeConverter;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.BeanCurrentlyInCreationException;
import org.springframework.beans.factory.BeanDefinitionStoreException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanFactoryUtils;
import org.springframework.beans.factory.BeanNotOfRequiredTypeException;
import org.springframework.beans.factory.CannotLoadBeanClassException;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InjectionPoint;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.NoUniqueBeanDefinitionException;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.SmartFactoryBean;
import org.springframework.beans.factory.SmartInitializingSingleton;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanDefinitionHolder;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.DependencyDescriptor;
import org.springframework.beans.factory.config.NamedBeanHolder;
import org.springframework.core.OrderComparator;
import org.springframework.core.ResolvableType;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.CompositeIterator;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

/**
 * 基于bean definition对象的完整bean工厂
 * 
 * Default implementation of the
 * {@link org.springframework.beans.factory.ListableBeanFactory} and
 * {@link BeanDefinitionRegistry} interfaces: a full-fledged bean factory
 * based on bean definition objects.
 *
 * @author Rod Johnson
 * @author Juergen Hoeller
 * @author Sam Brannen
 * @author Costin Leau
 * @author Chris Beams
 * @author Phillip Webb
 * @author Stephane Nicoll
 * @since 16 April 2001
 * @see StaticListableBeanFactory
 * @see PropertiesBeanDefinitionReader
 * @see org.springframework.beans.factory.xml.XmlBeanDefinitionReader
 */
@SuppressWarnings("serial")
public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFactory
        implements ConfigurableListableBeanFactory, BeanDefinitionRegistry, Serializable {

    @Nullable
    private static Class<?> javaxInjectProviderClass;

    static {
        try {
            javaxInjectProviderClass =
                    ClassUtils.forName("javax.inject.Provider", DefaultListableBeanFactory.class.getClassLoader());
        }
        catch (ClassNotFoundException ex) {
            // JSR-330 API not available - Provider interface simply not supported then.
            javaxInjectProviderClass = null;
        }
    }


    /** Map from serialized id to factory instance */
    private static final Map<String, Reference<DefaultListableBeanFactory>> serializableFactories =
            new ConcurrentHashMap<>(8);

    /** Optional id for this factory, for serialization purposes */
    @Nullable
    private String serializationId;

    /** Whether to allow re-registration of a different definition with the same name */
    private boolean allowBeanDefinitionOverriding = true;

    /** Whether to allow eager class loading even for lazy-init beans */
    private boolean allowEagerClassLoading = true;

    /** Optional OrderComparator for dependency Lists and arrays */
    @Nullable
    private Comparator<Object> dependencyComparator;

    /** Resolver to use for checking if a bean definition is an autowire candidate */
    private AutowireCandidateResolver autowireCandidateResolver = new SimpleAutowireCandidateResolver();

    /** Map from dependency type to corresponding autowired value */
    private final Map<Class<?>, Object> resolvableDependencies = new ConcurrentHashMap<>(16);

    /** Map of bean definition objects, keyed by bean name */
    //beanFactory持有此map,这样就能够在任什么时候候获取bean的BeanDefinition来建立一个bean实例
    private final Map<String, BeanDefinition> beanDefinitionMap = new ConcurrentHashMap<>(256);

    /** Map of singleton and non-singleton bean names, keyed by dependency type */
    private final Map<Class<?>, String[]> allBeanNamesByType = new ConcurrentHashMap<>(64);

    /** Map of singleton-only bean names, keyed by dependency type */
    private final Map<Class<?>, String[]> singletonBeanNamesByType = new ConcurrentHashMap<>(64);

    /** List of bean definition names, in registration order */
    private volatile List<String> beanDefinitionNames = new ArrayList<>(256);

    /** List of names of manually registered singletons, in registration order */
    private volatile Set<String> manualSingletonNames = new LinkedHashSet<>(16);

    /** Cached array of bean definition names in case of frozen configuration */
    @Nullable
    private volatile String[] frozenBeanDefinitionNames;

    /** Whether bean definition metadata may be cached for all beans */
    private volatile boolean configurationFrozen = false;
}

       DefaultListableBeanFactory类,这个类是默认的bean工厂实现类,这里只贴出了部分源码,完整的代码太长。咱们来看其中的一行代码:

       

/** Map of bean definition objects, keyed by bean name */
    //beanFactory持有此map,这样就能够在任什么时候候获取bean的BeanDefinition来建立一个bean实例
    private final Map<String, BeanDefinition> beanDefinitionMap = new ConcurrentHashMap<>(256);

       这行代码证实了咱们的猜想,从方法是说明就能够看出这是bean定义的map对象,以bean的名称做为key。到这里思路就明确了,bean工厂的初始化就是往这个map对象里加东西,把咱们xml文件里定义的bean填充到这个对象里,bean工厂就能够工做了。

       那么怎样将xml文件配置的bean注册到这个map对象里呢?咱们能够试试如下思路:

       一、须要一个工具来找到xml配置文件,能够称之为资源定位;

       二、须要一个Reader来读取xml配置信息,即DOM解析;

       三、将读取出来的信息注册到map对象里。

       以代码来验证一下,写一个Person类做为bean:

       

public class Person {

    public void work(){
        System.out.println("I am working...");
    }
}

       建立一个applicationContext.xml配置文件,配置bean:

       

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="person" class="com.springframework.bean.test.Person"></bean>
    
</beans>

       接下来写个测试类:

       

public class Client {

    public static void main(String[] args) {
        
        ClassPathResource classPathResource = new ClassPathResource("applicationContext.xml");
        
        DefaultListableBeanFactory defaultListableBeanFactory = new DefaultListableBeanFactory();
        
        XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(defaultListableBeanFactory);
        
        beanDefinitionReader.loadBeanDefinitions(classPathResource);
        
        System.out.println(defaultListableBeanFactory.getBeanDefinitionCount());
        
        Person person = (Person)defaultListableBeanFactory.getBean("person");
        
        person.work();
        
    }
}

       执行结果以下:

       

七月 06, 2017 9:41:48 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [applicationContext.xml]
1
I am working...

 

       从结果能够看出,咱们成功解析了xml文件,并注册了一个bean定义,经过getBean()方法成功返回了一个实例。上面的测试类用4行代码实现了bean工厂的初始化:

       第一行,完成了资源定位;

       第二行,建立了一个默认的bean工厂;

       第三行,建立了一个Reader,这个Reader用来读取xml文件,将建立的defaultListableBeanFactory 做为参数传递给Reader,表示为此工厂建立Reader;

       第四行,用Reader读取配置信息,并将解析的bean定义注册到defaultListableBeanFactory 中。

       执行完以上四个步骤,bean工厂酒杯正确初始化了,接下来咱们能够调用工厂的方法,以及得到bean实例。

       可是在实际开发中不会这么复杂,spring能够更简单的一步到位,它是这么作的:

       

public class TestSpringBeanFactory {
    
    public static void main(String[] args) {
        
        ApplicationContext ctx = new FileSystemXmlApplicationContext("src/applicationContext.xml");
        
        System.out.println(ctx.getBeanDefinitionCount());
        
        Person person = (Person) ctx.getBean("person");
        
        person.work();
        
    }

}

       咱们看看执行结果:

       

七月 06, 2017 9:42:55 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.FileSystemXmlApplicationContext@20ad9418: startup date [Thu Jul 06 21:42:55 CST 2017]; root of context hierarchy
七月 06, 2017 9:42:55 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from file [C:\Users\fangfuhai\workspace\spring-code-learning\src\applicationContext.xml]
1
I am working...

 

       从结果能够看出,spring用一行代码就完成了咱们四个步骤,仔细看看日志信息就能够发现,spring也是用XmlBeanDefinitionReader 来读取、解析并注册,同时在日志信息里还多了两行,这说明在这一行代码里,spring还作了更多的事情。

       咱们在new一个FileSystemXmlApplicationContext对象的时候,spring到底作了那些事情呢?下一章节咱们来一探究竟。

相关文章
相关标签/搜索