Spring源碼剖析1:初探Spring IOC核心流程

本文大體地介紹了IOC容器的初始化過程,只列出了比較重要的過程和代碼,能夠從中看出IOC容器執行的大體流程。html

接下來的文章會更加深刻剖析Bean容器如何解析xml,註冊和初始化bean,以及如何獲取bean實例等詳細的過程。node

轉自:http://www.importnew.com/19243.htmlgit

1. 初始化github

大體單步跟了下Spring IOC的初始化過程,整個脈絡很龐大,初始化的過程主要就是讀取XML資源,並解析,最終註冊到Bean Factory中:spring

在完成初始化的過程後,Bean們就在BeanFactory中蓄勢以待地等調用了。下面經過一個具體的例子,來詳細地學習一下初始化過程,例如當加載下面一個bean:app

<bean id="XiaoWang" class="com.springstudy.talentshow.SuperInstrumentalist">
    <property name="instruments">
        <list>
            <ref bean="piano"/>
            <ref bean="saxophone"/>
        </list>
    </property>
</bean>

加載時須要讀取、解析、註冊bean,這個過程具體的調用棧以下所示: 框架

下面對每一步的關鍵的代碼進行詳細分析:ide

準備

保存配置位置,並刷新 在調用ClassPathXmlApplicationContext後,先會將配置位置信息保存到configLocations,供後面解析使用,以後,會調用AbstractApplicationContext的refresh方法進行刷新:函數

public ClassPathXmlApplicationContext(String[] configLocations, boolean refresh,
        ApplicationContext parent) throws BeansException {
 
    super(parent);
    // 保存位置信息,好比`com/springstudy/talentshow/talent-show.xml`
    setConfigLocations(configLocations);
    if (refresh) {
        // 刷新
        refresh();
    }
}
 
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) {
            // Destroy already created singletons to avoid dangling resources.
            destroyBeans();
            // Reset 'active' flag.
            cancelRefresh(ex);
            // Propagate exception to caller.
            throw ex;
        }
    }
}

建立載入BeanFactorypost

protected final void refreshBeanFactory() throws BeansException {
    // ... ...
    DefaultListableBeanFactory beanFactory = createBeanFactory();
    // ... ...
    loadBeanDefinitions(beanFactory);
    // ... ...
}

建立XMLBeanDefinitionReader

protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory)
     throws BeansException, IOException {
    // Create a new XmlBeanDefinitionReader for the given BeanFactory.
    XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);
    // ... ...
    // Allow a subclass to provide custom initialization of the reader,
    // then proceed with actually loading the bean definitions.
    initBeanDefinitionReader(beanDefinitionReader);
    loadBeanDefinitions(beanDefinitionReader);

讀取

建立處理每個resource

public int loadBeanDefinitions(String location, Set<Resource> actualResources)
     throws BeanDefinitionStoreException {
    // ... ...
    // 經過Location來讀取Resource
    Resource[] resources = ((ResourcePatternResolver) resourceLoader).getResources(location);
    int loadCount = loadBeanDefinitions(resources);
    // ... ...
}
 
public int loadBeanDefinitions(Resource... resources) throws BeanDefinitionStoreException {
    Assert.notNull(resources, "Resource array must not be null");
    int counter = 0;
    for (Resource resource : resources) {
        // 載入每個resource
        counter += loadBeanDefinitions(resource);
    }
    return counter;
}

處理XML每一個元素

protected void parseBeanDefinitions(Element root, BeanDefinitionParserDelegate delegate) {
    // ... ...
    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)) {
                // 處理每一個xml中的元素,多是import、alias、bean
                parseDefaultElement(ele, delegate);
            }
            else {
                delegate.parseCustomElement(ele);
            }
        }
    }
    // ... ...
}

解析和註冊bean

protected void processBeanDefinition(Element ele, BeanDefinitionParserDelegate delegate) {
    // 解析
    BeanDefinitionHolder bdHolder = delegate.parseBeanDefinitionElement(ele);
    if (bdHolder != null) {
        bdHolder = delegate.decorateBeanDefinitionIfRequired(ele, bdHolder);
        try {
            // 註冊
            // Register the final decorated instance.
            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));
    }
}

本步驟中,經過parseBeanDefinitionElement將XML的元素解析爲BeanDefinition,而後存在BeanDefinitionHolder中,而後再利用BeanDefinitionHolder將BeanDefinition註冊,實質就是把BeanDefinition的實例put進BeanFactory中,和後面將詳細的介紹解析和註冊過程。

解析

處理每一個Bean的元素

public AbstractBeanDefinition parseBeanDefinitionElement(
        Element ele, String beanName, BeanDefinition containingBean) {
 
    // ... ...
    // 建立beandefinition
    AbstractBeanDefinition bd = createBeanDefinition(className, parent);
 
    parseBeanDefinitionAttributes(ele, beanName, containingBean, bd);
    bd.setDescription(DomUtils.getChildElementValueByTagName(ele, DESCRIPTION_ELEMENT));
 
    parseMetaElements(ele, bd);
    parseLookupOverrideSubElements(ele, bd.getMethodOverrides());
    parseReplacedMethodSubElements(ele, bd.getMethodOverrides());
    // 處理「Constructor」
    parseConstructorArgElements(ele, bd);
    // 處理「Preperty」
    parsePropertyElements(ele, bd);
    parseQualifierElements(ele, bd);
    // ... ...
}

處理屬性的值

public Object parsePropertyValue(Element ele, BeanDefinition bd, String propertyName) {
    String elementName = (propertyName != null) ?
                    "<property> element for property '" + propertyName + "'" :
                    "<constructor-arg> element";
 
    // ... ...
    if (hasRefAttribute) {
    // 處理引用
        String refName = ele.getAttribute(REF_ATTRIBUTE);
        if (!StringUtils.hasText(refName)) {
            error(elementName + " contains empty 'ref' attribute", ele);
        }
        RuntimeBeanReference ref = new RuntimeBeanReference(refName);
        ref.setSource(extractSource(ele));
        return ref;
    }
    else if (hasValueAttribute) {
    // 處理值
        TypedStringValue valueHolder = new TypedStringValue(ele.getAttribute(VALUE_ATTRIBUTE));
        valueHolder.setSource(extractSource(ele));
        return valueHolder;
    }
    else if (subElement != null) {
    // 處理子類型(好比list、map等)
        return parsePropertySubElement(subElement, bd);
    }
    // ... ...
}

1.4 註冊

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);
        }
    }
}
 
public void registerBeanDefinition(String beanName, BeanDefinition beanDefinition)
        throws BeanDefinitionStoreException {
 
    // ......
 
    // 將beanDefinition註冊
    this.beanDefinitionMap.put(beanName, beanDefinition);
 
    // ......
}

註冊過程當中,最核心的一句就是:this.beanDefinitionMap.put(beanName, beanDefinition),也就是說註冊的實質就是以beanName爲key,以beanDefinition爲value,將其put到HashMap中。

註冊

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);
        }
    }
}
 
public void registerBeanDefinition(String beanName, BeanDefinition beanDefinition)
        throws BeanDefinitionStoreException {
 
    // ......
 
    // 將beanDefinition註冊
    this.beanDefinitionMap.put(beanName, beanDefinition);
 
    // ......

理解了以上兩個過程,咱們就能夠本身實現一個簡單的Spring框架了。因而,我根據本身的理解實現了一個簡單的IOC框架Simple Spring,有興趣能夠看看。

註冊過程當中,最核心的一句就是:this.beanDefinitionMap.put(beanName, beanDefinition),也就是說註冊的實質就是以beanName爲key,以beanDefinition爲value,將其put到HashMap中。

注入依賴

當完成初始化IOC容器後,若是bean沒有設置lazy-init(延遲加載)屬性,那麼bean的實例就會在初始化IOC完成以後,及時地進行初始化。初始化時會先創建實例,而後根據配置利用反射對實例進行進一步操做,具體流程以下所示:

建立bean的實例 建立bean的實例過程函數調用棧以下所示:

注入bean的屬性 注入bean的屬性過程函數調用棧以下所示:

在建立bean和注入bean的屬性時,都是在doCreateBean函數中進行的,咱們重點看下:

protected Object doCreateBean(final String beanName, final RootBeanDefinition mbd,
            final Object[] args) {
        // Instantiate the bean.
        BeanWrapper instanceWrapper = null;
        if (mbd.isSingleton()) {
            instanceWrapper = this.factoryBeanInstanceCache.remove(beanName);
        }
        if (instanceWrapper == null) {
            // 建立bean的實例
            instanceWrapper = createBeanInstance(beanName, mbd, args);
        }
     
        // ... ...
     
        // Initialize the bean instance.
        Object exposedObject = bean;
        try {
            // 初始化bean的實例,如注入屬性
            populateBean(beanName, mbd, instanceWrapper);
            if (exposedObject != null) {
                exposedObject = initializeBean(beanName, exposedObject, mbd);
            }
        }
     
        // ... ...
    }

理解了以上兩個過程,咱們就能夠本身實現一個簡單的Spring框架了。因而,我根據本身的理解實現了一個簡單的IOC框架Simple Spring,有興趣能夠看看。

相關文章
相關標籤/搜索