您好,登錄后才能下訂單哦!
本篇內容主要講解“Spring IOC核心流程是什么”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“Spring IOC核心流程是什么”吧!
1. 初始化
大致單步跟了下Spring IOC的初始化過程,整個脈絡很龐大,初始化的過程主要就是讀取XML資源,并解析,最終注冊到Bean Factory中:
在完成初始化的過程后,Bean們就在BeanFactory中蓄勢以待地等調用了。下面通過一個具體的例子,來詳細地學習一下初始化過程,例如當加載下面一個bean:
<bean id="XiaoWang" class="com.springstudy.talentshow.SuperInstrumentalist"> <property name="instruments"> <list> <ref bean="piano"/> <ref bean="saxophone"/> </list> </property> </bean>
加載時需要讀取、解析、注冊bean,這個過程具體的調用棧如下所示:
下面對每一步的關鍵的代碼進行詳細分析:
保存配置位置,并刷新
在調用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; } } }
創建載入BeanFactory
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核心流程是什么”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。