深入浅出Spring源码:IOC原理解析(一)

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

首先我们来关注一个接口,源码如下:

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。

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

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

2、持有各种bean之间的依赖关系,如果一个类中持有对另一个类的引用,那么在对该类进行实例化时,必须根据类之间的依赖关系对相关类也进行实例化,因此,工厂必须获得类之间的依赖关系,否则无法正确实例化;

3、以上两种信息都依赖于我们的配置信息定义,比如xml配置文件,工厂需要一个工具来读取配置文件的信息。

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

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实例。

从上面两个接口,我们大致可以猜出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对象里呢?我们可以试试以下思路:

1、需要一个工具来找到xml配置文件,可以称之为资源定位;

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

3、将读取出来的信息注册到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到底做了那些事情呢?下一章节我们来一探究竟。


作者:风中程序猿
出处:https://www.cnblogs.com/fangfuhai/p/7128534.html

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,997评论 6 502
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,603评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 163,359评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,309评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,346评论 6 390
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,258评论 1 300
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,122评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,970评论 0 275
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,403评论 1 313
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,596评论 3 334
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,769评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,464评论 5 344
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,075评论 3 327
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,705评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,848评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,831评论 2 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,678评论 2 354

推荐阅读更多精彩内容

  • 作为Java程序员,Spirng我们再熟悉不过,可以说比自己的女朋友还要亲密,每天都会和他在一起,然而我们真的了解...
    莫那一鲁道阅读 15,498评论 7 57
  • 前言 Spring 最重要的概念是 IOC 和 AOP,本篇文章其实就是要带领大家来分析下 Spring 的 IO...
    Sunny捏阅读 513评论 0 2
  • 如果对IOC的概念还不是很清楚,可以先阅读上一篇文章:Spring_IOC_01——概念讲解 Spring IOC...
    _Zy阅读 1,158评论 3 12
  • 久违的晴天,家长会。 家长大会开好到教室时,离放学已经没多少时间了。班主任说已经安排了三个家长分享经验。 放学铃声...
    飘雪儿5阅读 7,523评论 16 22
  • 今天感恩节哎,感谢一直在我身边的亲朋好友。感恩相遇!感恩不离不弃。 中午开了第一次的党会,身份的转变要...
    迷月闪星情阅读 10,564评论 0 11