您好,登錄后才能下訂單哦!
本篇內(nèi)容主要講解“Spring IOC核心流程是什么”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“Spring IOC核心流程是什么”吧!
1. 初始化
大致單步跟了下Spring IOC的初始化過程,整個(gè)脈絡(luò)很龐大,初始化的過程主要就是讀取XML資源,并解析,最終注冊到Bean Factory中:
在完成初始化的過程后,Bean們就在BeanFactory中蓄勢以待地等調(diào)用了。下面通過一個(gè)具體的例子,來詳細(xì)地學(xué)習(xí)一下初始化過程,例如當(dāng)加載下面一個(gè)bean:
<bean id="XiaoWang" class="com.springstudy.talentshow.SuperInstrumentalist"> <property name="instruments"> <list> <ref bean="piano"/> <ref bean="saxophone"/> </list> </property> </bean>
加載時(shí)需要讀取、解析、注冊bean,這個(gè)過程具體的調(diào)用棧如下所示:
下面對每一步的關(guān)鍵的代碼進(jìn)行詳細(xì)分析:
保存配置位置,并刷新
在調(diào)用ClassPathXmlApplicationContext后,先會(huì)將配置位置信息保存到configLocations,供后面解析使用,之后,會(huì)調(diào)用AbstractApplicationContext
的refresh方法進(jìn)行刷新:
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; } } }
創(chuàng)建載入BeanFactory
protected final void refreshBeanFactory() throws BeansException { // ... ... DefaultListableBeanFactory beanFactory = createBeanFactory(); // ... ... loadBeanDefinitions(beanFactory); // ... ... }
創(chuàng)建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);
創(chuàng)建處理每一個(gè)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) { // 載入每一個(gè)resource counter += loadBeanDefinitions(resource); } return counter; }
處理XML每個(gè)元素
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)) { // 處理每個(gè)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注冊,實(shí)質(zhì)就是把BeanDefinition的實(shí)例put進(jìn)BeanFactory中,和后面將詳細(xì)的介紹解析和注冊過程。
處理每個(gè)Bean的元素
public AbstractBeanDefinition parseBeanDefinitionElement( Element ele, String beanName, BeanDefinition containingBean) { // ... ... // 創(chuàng)建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),也就是說注冊的實(shí)質(zhì)就是以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); // ......
理解了以上兩個(gè)過程,我們就可以自己實(shí)現(xiàn)一個(gè)簡單的Spring框架了。于是,我根據(jù)自己的理解實(shí)現(xiàn)了一個(gè)簡單的IOC框架Simple Spring,有興趣可以看看。
注冊過程中,最核心的一句就是:this.beanDefinitionMap.put(beanName, beanDefinition)
,也就是說注冊的實(shí)質(zhì)就是以beanName為key,以beanDefinition為value,將其put到HashMap中。
當(dāng)完成初始化IOC容器后,如果bean沒有設(shè)置lazy-init(延遲加載)屬性,那么bean的實(shí)例就會(huì)在初始化IOC完成之后,及時(shí)地進(jìn)行初始化。初始化時(shí)會(huì)先建立實(shí)例,然后根據(jù)配置利用反射對實(shí)例進(jìn)行進(jìn)一步操作,具體流程如下所示:
創(chuàng)建bean的實(shí)例
創(chuàng)建bean的實(shí)例過程函數(shù)調(diào)用棧如下所示:
注入bean的屬性
注入bean的屬性過程函數(shù)調(diào)用棧如下所示:
在創(chuàng)建bean和注入bean的屬性時(shí),都是在doCreateBean函數(shù)中進(jìn)行的,我們重點(diǎn)看下:
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) { // 創(chuàng)建bean的實(shí)例 instanceWrapper = createBeanInstance(beanName, mbd, args); } // ... ... // Initialize the bean instance. Object exposedObject = bean; try { // 初始化bean的實(shí)例,如注入屬性 populateBean(beanName, mbd, instanceWrapper); if (exposedObject != null) { exposedObject = initializeBean(beanName, exposedObject, mbd); } } // ... ... }
到此,相信大家對“Spring IOC核心流程是什么”有了更深的了解,不妨來實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。