3_2 IOC容器根据xml配置创建Bean的实例

前言

IOC容器最核心的功能,加载Bean

Demo代码

java部分

package com.shenge.spring.ioc;

import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 *IOC容器初始化
 * 简单的获取Bean
 * */
public class SpringIOC1 {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext context =  new ClassPathXmlApplicationContext("spring_ioc_1.xml");
        Object testBeean = context.getBean("testBeean");
        System.out.println(testBeean.getClass().getName());
    }
}

xml

<?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="testBeean" class="com.shenge.spring.model.TestBean1"></bean>
</beans>

流程图

  1. ClassPathXmlApplicationContext构造函数的时候,做了什么

     ClassPathXmlApplicationContext context =  new ClassPathXmlApplicationContext("spring_ioc_1.xml");
    
ClassPathXmlApplicationContext构造流程.png

上述流程图描述大概的流程,内容其实太多了,只关注本节核心部分


ClassPathXmlApplicationContext注册BeanDefination实例化Bean.png
  1. 加载BeanDefination总结

    • BeanDefination 顾名思义就是Bean定义的抽象,用于描述Bean的生命周期,className等属性
    • 通过AbstractApplicationConext的核心方法refresh,加载新的BeanFactory
    • refresh是个模版方法,实际调用AbstractRefreshableApplicationContext的refreshBeanFactory加载新的BeanFactory
    • 加载新的BeanFactory的时候,最终调用XmlBeanDefinationReader解析xml成document
    • 再由DefaultBeanDefinitionDocumentReader读取Document,解析成BeanDefination
    • 最后再由BeanFactory将解析后的BeanDefination注册到BeanFactory内部的一个map当中,(Map<String, BeanDefinition> beanDefinitionMap)
  2. 实例话Bean总结

    • 通过AbstractApplicationConext的核心方法refresh
    • refresh是个模版方法,实际调用AbstractApplicationConext的finishBeanFactoryInitialization完成单例Bean的实例化
    • AbstractApplicationConext 调用内部BeanFactory的preInstantiateSingletons实例化Bean

接口设计

image-20200719190708920.png

每一个接口都代表一个最核心的功能模块,现在我们想看的是IOC容器怎么加载Bean的,那最核心的一定是BeanFactory

BeanFactory -------> ListableBeanFactory,HierarchicalBeanFactory

BeanFactory
  1. 根据Bean名称或类名或名称+构造参数等获取Bean实例

  2. 根据Bean名称判断容器内是否含有此Bean

  3. 根据Bean名称判断Bean是否单例 或者 多例

  4. 获取Bean的类型

  5. 判断Bean的类型

  6. 获取特殊Bean提供者(暂时不知道)

  7. 获取别名


    image-20200719193806150.png
ListableBeanFactory
  1. 根据Bean的名称获取Bean的元数据(BeanDefinition)

  2. 获取所有元数据的数量

  3. 根据Bean的类型获取Bean的name集合

  4. 根据Bean的类型取Bean的实例集合

  5. 根据注解获取Bean的name集合

  6. 根据注解获取Bean和注解的Map

  7. 获取Bean的注解

image-20200719200531640.png
HierarchicalBeanFactory
  1. 得到父容器

  2. 根据Bean的额名称在本地容器查找是否存在

image-20200719201345120.png

在Demo代码中我们用的ApplicationContext,即上下文

ApplicationContext ---------->ConfigurableApplicationContext ------->AbstractApplicationContext -------------> AbstractRefreshableApplictionContext------------->AbstractRefreshableConfigApplictionContext------------->AbstractXmlApplicationContext------------->ClassPathXmlApplicationContext

ApplicationContext
  1. 获取ApplicationContext的唯一ID

  2. 获取ApplicationContext的名称

  3. 获取ApplicationContext的friendly名称

  4. 获取ApplicationContext 启动时间

  5. 获取ApplicationContext 的父ApplicationContext

  6. 获取具有自动装配能力的容器(BeanFactory)

image-20200719202354116.png
ConfigurableApplicationContext
  1. 设置context的唯一ID

  2. 设置context 的父ApplicationContext

  3. 设置conetext的environment

  4. 设置context的BeanFactoryPostProcessor

  5. 设置context的Application监听者

  6. 设置类加载器

  7. 设置协议解析器

  8. 刷新容器

  9. 注册关闭钩子

  10. 关闭容器

  11. 判断是否context激活

  12. 获取一个ConfigurableListableBeanFactory容器实例

image-20200719204012884.png
抽象类

AbstractContextApplication


ApplicationConetxt接口实现部分

//---------------------------------------------------------------------
    // Implementation of ApplicationContext interface
    //---------------------------------------------------------------------
    @Override
    public String getId() {
        return this.id;
    }

    @Override
    public String getApplicationName() {
        return "";
    }

    @Override
    public String getDisplayName() {
        return this.displayName;
    }

    @Override
    @Nullable
    public ApplicationContext getParent() {
        return this.parent;
    }

    @Override
    public ConfigurableEnvironment getEnvironment() {
        if (this.environment == null) {
            this.environment = createEnvironment();
        }
        return this.environment;
    }

    @Override
    public AutowireCapableBeanFactory getAutowireCapableBeanFactory() throws IllegalStateException {
        return getBeanFactory();
    }

    @Override
    public long getStartupDate() {
        return this.startupDate;
    }

    @Override
    public void publishEvent(ApplicationEvent event) {
        publishEvent(event, null);
    }

    @Override
    public void publishEvent(Object event) {
        publishEvent(event, null);
    }

ConfigurableApplicationContext接口实现部分(getBeanFactory, setClassLoader,addProtocalResolver没有实现 )

    @Override
    public void setId(String id) {
        this.id = id;
    }

   @Override
    public void addApplicationListener(ApplicationListener<?> listener) {
        Assert.notNull(listener, "ApplicationListener must not be null");
        if (this.applicationEventMulticaster != null) {
            this.applicationEventMulticaster.addApplicationListener(listener);
        }
        this.applicationListeners.add(listener);
    }

   @Override
    public void addBeanFactoryPostProcessor(BeanFactoryPostProcessor postProcessor) {
        Assert.notNull(postProcessor, "BeanFactoryPostProcessor must not be null");
        this.beanFactoryPostProcessors.add(postProcessor);
    }

   @Override
    public void close() {
        synchronized (this.startupShutdownMonitor) {
            doClose();
            // If we registered a JVM shutdown hook, we don't need it anymore now:
            // We've already explicitly closed the context.
            if (this.shutdownHook != null) {
                try {
                    Runtime.getRuntime().removeShutdownHook(this.shutdownHook);
                }
                catch (IllegalStateException ex) {
                    // ignore - VM is already shutting down
                }
            }
        }
    }


   @Override
    public ConfigurableEnvironment getEnvironment() {
    if (this.environment == null) {
        this.environment = createEnvironment();
    }
    return this.environment;
   }


   @Override
   public boolean isActive() {
        return this.active.get();
   }


   @Override
   public void refresh() throws BeansException, IllegalStateException {
        synchronized (this.startupShutdownMonitor) {
            // Prepare this context for refreshing.
            prepareRefresh();

            // Tell the subclass to refresh the internal bean factory.
            ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

            // Prepare the bean factory for use in this context.
            prepareBeanFactory(beanFactory);

            try {
                // Allows post-processing of the bean factory in context subclasses.
                postProcessBeanFactory(beanFactory);

                // Invoke factory processors registered as beans in the context.
                invokeBeanFactoryPostProcessors(beanFactory);

                // Register bean processors that intercept bean creation.
                registerBeanPostProcessors(beanFactory);

                // Initialize message source for this context.
                initMessageSource();

                // Initialize event multicaster for this context.
                initApplicationEventMulticaster();

                // Initialize other special beans in specific context subclasses.
                onRefresh();

                // Check for listener beans and register them.
                registerListeners();

                // Instantiate all remaining (non-lazy-init) singletons.
                finishBeanFactoryInitialization(beanFactory);

                // Last step: publish corresponding event.
                finishRefresh();
            }

            catch (BeansException ex) {
                if (logger.isWarnEnabled()) {
                    logger.warn("Exception encountered during context initialization - " +
                            "cancelling refresh attempt: " + ex);
                }

                // Destroy already created singletons to avoid dangling resources.
                destroyBeans();

                // Reset 'active' flag.
                cancelRefresh(ex);

                // Propagate exception to caller.
                throw ex;
            }

            finally {
                // Reset common introspection caches in Spring's core, since we
                // might not ever need metadata for singleton beans anymore...
                resetCommonCaches();
            }
        }
   }

   @Override
    public void registerShutdownHook() {
        if (this.shutdownHook == null) {
            // No shutdown hook registered yet.
            this.shutdownHook = new Thread(SHUTDOWN_HOOK_THREAD_NAME) {
                @Override
                public void run() {
                    synchronized (startupShutdownMonitor) {
                        doClose();
                    }
                }
            };
            Runtime.getRuntime().addShutdownHook(this.shutdownHook);
        }
    }

   @Override
    public void setEnvironment(ConfigurableEnvironment environment) {
        this.environment = environment;
   }


   @Override
    public void setId(String id) {
        this.id = id;
   }

    @Override
    public void setParent(@Nullable ApplicationContext parent) {
        this.parent = parent;
        if (parent != null) {
            Environment parentEnvironment = parent.getEnvironment();
            if (parentEnvironment instanceof ConfigurableEnvironment) {
                getEnvironment().merge((ConfigurableEnvironment) parentEnvironment);
            }
        }
 }

BeanFactory接口实现部分

//---------------------------------------------------------------------
    // Implementation of BeanFactory interface
    //---------------------------------------------------------------------

    @Override
    public Object getBean(String name) throws BeansException {
        assertBeanFactoryActive();
        return getBeanFactory().getBean(name);
    }

    @Override
    public <T> T getBean(String name, Class<T> requiredType) throws BeansException {
        assertBeanFactoryActive();
        return getBeanFactory().getBean(name, requiredType);
    }

    @Override
    public Object getBean(String name, Object... args) throws BeansException {
        assertBeanFactoryActive();
        return getBeanFactory().getBean(name, args);
    }

    @Override
    public <T> T getBean(Class<T> requiredType) throws BeansException {
        assertBeanFactoryActive();
        return getBeanFactory().getBean(requiredType);
    }

    @Override
    public <T> T getBean(Class<T> requiredType, Object... args) throws BeansException {
        assertBeanFactoryActive();
        return getBeanFactory().getBean(requiredType, args);
    }

    @Override
    public <T> ObjectProvider<T> getBeanProvider(Class<T> requiredType) {
        assertBeanFactoryActive();
        return getBeanFactory().getBeanProvider(requiredType);
    }

    @Override
    public <T> ObjectProvider<T> getBeanProvider(ResolvableType requiredType) {
        assertBeanFactoryActive();
        return getBeanFactory().getBeanProvider(requiredType);
    }

    @Override
    public boolean containsBean(String name) {
        return getBeanFactory().containsBean(name);
    }

    @Override
    public boolean isSingleton(String name) throws NoSuchBeanDefinitionException {
        assertBeanFactoryActive();
        return getBeanFactory().isSingleton(name);
    }

    @Override
    public boolean isPrototype(String name) throws NoSuchBeanDefinitionException {
        assertBeanFactoryActive();
        return getBeanFactory().isPrototype(name);
    }

    @Override
    public boolean isTypeMatch(String name, ResolvableType typeToMatch) throws NoSuchBeanDefinitionException {
        assertBeanFactoryActive();
        return getBeanFactory().isTypeMatch(name, typeToMatch);
    }

    @Override
    public boolean isTypeMatch(String name, Class<?> typeToMatch) throws NoSuchBeanDefinitionException {
        assertBeanFactoryActive();
        return getBeanFactory().isTypeMatch(name, typeToMatch);
    }

    @Override
    @Nullable
    public Class<?> getType(String name) throws NoSuchBeanDefinitionException {
        assertBeanFactoryActive();
        return getBeanFactory().getType(name);
    }

    @Override
    @Nullable
    public Class<?> getType(String name, boolean allowFactoryBeanInit) throws NoSuchBeanDefinitionException {
        assertBeanFactoryActive();
        return getBeanFactory().getType(name, allowFactoryBeanInit);
    }

    @Override
    public String[] getAliases(String name) {
        return getBeanFactory().getAliases(name);
    }

ListableBeanFactory的接口实现部分

@Override
    public boolean containsBeanDefinition(String beanName) {
        return getBeanFactory().containsBeanDefinition(beanName);
    }

    @Override
    public int getBeanDefinitionCount() {
        return getBeanFactory().getBeanDefinitionCount();
    }

    @Override
    public String[] getBeanDefinitionNames() {
        return getBeanFactory().getBeanDefinitionNames();
    }

    @Override
    public String[] getBeanNamesForType(ResolvableType type) {
        assertBeanFactoryActive();
        return getBeanFactory().getBeanNamesForType(type);
    }

    @Override
    public String[] getBeanNamesForType(ResolvableType type, boolean includeNonSingletons, boolean allowEagerInit) {
        assertBeanFactoryActive();
        return getBeanFactory().getBeanNamesForType(type, includeNonSingletons, allowEagerInit);
    }

    @Override
    public String[] getBeanNamesForType(@Nullable Class<?> type) {
        assertBeanFactoryActive();
        return getBeanFactory().getBeanNamesForType(type);
    }

    @Override
    public String[] getBeanNamesForType(@Nullable Class<?> type, boolean includeNonSingletons, boolean allowEagerInit) {
        assertBeanFactoryActive();
        return getBeanFactory().getBeanNamesForType(type, includeNonSingletons, allowEagerInit);
    }

    @Override
    public <T> Map<String, T> getBeansOfType(@Nullable Class<T> type) throws BeansException {
        assertBeanFactoryActive();
        return getBeanFactory().getBeansOfType(type);
    }

    @Override
    public <T> Map<String, T> getBeansOfType(@Nullable Class<T> type, boolean includeNonSingletons, boolean allowEagerInit)
            throws BeansException {

        assertBeanFactoryActive();
        return getBeanFactory().getBeansOfType(type, includeNonSingletons, allowEagerInit);
    }

    @Override
    public String[] getBeanNamesForAnnotation(Class<? extends Annotation> annotationType) {
        assertBeanFactoryActive();
        return getBeanFactory().getBeanNamesForAnnotation(annotationType);
    }

    @Override
    public Map<String, Object> getBeansWithAnnotation(Class<? extends Annotation> annotationType)
            throws BeansException {

        assertBeanFactoryActive();
        return getBeanFactory().getBeansWithAnnotation(annotationType);
    }

    @Override
    @Nullable
    public <A extends Annotation> A findAnnotationOnBean(String beanName, Class<A> annotationType)
            throws NoSuchBeanDefinitionException {

        assertBeanFactoryActive();
        return getBeanFactory().findAnnotationOnBean(beanName, annotationType);
    }

HierarchicalBeanFactory接口实现部分

   @Override
   @Nullable
   public BeanFactory getParentBeanFactory() {
        return getParent();
   }

   @Override
   public boolean containsLocalBean(String name) {
        return getBeanFactory().containsLocalBean(name);
   }

三个抽象方法


    protected abstract void refreshBeanFactory() throws BeansException, IllegalStateException;

    protected abstract void closeBeanFactory();

    @Override
    public abstract ConfigurableListableBeanFactory getBeanFactory() throws IllegalStateException;

总结

  • 最底层的ApplictionContext抽象类
  • 类内置的一个BeanFacorty成员变量,它实现了BeanFactory,HierarchicalBeanFactory,ListableBeanFactory的所有接口。应该是采用了策略模式
  • 细品ConfigurableApplicationContext,Configurable,可配置的,它继承于ApplicationContext,它有很多公共set方法,它允许其他对象修改其内部属性
  • getBeanFactory继承于ConfigurableApplicationContext没有被实现,也就是说AbstractAppicationContext的实现类必须实现这个方法。不难发现,这其实是一个解耦的一个思想,AbstractAppicationContext作为最基础的抽象类,它需要把最灵活,最多变的东西抽离出去。这个东西就是BeanFactory,ConfigurableApplicationContext从这个接口已经开始提前做准备了。通过策略模式将IOC的实现BeanFactory剥离出去。将方法getBeanFactory交由子类实现,源码里有两个实现了此方法的实现类AbstractRefreshableApplicationContext和GenericApplicationContext
  • DefaultListableBeanFactory是子类getBeanFactory返回的实际BeanFactory
  • 核心方法refresh,模版方法,描述了整个ApplictionContext加载Bean的全过程,设置监听器,加载内部BeanFactory,实例化Bean,事件通知等等。其中最灵活,最多变的refreshBeanFactory被抽离了出去,交给子类实现
ClassPathXmlApplicationContext
  1. 10个构造方法
  2. 获取资源文件列表
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 204,590评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 86,808评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 151,151评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,779评论 1 277
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,773评论 5 367
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,656评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,022评论 3 398
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,678评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 41,038评论 1 299
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,659评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,756评论 1 330
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,411评论 4 321
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,005评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,973评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,203评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,053评论 2 350
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,495评论 2 343