微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Spring框架系列(7) - Spring IOC实现原理详解之IOC初始化流程

上文,我们看了IOC设计要点和设计结构;紧接着这篇,我们可以看下源码的实现了:Spring如何实现将资源配置(以xml配置为例)通过加载,解析,生成BeanDefination并注册到IoC容器中的。@pdai

引入

上文,我们看了IOC设计要点和设计结构;紧接着这篇,我们可以看下源码的实现了:Spring如何实现将资源配置(以xml配置为例)通过加载,解析,生成BeanDefination并注册到IoC容器中的(就是我们圈出来的部分)

如何将Bean从XML配置中解析后放到IoC容器中的?

本文的目标就是分析Spring如何实现将资源配置(以xml配置为例)通过加载,解析,生成BeanDefination并注册到IoC容器中的。

初始化的入口

对于xml配置的Spring应用,在main()方法中实例化ClasspathXmlApplicationContext即可创建一个IoC容器。我们可以从这个构造方法开始,探究一下IoC容器的初始化过程。

 // create and configure beans
ApplicationContext context = new ClasspathXmlApplicationContext("aspects.xml", "daos.xml", "services.xml");
public ClasspathXmlApplicationContext(String... configLocations) throws BeansException {
    this(configLocations, true, (ApplicationContext)null);
}

public ClasspathXmlApplicationContext(String[] configLocations, boolean refresh, @Nullable ApplicationContext parent) throws BeansException {
    // 设置Bean资源加载器
    super(parent);

    // 设置配置路径
    this.setConfigLocations(configLocations);

    // 初始化容器
    if (refresh) {
        this.refresh();
    }
}

设置资源解析器和环境

调用父类容器AbstractApplicationContext的构造方法(super(parent)方法)为容器设置好Bean资源加载器

public AbstractApplicationContext(@Nullable ApplicationContext parent) {
    // 认构造函数初始化容器id, name, 状态 以及 资源解析器
    this();

    // 将父容器的Environment合并到当前容器
    this.setParent(parent);
}

通过AbstractApplicationContext认构造函数初始化容器id, name, 状态 以及 资源解析器

public AbstractApplicationContext() {
    this.logger = LogFactory.getLog(this.getClass());
    this.id = ObjectUtils.identityToString(this);
    this.displayName = ObjectUtils.identityToString(this);
    this.beanfactoryPostProcessors = new ArrayList();
    this.active = new AtomicBoolean();
    this.closed = new AtomicBoolean();
    this.startupShutdownMonitor = new Object();
    this.applicationStartup = ApplicationStartup.DEFAULT;
    this.applicationListeners = new LinkedHashSet();
    this.resourcePatternResolver = this.getResourcePatternResolver();
}
// Spring资源加载器
protected ResourcePatternResolver getResourcePatternResolver() {
    return new PathMatchingResourcePatternResolver(this);
}

通过AbstractApplicationContext的setParent(parent)方法父容器的Environment合并到当前容器

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

设置配置路径

在设置容器的资源加载器之后,接下来FileSystemXmlApplicationContet执行setConfigLocations方法通过调用父类AbstractRefreshableConfigApplicationContext的方法进行对Bean定义资源文件的定位

public void setConfigLocations(@Nullable String... locations) {
    if (locations != null) {
        Assert.noNullElements(locations, "Config locations must not be null");
        this.configLocations = new String[locations.length];

        for(int i = 0; i < locations.length; ++i) {
            // 解析配置路径
            this.configLocations[i] = this.resolvePath(locations[i]).trim();
        }
    } else {
        this.configLocations = null;
    }
}
protected String resolvePath(String path) {
    // 从上一步Environment中解析
    return this.getEnvironment().resolverequiredPlaceholders(path);
}

初始化的主体流程

Spring IoC容器对Bean定义资源的载入是从refresh()函数开始的,refresh()是一个模板方法,refresh()方法的作用是:在创建IoC容器前,如果已经有容器存在,则需要把已有的容器销毁和关闭,以保证在refresh之后使用的是新建立起来的IoC容器。refresh的作用类似于对IoC容器的重启,在新建立好的容器中对容器进行初始化,对Bean定义资源进行载入。

@Override
public void refresh() throws BeansException, IllegalStateException {
    synchronized (this.startupShutdownMonitor) {
        StartupStep contextRefresh = this.applicationStartup.start("spring.context.refresh");

        // 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);

            StartupStep beanPostProcess = this.applicationStartup.start("spring.context.beans.post-process");
            // Invoke factory processors registered as beans in the context.
            invokebeanfactoryPostProcessors(beanfactory);

            // Register bean processors that intercept bean creation.
            registerBeanPostProcessors(beanfactory);
            beanPostProcess.end();

            // 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();
            contextRefresh.end();
        }
    }
}

这里的设计上是一个非常典型的资源类加载处理型的思路,头脑中需要形成如下图的顶层思路(而不是只停留在流水式的方法上面):

  • 模板方法设计模式,模板方法中使用典型的钩子方法
  • 具体的初始化加载方法插入到钩子方法之间
  • 将初始化的阶段封装,用来记录当前初始化到什么阶段;常见的设计是xxxPhase/xxxStage;
  • 资源加载初始化有失败等处理,必然是try/catch/finally...

初始化beanfactory之obtainFreshbeanfactory

AbstractApplicationContext的obtainFreshbeanfactory()方法调用子类容器的refreshbeanfactory()方法,启动容器载入Bean定义资源文件的过程,代码如下:

protected ConfigurableListablebeanfactory obtainFreshbeanfactory() {
    // 这里使用了委派设计模式,父类定义了抽象的refreshbeanfactory()方法,具体实现调用子类容器的refreshbeanfactory()方法
    refreshbeanfactory();
    return getbeanfactory();
}

AbstractApplicationContext类中只抽象定义了refreshbeanfactory()方法,容器真正调用的是其子类AbstractRefreshableApplicationContext实现的refreshbeanfactory()方法;
在创建IoC容器前,如果已经有容器存在,则需要把已有的容器销毁和关闭,以保证在refresh之后使用的是新建立起来的IoC容器。方法的源码如下:

protected final void refreshbeanfactory() throws BeansException {
    // 如果已经有容器存在,则需要把已有的容器销毁和关闭,以保证在refresh之后使用的是新建立起来的IoC容器
    if (hasbeanfactory()) {
        destroyBeans();
        closebeanfactory();
    }
    try {
        // 创建DefaultListablebeanfactory,并调用loadBeanDeFinitions(beanfactory)装载bean定义
        DefaultListablebeanfactory beanfactory = createbeanfactory();
        beanfactory.setSerializationId(getId());
        customizebeanfactory(beanfactory); // 对IoC容器进行定制化,如设置启动参数,开启注解的自动装配等 
        loadBeanDeFinitions(beanfactory); // 调用载入Bean定义的方法,主要这里又使用了一个委派模式,在当前类中只定义了抽象的loadBeanDeFinitions方法,具体的实现调用子类容器  
        this.beanfactory = beanfactory;
    }
    catch (IOException ex) {
        throw new ApplicationContextException("I/O error parsing bean deFinition source for " + getdisplayName(), ex);
    }
}

初始化beanfactory之loadBeanDeFinitions

AbstractRefreshableApplicationContext中只定义了抽象的loadBeanDeFinitions方法,容器真正调用的是其子类AbstractXmlApplicationContext对该方法的实现,AbstractXmlApplicationContext的主要源码如下:

protected void loadBeanDeFinitions(DefaultListablebeanfactory beanfactory) throws BeansException, IOException {
    // 创建XMLBeanDeFinitionReader,即创建Bean读取器,并通过回调设置到容器中去,容器使用该读取器读取Bean定义资源  
    XmlBeanDeFinitionReader beanDeFinitionReader = new XmlBeanDeFinitionReader(beanfactory);

    // 配置上下文的环境,资源加载器、解析器
    beanDeFinitionReader.setEnvironment(this.getEnvironment());
    beanDeFinitionReader.setResourceLoader(this);
    beanDeFinitionReader.setEntityResolver(new ResourceEntityResolver(this)); // 为Bean读取器设置SAX xml解析器

    // 允许子类自行初始化(比如校验机制),并提供真正的加载方法
    initBeanDeFinitionReader(beanDeFinitionReader); // 当Bean读取器读取Bean定义的Xml资源文件时,启用Xml的校验机制  
    loadBeanDeFinitions(beanDeFinitionReader);
}

protected void loadBeanDeFinitions(XmlBeanDeFinitionReader reader) throws BeansException, IOException {
    // 加载XML配置方式里的Bean定义的资源
    Resource[] configResources = getConfigResources();
    if (configResources != null) {
        reader.loadBeanDeFinitions(configResources);
    }
    // 加载构造函数里配置的Bean配置文件,即{"aspects.xml", "daos.xml", "services.xml"}
    String[] configLocations = getConfigLocations();
    if (configLocations != null) {
        reader.loadBeanDeFinitions(configLocations);
    }
}

Xml Bean读取器(XmlBeanDeFinitionReader)调用父类AbstractBeanDeFinitionReader的 reader.loadBeanDeFinitions方法读取Bean定义资源。

由于我们使用ClasspathXmlApplicationContext作为例子分析,因此getConfigResources的返回值为null,因此程序执行reader.loadBeanDeFinitions(configLocations)分支。

AbstractBeanDeFinitionReader读取Bean定义资源

AbstractBeanDeFinitionReader的loadBeanDeFinitions方法源码如下:

@Override
public int loadBeanDeFinitions(String location) throws BeanDeFinitionStoreException {
    return loadBeanDeFinitions(location, null);
}

public int loadBeanDeFinitions(String location, @Nullable Set<Resource> actualResources) throws BeanDeFinitionStoreException {
    ResourceLoader resourceLoader = getResourceLoader();
    if (resourceLoader == null) {
        throw new BeanDeFinitionStoreException(
                "Cannot load bean deFinitions from location [" + location + "]: no ResourceLoader available");
    }

    // 模式匹配类型的解析器,这种方式是加载多个满足匹配条件的资源
    if (resourceLoader instanceof ResourcePatternResolver) {
        try {
            // 获取到要加载的资源
            Resource[] resources = ((ResourcePatternResolver) resourceLoader).getResources(location);
            int count = loadBeanDeFinitions(resources); // 委派调用其子类XmlBeanDeFinitionReader的方法,实现加载功能  
            if (actualResources != null) {
                Collections.addAll(actualResources, resources);
            }
            if (logger.isTraceEnabled()) {
                logger.trace("Loaded " + count + " bean deFinitions from location pattern [" + location + "]");
            }
            return count;
        }
        catch (IOException ex) {
            throw new BeanDeFinitionStoreException(
                    "Could not resolve bean deFinition resource pattern [" + location + "]", ex);
        }
    }
    else {
        // 只能通过绝对路径URL加载单个资源.
        Resource resource = resourceLoader.getResource(location);
        int count = loadBeanDeFinitions(resource);
        if (actualResources != null) {
            actualResources.add(resource);
        }
        if (logger.isTraceEnabled()) {
            logger.trace("Loaded " + count + " bean deFinitions from location [" + location + "]");
        }
        return count;
    }
}

从对AbstractBeanDeFinitionReader的loadBeanDeFinitions方法源码分析可以看出该方法做了以下两件事:

XmlBeanDeFinitionReader加载Bean定义资源

继续看子类XmlBeanDeFinitionReader的loadBeanDeFinitions(Resource …)方法看到代表bean文件的资源定义以后的载入过程。

/**
    * 本质上是加载XML配置的Bean。
    * @param inputSource the SAX InputSource to read from
    * @param resource the resource descriptor for the XML file
    */
protected int doLoadBeanDeFinitions(InputSource inputSource, Resource resource)
        throws BeanDeFinitionStoreException {

    try {
        Document doc = doLoadDocument(inputSource, resource); // 将Bean定义资源转换成Document对象
        int count = registerBeanDeFinitions(doc, resource);
        if (logger.isDebugEnabled()) {
            logger.debug("Loaded " + count + " bean deFinitions from " + resource);
        }
        return count;
    }
    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);
    }
}

// 使用配置的DocumentLoader加载XML定义文件为Document.
protected Document doLoadDocument(InputSource inputSource, Resource resource) throws Exception {
    return this.documentLoader.loadDocument(inputSource, getEntityResolver(), this.errorHandler,
            getValidationModeForResource(resource), isNamespaceAware());
}

通过源码分析,载入Bean定义资源文件的最后一步是将Bean定义资源转换为Document对象,该过程由documentLoader实现

DocumentLoader将Bean定义资源转换为Document对象

DocumentLoader将Bean定义资源转换成Document对象的源码如下:

// 使用标准的JAXP将载入的Bean定义资源转换成document对象
@Override
public Document loadDocument(InputSource inputSource, EntityResolver entityResolver,
        ErrorHandler errorHandler, int validationMode, boolean namespaceAware) throws Exception {

    // 创建文件解析器工厂
    DocumentBuilderFactory factory = createDocumentBuilderFactory(validationMode, namespaceAware);
    if (logger.isTraceEnabled()) {
        logger.trace("Using JAXP provider [" + factory.getClass().getName() + "]");
    }
    // 创建文档解析器
    DocumentBuilder builder = createDocumentBuilder(factory, entityResolver, errorHandler);
    return builder.parse(inputSource); // 解析
}

protected DocumentBuilderFactory createDocumentBuilderFactory(int validationMode, boolean namespaceAware)
        throws ParserConfigurationException {

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(namespaceAware);

    // 设置解析XML的校验
    if (validationMode != XmlValidationModeDetector.VALIDATION_NONE) {
        factory.setValidating(true);
        if (validationMode == XmlValidationModeDetector.VALIDATION_XSD) {
            // Enforce namespace aware for XSD...
            factory.setNamespaceAware(true);
            try {
                factory.setAttribute(SCHEMA_LANGUAGE_ATTRIBUTE, XSD_SCHEMA_LANGUAGE);
            }
            catch (IllegalArgumentException ex) {
                ParserConfigurationException pcex = new ParserConfigurationException(
                        "Unable to validate using XSD: Your JAXP provider [" + factory +
                        "] does not support XML Schema. Are you running on Java 1.4 with Apache Crimson? " +
                        "Upgrade to Apache Xerces (or Java 1.5) for full XSD support.");
                pcex.initCause(ex);
                throw pcex;
            }
        }
    }

    return factory;
}

该解析过程调用JavaEE标准的JAXP标准进行处理。

至此Spring IoC容器根据定位的Bean定义资源文件,将其加载读入并转换成为Document对象过程完成。

接下来我们要继续分析Spring IoC容器将载入的Bean定义资源文件转换为Document对象之后,是如何将其解析为Spring IoC管理的Bean对象并将其注册到容器中的。

XmlBeanDeFinitionReader解析载入的Bean定义资源文件

XmlBeanDeFinitionReader类中的doLoadBeanDeFinitions方法是从特定XML文件中实际载入Bean定义资源的方法,该方法在载入Bean定义资源之后将其转换为Document对象,接下来调用registerBeanDeFinitions启动Spring IoC容器对Bean定义的解析过程,registerBeanDeFinitions方法源码如下:

// 按照Spring的Bean语义要求将Bean定义资源解析并转换为容器内部数据结构 
public int registerBeanDeFinitions(Document doc, Resource resource) throws BeanDeFinitionStoreException {
    BeanDeFinitionDocumentReader documentReader = createBeanDeFinitionDocumentReader();
    int countBefore = getRegistry().getBeanDeFinitionCount();
    // 解析过程入口,这里使用了委派模式,具体的解析实现过程有实现类DefaultBeanDeFinitionDocumentReader完成  
    documentReader.registerBeanDeFinitions(doc, createReaderContext(resource));
    return getRegistry().getBeanDeFinitionCount() - countBefore;  // 返回此次解析了多少个对象
}

// 创建BeanDeFinitionDocumentReader对象,解析Document对象  
protected BeanDeFinitionDocumentReader createBeanDeFinitionDocumentReader() {
    return BeanUtils.instantiateClass(this.documentReaderClass);
}

/**
    * Create the {@link XmlReaderContext} to pass over to the document reader.
    */
public XmlReaderContext createReaderContext(Resource resource) {
    return new XmlReaderContext(resource, this.problemReporter, this.eventListener,
            this.sourceExtractor, this, getNamespaceHandlerResolver());
}

Bean定义资源的载入解析分为以下两个过程:

  • 首先,通过调用XML解析器将Bean定义资源文件转换得到Document对象,但是这些Document对象并没有按照Spring的Bean规则进行解析。这一步是载入的过程
  • 其次,在完成通用的XML解析之后,按照Spring的Bean规则对Document对象进行解析。

按照Spring的Bean规则对Document对象解析的过程是在接口BeanDeFinitionDocumentReader的实现类DefaultBeanDeFinitionDocumentReader中实现的。

DefaultBeanDeFinitionDocumentReader对Bean定义的Document对象解析

BeanDeFinitionDocumentReader接口通过registerBeanDeFinitions方法调用其实现类DefaultBeanDeFinitionDocumentReader对Document对象进行解析,解析的代码如下:

@Override
public void registerBeanDeFinitions(Document doc, XmlReaderContext readerContext) {
    this.readerContext = readerContext;
    doRegisterBeanDeFinitions(doc.getDocumentElement());
}

// 注册<beans/>配置的Beans
@SuppressWarnings("deprecation")  // for Environment.acceptsProfiles(String...)
protected void doRegisterBeanDeFinitions(Element root) {
    // Any nested <beans> elements will cause recursion in this method. In
    // order to propagate and preserve <beans> default-* attributes correctly,
    // keep track of the current (parent) delegate, which may be null. Create
    // the new (child) delegate with a reference to the parent for fallback purposes,
    // then ultimately reset this.delegate back to its original (parent) reference.
    // this behavior emulates a stack of delegates without actually necessitating one.
    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);
            // We cannot use Profiles.of(...) since profile expressions are not supported
            // in XML config. See SPR-12458 for details.
            if (!getReaderContext().getEnvironment().acceptsProfiles(specifiedProfiles)) {
                if (logger.isDebugEnabled()) {
                    logger.debug("Skipped XML bean deFinition file due to specified profiles [" + profileSpec +
                            "] not matching: " + getReaderContext().getResource());
                }
                return;
            }
        }
    }

    preProcessXml(root);
    parseBeanDeFinitions(root, this.delegate); // 从Document的根元素开始进行Bean定义的Document对象  
    postProcessXml(root);

    this.delegate = parent;
}

BeanDeFinitionParserDelegate解析Bean定义资源文件生成BeanDeFinition

/**
    * Parse the elements at the root level in the document:
    * "import", "alias", "bean".
    * @param root the DOM root element of the document
    */
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) {
      
    // 如果元素节点是<Import>导入元素,进行导入解析
    if (delegate.nodeNameEquals(ele, IMPORT_ELEMENT)) {
        importBeanDeFinitionResource(ele);
    }
    // 如果元素节点是<Alias>别名元素,进行别名解析 
    else if (delegate.nodeNameEquals(ele, ALIAS_ELEMENT)) {
        processAliasRegistration(ele);
    }
    // 如果元素节点<Bean>元素, 按照Spring的Bean规则解析元素  
    else if (delegate.nodeNameEquals(ele, BEAN_ELEMENT)) {
        processBeanDeFinition(ele, delegate);
    }
    // 如果元素节点<Beans>元素,即它是嵌套类型的
    else if (delegate.nodeNameEquals(ele, nesTED_BEANS_ELEMENT)) {
        // 递归解析
        doRegisterBeanDeFinitions(ele);
    }
}

解析Bean生成BeanDeFinitionHolder的方法

/**
    * Process the given bean element, parsing the bean deFinition
    * and registering it with the registry.
    */
protected void processBeanDeFinition(Element ele, BeanDeFinitionParserDelegate delegate) {
    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);
        }
        // Send registration event.
        getReaderContext().fireComponentRegistered(new BeanComponentDeFinition(bdHolder));
    }
}

(这里就不展开了,无非就是解析XML各种元素,来生成BeanDeFinition)

解析过后的BeanDeFinition在IoC容器中的注册

Document对象的解析后得到封装BeanDeFinition的BeanDeFinitionHold对象,然后调用BeanDeFinitionReaderUtils的registerBeanDeFinition方法向IoC容器注册解析的Bean,BeanDeFinitionReaderUtils的注册的源码如下:

// 通过BeanDeFinitionRegistry将BeanDeFinitionHolder注册beanfactory
public static void registerBeanDeFinition(
        BeanDeFinitionHolder deFinitionHolder, BeanDeFinitionRegistry registry)
        throws BeanDeFinitionStoreException {

    // Register bean deFinition under primary name.
    String beanName = deFinitionHolder.getBeanName();
    registry.registerBeanDeFinition(beanName, deFinitionHolder.getBeanDeFinition());

    // Register aliases for bean name, if any.
    String[] aliases = deFinitionHolder.getAliases();
    if (aliases != null) {
        for (String alias : aliases) {
            registry.registeralias(beanName, alias);
        }
    }
}

调用BeanDeFinitionReaderUtils向IoC容器注册解析的BeanDeFinition时,真正完成注册功能的是DefaultListablebeanfactory

DefaultListablebeanfactory向IoC容器注册解析后的BeanDeFinition

IOC容器本质上就是一个beanDeFinitionMap, 注册即将BeanDeFinition put到map中

/** Map of bean deFinition objects, keyed by bean name. */
private final Map<String, BeanDeFinition> beanDeFinitionMap = new ConcurrentHashMap<>(256);

/** Map from bean name to merged BeanDeFinitionHolder. */
private final Map<String, BeanDeFinitionHolder> mergedBeanDeFinitionHolders = new ConcurrentHashMap<>(256);


@Override
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");

    if (beanDeFinition instanceof AbstractBeanDeFinition) {
        try {
            ((AbstractBeanDeFinition) beanDeFinition).validate();
        }
        catch (BeanDeFinitionValidationException ex) {
            throw new BeanDeFinitionStoreException(beanDeFinition.getResourceDescription(), beanName,
                    "Validation of bean deFinition Failed", ex);
        }
    }

    BeanDeFinition existingDeFinition = this.beanDeFinitionMap.get(beanName);
    // 如果已经注册
    if (existingDeFinition != null) {
        // 检查是否可以覆盖
        if (!isAllowBeanDeFinitionOverriding()) {
            throw new BeanDeFinitionOverrideException(beanName, beanDeFinition, existingDeFinition);
        }
        else if (existingDeFinition.getRole() < beanDeFinition.getRole()) {
            // e.g. was ROLE_APPLICATION, Now overriding with ROLE_SUPPORT or ROLE_INFRASTRUCTURE
            if (logger.isInfoEnabled()) {
                logger.info("Overriding user-defined bean deFinition for bean '" + beanName +
                        "' with a framework-generated bean deFinition: replacing [" +
                        existingDeFinition + "] with [" + beanDeFinition + "]");
            }
        }
        else if (!beanDeFinition.equals(existingDeFinition)) {
            if (logger.isDebugEnabled()) {
                logger.debug("Overriding bean deFinition for bean '" + beanName +
                        "' with a different deFinition: replacing [" + existingDeFinition +
                        "] with [" + beanDeFinition + "]");
            }
        }
        else {
            if (logger.isTraceEnabled()) {
                logger.trace("Overriding bean deFinition for bean '" + beanName +
                        "' with an equivalent deFinition: replacing [" + existingDeFinition +
                        "] with [" + beanDeFinition + "]");
            }
        }
        // 覆盖
        this.beanDeFinitionMap.put(beanName, beanDeFinition);
    }
    else {
        if (hasBeanCreationStarted()) {
            // Cannot modify startup-time collection elements anymore (for stable iteration)
            synchronized (this.beanDeFinitionMap) {
                this.beanDeFinitionMap.put(beanName, beanDeFinition);
                List<String> updatedDeFinitions = new ArrayList<>(this.beanDeFinitionNames.size() + 1);
                updatedDeFinitions.addAll(this.beanDeFinitionNames);
                updatedDeFinitions.add(beanName);
                this.beanDeFinitionNames = updatedDeFinitions;
                removeManualSingletonName(beanName);
            }
        }
        else {
            // Still in startup registration phase
            this.beanDeFinitionMap.put(beanName, beanDeFinition);
            this.beanDeFinitionNames.add(beanName);
            removeManualSingletonName(beanName);
        }
        //重置所有已经注册过的BeanDeFinition的缓存  
        this.frozenBeanDeFinitionNames = null;
    }

    if (existingDeFinition != null || containsSingleton(beanName)) {
        resetBeanDeFinition(beanName);
    }
    else if (isConfigurationFrozen()) {
        clearByTypeCache();
    }
}

至此,Bean定义资源文件中配置的Bean被解析过后,已经注册到IoC容器中,被容器管理起来,真正完成了IoC容器初始化所做的全部工作。现 在IoC容器中已经建立了整个Bean的配置信息,这些BeanDeFinition信息已经可以使用,并且可以被检索,IoC容器的作用就是对这些注册的Bean定义信息进行处理和维护。这些的注册的Bean定义信息是IoC容器控制反转的基础,正是有了这些注册的数据,容器才可以进行依赖注入。

总结

现在通过上面的代码,总结一下IOC容器初始化的基本步骤:

  • 初始化的入口在容器实现中的 refresh()调用来完成

  • 对 bean 定义载入 IOC 容器使用的方法是 loadBeanDeFinition,其中的大致过程如下:

    • 通过 ResourceLoader 来完成资源文件位置的定位,DefaultResourceLoader 是认的实现,同时上下文本身就给出了 ResourceLoader 的实现,可以从类路径,文件系统, URL 等方式来定为资源位置。如果是 Xmlbeanfactory作为 IOC 容器,那么需要为它指定 bean 定义的资源,也就是说 bean 定义文件时通过抽象成 Resource 来被 IOC 容器处理的
    • 通过 BeanDeFinitionReader来完成定义信息的解析和 Bean 信息的注册, 往往使用的是XmlBeanDeFinitionReader 来解析 bean 的 xml 定义文件 - 实际的处理过程是委托给 BeanDeFinitionParserDelegate 来完成的,从而得到 bean 的定义信息,这些信息在 Spring 中使用 BeanDeFinition 对象来表示 - 这个名字可以让我们想到loadBeanDeFinition,RegisterBeanDeFinition 这些相关的方法 - 他们都是为处理 BeanDefinitin 服务的
    • 容器解析得到 BeanDeFinition 以后,需要把它在 IOC 容器中注册,这由 IOC 实现 BeanDeFinitionRegistry 接口来实现。注册过程就是在 IOC 容器内部维护的一个HashMap 来保存得到的 BeanDeFinition 的过程。这个 HashMap 是 IoC 容器持有 bean 信息的场所,以后对 bean 的操作都是围绕这个HashMap 来实现的.
  • 然后我们就可以通过 beanfactory 和 ApplicationContext 来享受到 Spring IOC 的服务了,在使用 IOC 容器的时候,我们注意到除了少量粘合代码,绝大多数以正确 IoC 风格编写的应用程序代码完全不用关心如何到达工厂,因为容器将把这些对象与容器管理的其他对象钩在一起。基本的策略是把工厂放到已知的地方,最好是放在对预期使用的上下文有意义的地方,以及代码将实际需要访问工厂的地方。 Spring 本身提供了对声明式载入 web 应用程序用法的应用程序上下文,并将其存储在ServletContext 中的框架实现。

参考文章

https://blog.csdn.net/qq_36212439/article/details/82749963

https://juejin.cn/post/6973884466171215908

https://juejin.cn/post/6844903838743265294

https://blog.csdn.net/hjing123/article/details/104867343

https://www.cnblogs.com/wl20200316/p/12522993.html

更多文章

首先, 从Spring框架的整体架构和组成对整体框架有个认知。

其次,通过案例引出Spring的核心(IoC和AOP),同时对IoC和AOP进行案例使用分析。

基于Spring框架和IOC,AOP的基础,为构建上层web应用,需要进一步学习SpringMVC。

  • Spring基础 - SpringMVC请求流程和案例
    • 前文我们介绍了Spring框架和Spring框架中最为重要的两个技术点(IOC和AOP),那我们如何更好的构建上层的应用呢(比如web 应用),这便是SpringMVC;Spring MVC是Spring在Spring Container Core和AOP等技术基础上,遵循上述Web MVC的规范推出的web开发框架,目的是为了简化Java栈的web开发。 本文主要介绍SpringMVC的请求流程和基础案例的编写和运行。

Spring进阶 - IoC,AOP以及SpringMVC的源码分析

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐