<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                [TOC] ## Spring IOC 實現原理簡要解析 ### 1\. 初始化 大致單步跟了下Spring IOC的初始化過程,整個脈絡很龐大,初始化的過程主要就是**讀取XML資源,并解析,最終注冊到Bean Factory中**: ![flow](https://cloud.githubusercontent.com/assets/1736354/7897341/032179be-070b-11e5-9ecf-d7befc804e9d.png) 在完成初始化的過程后,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,這個過程具體的調用棧如下所示: ![load](https://cloud.githubusercontent.com/assets/1736354/7896285/8a488060-06e6-11e5-9ad9-4ddd3375984f.png) 下面對每一步的關鍵的代碼進行詳細分析: #### 1.1 準備 **保存配置位置,并刷新** 在調用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); } ``` #### 1.2 讀取 **創建處理每一個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)); } } ``` #### 1.3 解析 ![process](https://cloud.githubusercontent.com/assets/1736354/7896302/eae02bc6-06e6-11e5-941a-d1f59e3b363f.png) **處理每個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中。 ### 2\. 注入依賴 當完成初始化IOC容器后,如果bean沒有設置lazy-init(延遲加載)屬性,那么bean的實例就會在初始化IOC完成之后,及時地進行初始化。初始化時會先建立實例,然后根據配置利用反射對實例進行進一步操作,具體流程如下所示: ![bean_flow](https://cloud.githubusercontent.com/assets/1736354/7929429/615570ea-0930-11e5-8097-ae982ef7709d.png) **創建bean的實例** 創建bean的實例過程函數調用棧如下所示: ![create_bean](https://cloud.githubusercontent.com/assets/1736354/7929379/cec01bcc-092f-11e5-81ad-88c285f33845.png) **注入bean的屬性** 注入bean的屬性過程函數調用棧如下所示: ![inject_property](https://cloud.githubusercontent.com/assets/1736354/7929381/db58350e-092f-11e5-82a4-caaf349291ea.png) 在創建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框架了。 參考資料: [Spring-IOC核心源碼學習](https://yikun.github.io/2015/05/29/Spring-IOC%E6%A0%B8%E5%BF%83%E6%BA%90%E7%A0%81%E5%AD%A6%E4%B9%A0/) [深入Spring IOC 容器源碼分析](https://javadoop.com/post/spring-ioc)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看