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 中自定义标签的详细讲解。小伙伴有不懂的地方可以留言。

 

 

 

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