spring初始化解析xml 配置文件一

從本篇開始我們正式進入spring源碼學習,首先從我們最熟悉的解析xml 配置文件開始。依次跟蹤代碼,緊接着上一篇的開始,如下,創建一個測試方法:

import com.xiangxue.jack.bean.Student;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {
   @Test
   public void test1() {
     //從這裏入口,點擊 ClassPathXmlApplicationContext  查看初始化的過程
      ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
      Student student = (Student)applicationContext.getBean("student");
      System.out.println(student.getUsername());
   }
}

1、點擊兩次進入其構造方法,約定一下,今天我們只講解重點核心流程,因爲spring 源碼內容非常多,我們從最核心的代碼開始

public ClassPathXmlApplicationContext(String configLocation) throws BeansException {
   //這是另一個多參構造方法的引用,點擊後進入下一個構造方法
   this(new String[] {configLocation}, true, null);
}
public ClassPathXmlApplicationContext(
      String[] configLocations, boolean refresh, @Nullable ApplicationContext parent)
      throws BeansException {
   //首先初始化父類
   super(parent);

   //創建解析器,解析configLocations 也就是傳過來的xml文件名稱,這裏進行模糊匹配,拿到一個佔位符,此處不是核心內容,暫時不講     解,後續再講
   setConfigLocations(configLocations);
   if (refresh) {
   //重要核心方法,往下看
      refresh();
   }
}

2、進入spring最核心方法,我們主要講解最重要的方法,不重要的大概解析一下,以後等核心內容講完,我們在聊這些不重要的輔助方法。爲方便大家閱讀,每介紹一個方法,我儘量加中文註解

@Override
public void refresh() throws BeansException, IllegalStateException {
   synchronized (this.startupShutdownMonitor) {
      //爲容器初始化做準備
      prepareRefresh();//方法1
      /**
        1、創建BeanFactory對象
      * 2、spring 的 xml解析
      *  傳統標籤解析:bean、import、alias等
      *  自定義標籤解析 如:<context:component-scan base-package="com.xiangxue.jack"/> 帶前綴 :
      *  自定義標籤解析流程:
      *     a、根據當前解析標籤的頭信息找到對應的namespaceUri
      *     b、加載spring所以jar中的spring.handlers文件,並建立映射關係,這裏是SPI設計思想
      *     c、根據namespaceUri從映射關係中找到對應的實現了NamespaceHandler接口的類
      *     d、調用類的init方法,init方法是註冊了各種自定義標籤的解析類
      *     e、根據namespaceUri找到對應的解析類,然後調用paser方法完成標籤解析
      * 3、把解析出來的xml標籤封裝成BeanDefinition對象
      * */
      ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();//方法2
      /** 給beanFactory設置一些屬性值* */
      prepareBeanFactory(beanFactory);//方法3
      try {
         postProcessBeanFactory(beanFactory);//方法4
         /*
         * BeanDefinitionRegistryPostProcessor
         * BeanFactoryPostProcessor
         * 完成對這兩個接口的調用
         * */
         invokeBeanFactoryPostProcessors(beanFactory);//方法5

         // 把實現了BeanPostProcessor接口的類進行實例化,並且加入到BeanFactory中
         registerBeanPostProcessors(beanFactory);//方法6

          //國際化
         initMessageSource();//方法7

         //初始化事件管理類
         initApplicationEventMulticaster();//方法8

         //這個方法着重理解模板設計模式,因爲在springboot中,這個方法是用來做內嵌tomcat啓動的
         // Initialize other special beans in specific context subclasses.
         onRefresh();//方法9

          //往事件管理類中註冊事件類
         // Check for listener beans and register them.
         registerListeners();//方法10

         /*
         * 這個方法是spring中最重要的方法
         * 1、bean實例化過程
         * 2、ioc
         * 3、註解支持
         * 4、BeanPostProcessor的執行
         * 5、Aop的入口
         * */
         // Instantiate all remaining (non-lazy-init) singletons.
         finishBeanFactoryInitialization(beanFactory);//方法11

         // Last step: publish corresponding event.
         finishRefresh();//方法12
      }
      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();
      }
   }
}

方法1爲容器初始化做準備,方法2 AbstractApplicationContext 中創建BeanFactory我們詳細進入看一下

protected ConfigurableListableBeanFactory obtainFreshBeanFactory() {
   //核心方法,子類去實現,這是模板模式的一種鉤子方法,父類是個抽象的方法。
  //protected abstract void refreshBeanFactory() throws BeansException, IllegalStateException;
   refreshBeanFactory();
   return getBeanFactory();
}

我們Ctrl+T 出現兩個子類方法,我們應該選哪一個啊?很多初學者都很迷茫!

答案是: 最初的 ClassPathXmlApplicationContext  類 直接或間接繼承了哪個類,就進入哪個類!顯然,繼承了

AbstractRefreshableApplicationContext 這個類,此類對 refreshBeanFactory() 方法進行了重寫。進入後顯示

3、改方法中loadBeanDefinitions(beanFactory) 最重要,解析xml文件,這也是一個需要子類實現的抽象方法;

@Override
protected final void refreshBeanFactory() throws BeansException {
   //如果BeanFactory不爲空,則清除BeanFactory和裏面的實例
   if (hasBeanFactory()) {
      destroyBeans();
      closeBeanFactory();
   }
   try {
      //創建DefaultListableBeanFactory
      //BeanFactory 實例工廠
      DefaultListableBeanFactory beanFactory = createBeanFactory();
      beanFactory.setSerializationId(getId());

      //設置是否可以循環依賴 allowCircularReferences
      //是否允許使用相同名稱重新註冊不同的bean實現.
      customizeBeanFactory(beanFactory);

      //解析xml,並把xml中的標籤封裝成BeanDefinition對象
      loadBeanDefinitions(beanFactory);
      synchronized (this.beanFactoryMonitor) {
         this.beanFactory = beanFactory;
      }
   }
   catch (IOException ex) {
      throw new ApplicationContextException("I/O error parsing bean definition source for " + getDisplayName(), ex);
   }
}

4、ctrl+T 進入 AbstractXmlApplicationContext 字 類。

@Override
protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws BeansException, IOException {
   // Create a new XmlBeanDefinitionReader for the given BeanFactory.
   //創建xml的解析器,這裏是一個委託模式,後面我們會專門講設計模式,委託beanDefinitionReader 解析xml
   XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);

   // Configure the bean definition reader with this context's
   // resource loading environment.
   beanDefinitionReader.setEnvironment(this.getEnvironment());

   //這裏傳一個this進去,因爲ApplicationContext是實現了ResourceLoader接口的
   beanDefinitionReader.setResourceLoader(this);
   beanDefinitionReader.setEntityResolver(new ResourceEntityResolver(this));

   // Allow a subclass to provide custom initialization of the reader,
   // then proceed with actually loading the bean definitions.
   initBeanDefinitionReader(beanDefinitionReader);

   //主要看這個方法 ,點擊進入下面
   loadBeanDefinitions(beanDefinitionReader);
}
protected void loadBeanDefinitions(XmlBeanDefinitionReader reader) throws BeansException, IOException {
   Resource[] configResources = getConfigResources();
   if (configResources != null) {
      reader.loadBeanDefinitions(configResources);
   }
   //獲取需要加載的xml配置文件 
   String[] configLocations = getConfigLocations();
   if (configLocations != null) {
      reader.loadBeanDefinitions(configLocations);//去解析,繼續點擊
   }
}
@Override
public int loadBeanDefinitions(String... locations) throws BeanDefinitionStoreException {
   Assert.notNull(locations, "Location array must not be null");
   int count = 0;
   //配置文件有多個,加載多個配置文件
   for (String location : locations) {
      count += loadBeanDefinitions(location);//點擊進入
   }
   return count;
}
@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) {
      // Resource pattern matching available.
      try {
         //把字符串類型的xml文件路徑,形如:classpath*:user/**/*-context.xml,轉換成Resource對象類型,其實就是用流
         //的方式加載配置文件,然後封裝成Resource對象,
         Resource[] resources = ((ResourcePatternResolver) resourceLoader).getResources(location);

         //看這個方法 5
         int count = loadBeanDefinitions(resources);//繼續點擊
         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 {
      // Can only load single resources by absolute 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;
   }
}
@Override
public int loadBeanDefinitions(Resource... resources) throws BeanDefinitionStoreException {
   Assert.notNull(resources, "Resource array must not be null");
   int count = 0;
   for (Resource resource : resources) {
      //模板設計模式,調用到子類中的方法
      count += loadBeanDefinitions(resource);//ctrl+T 進入子類重寫的方法
   }
   return count;
}

4、XmlBeanDefinitionReader  子類

@Override
public int loadBeanDefinitions(Resource resource) throws BeanDefinitionStoreException {
   //EncodedResource帶編碼的對Resource對象的封裝 點擊進入
   return loadBeanDefinitions(new EncodedResource(resource));
}
public int loadBeanDefinitions(EncodedResource encodedResource) throws BeanDefinitionStoreException {
   Assert.notNull(encodedResource, "EncodedResource must not be null");
   if (logger.isTraceEnabled()) {
      logger.trace("Loading XML bean definitions from " + encodedResource);
   }
   Set<EncodedResource> currentResources = this.resourcesCurrentlyBeingLoaded.get();
   if (currentResources == null) {
      currentResources = new HashSet<>(4);
      this.resourcesCurrentlyBeingLoaded.set(currentResources);
   }
   if (!currentResources.add(encodedResource)) {
      throw new BeanDefinitionStoreException(
            "Detected cyclic loading of " + encodedResource + " - check your import definitions!");
   }
   try {
      //獲取Resource對象中的xml文件流對象
      InputStream inputStream = encodedResource.getResource().getInputStream();
      try {
         //InputSource是jdk中的sax xml文件解析對象
         InputSource inputSource = new InputSource(inputStream);
         if (encodedResource.getEncoding() != null) {
            inputSource.setEncoding(encodedResource.getEncoding());
         }
         //主要看這個核心方法  點擊進入 
         return doLoadBeanDefinitions(inputSource, encodedResource.getResource());
      }
      finally {
         inputStream.close();
      }
   }
   catch (IOException ex) {
      throw new BeanDefinitionStoreException(
            "IOException parsing XML document from " + encodedResource.getResource(), ex);
   }
   finally {
      currentResources.remove(encodedResource);
      if (currentResources.isEmpty()) {
         this.resourcesCurrentlyBeingLoaded.remove();
      }
   }
}
protected int doLoadBeanDefinitions(InputSource inputSource, Resource resource)
      throws BeanDefinitionStoreException {

   try {
      //把inputSource 封裝成Document文件對象,這是jdk的API
      Document doc = doLoadDocument(inputSource, resource);

      //主要看這個方法,根據解析出來的document對象,拿到裏面的標籤元素封裝成BeanDefinition
      int count = registerBeanDefinitions(doc, resource);
      if (logger.isDebugEnabled()) {
         logger.debug("Loaded " + count + " bean definitions from " + resource);
      }
      return count;
   }
   //刪除了部分無用代碼
}
public int registerBeanDefinitions(Document doc, Resource resource) throws BeanDefinitionStoreException {
   //委託模式,BeanDefinitionDocumentReader委託這個類進行document的解析
   BeanDefinitionDocumentReader documentReader = createBeanDefinitionDocumentReader();
   int countBefore = getRegistry().getBeanDefinitionCount();
   //主要看這個方法,createReaderContext(resource) XmlReaderContext上下文,封裝了XmlBeanDefinitionReader對象
   documentReader.registerBeanDefinitions(doc, createReaderContext(resource));
   return getRegistry().getBeanDefinitionCount() - countBefore;
}

5、ctrl+T   registerBeanDefinitions  進入方法的實現類  DefaultBeanDefinitionDocumentReader

@Override
public void registerBeanDefinitions(Document doc, XmlReaderContext readerContext) {
   this.readerContext = readerContext;
   //主要看這個方法,把root節點傳進去,解析每個節點
   doRegisterBeanDefinitions(doc.getDocumentElement());
}
protected void doRegisterBeanDefinitions(Element root) {
   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);    
         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);
   postProcessXml(root);
   this.delegate = parent;
}
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標籤,也是最常用標籤 ,進入詳細看
   else if (delegate.nodeNameEquals(ele, BEAN_ELEMENT)) {
      processBeanDefinition(ele, delegate);
   }
   else if (delegate.nodeNameEquals(ele, NESTED_BEANS_ELEMENT)) {
      // recurse
      doRegisterBeanDefinitions(ele);
   }
}
protected void processBeanDefinition(Element ele, BeanDefinitionParserDelegate delegate) {
   // 解析document,封裝成BeanDefinition
   BeanDefinitionHolder bdHolder = delegate.parseBeanDefinitionElement(ele);
   if (bdHolder != null) {
      //該方法用裝飾者設計模式,加上SPI設計思想,裏面有屬性標籤,可以看看
      bdHolder = delegate.decorateBeanDefinitionIfRequired(ele, bdHolder);
      try {
         //完成document到BeanDefinition對象轉換後,對BeanDefinition對象進行緩存註冊,重點分析,點擊進入
         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));
   }
}

6、來到 BeanDefinitionReaderUtils 工具類 

public static void registerBeanDefinition(
      BeanDefinitionHolder definitionHolder, BeanDefinitionRegistry registry)
      throws BeanDefinitionStoreException {
   String beanName = definitionHolder.getBeanName();//取出BeanDefinition 的名稱
   //完成BeanDefinition的註冊, ctrl + T 點擊進入
   registry.registerBeanDefinition(beanName, definitionHolder.getBeanDefinition());
   //建立別名和 id的映射,這樣就可以根據別名獲取到id
   // Register aliases for bean name, if any.
   String[] aliases = definitionHolder.getAliases();
   if (aliases != null) {
      for (String alias : aliases) {
         registry.registerAlias(beanName, alias);
      }
   }
}

7、進入 DefaultListableBeanFactory 

@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是否已經註冊
   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;
            if (this.manualSingletonNames.contains(beanName)) {
               Set<String> updatedSingletons = new LinkedHashSet<>(this.manualSingletonNames);
               updatedSingletons.remove(beanName);
               this.manualSingletonNames = updatedSingletons;
            }
         }
      }
      else {
         //核心業務主要在這裏、把beanDefinition緩存到map,後續實例化的時候用
         this.beanDefinitionMap.put(beanName, beanDefinition);
         //把beanName放到beanDefinitionNames list中,這個list着重記住,bean實例化的時候需要用到
         this.beanDefinitionNames.add(beanName);
         this.manualSingletonNames.remove(beanName);
      }
      this.frozenBeanDefinitionNames = null;
   }
   if (existingDefinition != null || containsSingleton(beanName)) {
      resetBeanDefinition(beanName);
   }
}

xml 文件解析走到這裏算告一段落,下一篇我們講解xml 中自定義標籤的詳細講解。小夥伴有不懂的地方可以留言。

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章