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());
   }
}

创建配置文件spring.xml




    

   

 如图:
spring初始化解析xml 配置文件一_第1张图片

student类:

@Data
public class Student {
    private String username = "nandao";
}

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文件名称 spring.xml,这里进行模糊匹配,拿到一个占位符,此处不是核心内容,暂时不讲     解,后续再讲
   setConfigLocations(configLocations);
   if (refresh) {
   //重要核心方法,往下看
      refresh();
   }
}

赋值给数组类型  configLocations 参数,父类AbstractRefreshableConfigApplicationContext 处理:后续解析会用到

public void setConfigLocations(@Nullable String... locations) {
		if (locations != null) {
			Assert.noNullElements(locations, "Config locations must not be null");
			this.configLocations = new String[locations.length];
			for (int i = 0; i < locations.length; i++) {
				this.configLocations[i] = resolvePath(locations[i]).trim();
			}
		}
		else {
			this.configLocations = null;
		}
	}

2、进入spring最核心方法,我们主要讲解最重要的方法,不重要的大概解析一下,以后等核心内容讲完,我们在聊这些不重要的辅助方法。为方便大家阅读,每介绍一个方法,我尽量加中文注解

@Override
public void refresh() throws BeansException, IllegalStateException {
   synchronized (this.startupShutdownMonitor) {
      //为容器初始化做准备
      prepareRefresh();//方法1
      /**
        1、创建BeanFactory对象
      * 2、spring 的 xml解析
      *  传统标签解析:bean、import、alias等
      *  自定义标签解析 如: 带前缀 :
      *  自定义标签解析流程:
      *     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();//这里创建 DefaultListableBeanFactory,赋值给成员变量
   return getBeanFactory();//这里返回 DefaultListableBeanFactory 是ConfigurableListableBeanFactory 的实现类
}

我们Ctrl+T 出现两个子类方法,我们应该选哪一个啊?很多初学者都很迷茫!

spring初始化解析xml 配置文件一_第2张图片

答案是: 最初的 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实现.
      //此处默认都是null,业务代码可以自定义实现,类似于修改源码,后期我们会单独分析
      customizeBeanFactory(beanFactory);

      //解析xml,并把xml中的标签封装成BeanDefinition对象
      loadBeanDefinitions(beanFactory);
      synchronized (this.beanFactoryMonitor) {
         this.beanFactory = beanFactory;//赋值给成员变量,对应上面的 return getBeanFactory(); 代码
      }
   }
   catch (IOException ex) {
      throw new ApplicationContextException("I/O error parsing bean definition source for " + getDisplayName(), ex);
   }
}

4、通过对接口loadBeanDefinitions(beanFactory)点击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);
}

查看this:

spring初始化解析xml 配置文件一_第3张图片

点击 beanDefinitionReader.setResourceLoader(this);就发现确实是实现了,当然也可以手动跟踪看看,这里就省略了

	public void setResourceLoader(@Nullable ResourceLoader resourceLoader) {
		this.resourceLoader = resourceLoader;
	}

 查看核心方法:

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);//去抽象的父类 AbstractBeanDefinitionReader 中解析,继续点击
   }
}

来到 AbstractBeanDefinitionReader 类

@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 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;
   }
}

查看文件内容:

spring初始化解析xml 配置文件一_第4张图片

继续解析 

@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 进入子类 XmlBeanDefinitionReader 重写的方法;前面有子类进入父类,这里是父类进入子类,完全吻合
   }
   return count;
}

多个子类看看:

spring初始化解析xml 配置文件一_第5张图片

注意:通用规则是从哪个子类进来的最后又回到哪个子类! 

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 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();
      }
   }
}

加载文件的绝对路径:path 路径

spring初始化解析xml 配置文件一_第6张图片

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委托这个接口的实现类DefaultBeanDefinitionDocumentReader 进行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);
   //这个方法,是标签具体解析过程,点击进入; this.delegate = BeanDefinitionParserDelegate delegate;
   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));
   }
}

先进入 delegate.parseBeanDefinitionElement(ele);

    @Nullable
	public BeanDefinitionHolder parseBeanDefinitionElement(Element ele) {
		return parseBeanDefinitionElement(ele, null);
	}

 来到:

@Nullable
	public BeanDefinitionHolder parseBeanDefinitionElement(Element ele, @Nullable BeanDefinition containingBean) {
		String id = ele.getAttribute(ID_ATTRIBUTE);
		String nameAttr = ele.getAttribute(NAME_ATTRIBUTE);

		List aliases = new ArrayList<>();
		if (StringUtils.hasLength(nameAttr)) {
			String[] nameArr = StringUtils.tokenizeToStringArray(nameAttr, MULTI_VALUE_ATTRIBUTE_DELIMITERS);
			aliases.addAll(Arrays.asList(nameArr));
		}

		String beanName = id;
		if (!StringUtils.hasText(beanName) && !aliases.isEmpty()) {
			beanName = aliases.remove(0);
			if (logger.isTraceEnabled()) {
				logger.trace("No XML 'id' specified - using '" + beanName +
						"' as bean name and " + aliases + " as aliases");
			}
		}

		//检查beanName是否重复
		if (containingBean == null) {
			checkNameUniqueness(beanName, aliases, ele);
		}

		//标签解析的核心方法,重要程度5
		AbstractBeanDefinition beanDefinition = parseBeanDefinitionElement(ele, beanName, containingBean);
		if (beanDefinition != null) {
			if (!StringUtils.hasText(beanName)) {
				try {
					if (containingBean != null) {
						beanName = BeanDefinitionReaderUtils.generateBeanName(
								beanDefinition, this.readerContext.getRegistry(), true);
					}
					else {
						beanName = this.readerContext.generateBeanName(beanDefinition);
						// Register an alias for the plain bean class name, if still possible,
						// if the generator returned the class name plus a suffix.
						// This is expected for Spring 1.2/2.0 backwards compatibility.
						String beanClassName = beanDefinition.getBeanClassName();
						if (beanClassName != null &&
								beanName.startsWith(beanClassName) && beanName.length() > beanClassName.length() &&
								!this.readerContext.getRegistry().isBeanNameInUse(beanClassName)) {
							aliases.add(beanClassName);
						}
					}
					if (logger.isTraceEnabled()) {
						logger.trace("Neither XML 'id' nor 'name' specified - " +
								"using generated bean name [" + beanName + "]");
					}
				}
				catch (Exception ex) {
					error(ex.getMessage(), ele);
					return null;
				}
			}
			String[] aliasesArray = StringUtils.toStringArray(aliases);
			return new BeanDefinitionHolder(beanDefinition, beanName, aliasesArray);
		}

		return null;
	}

进入 parseBeanDefinitionElement(ele, beanName, containingBean);

@Nullable
	public AbstractBeanDefinition parseBeanDefinitionElement(
			Element ele, String beanName, @Nullable BeanDefinition containingBean) {

		this.parseState.push(new BeanEntry(beanName));

		String className = null;
		if (ele.hasAttribute(CLASS_ATTRIBUTE)) {
			className = ele.getAttribute(CLASS_ATTRIBUTE).trim();
		}
		String parent = null;
		if (ele.hasAttribute(PARENT_ATTRIBUTE)) {
			parent = ele.getAttribute(PARENT_ATTRIBUTE);
		}

		try {
			//创建GenericBeanDefinition对象
			AbstractBeanDefinition bd = createBeanDefinition(className, parent);

			//解析bean标签的属性,并把解析出来的属性设置到BeanDefinition对象中
			parseBeanDefinitionAttributes(ele, beanName, containingBean, bd);
			bd.setDescription(DomUtils.getChildElementValueByTagName(ele, DESCRIPTION_ELEMENT));

			//解析bean中的meta标签
			parseMetaElements(ele, bd);
			//解析bean中的lookup-method标签  重要程度:2,可看可不看
			parseLookupOverrideSubElements(ele, bd.getMethodOverrides());
			//解析bean中的replaced-method标签  重要程度:2,可看可不看
			parseReplacedMethodSubElements(ele, bd.getMethodOverrides());

			//解析bean中的constructor-arg标签  重要程度:2,可看可不看
			parseConstructorArgElements(ele, bd);
			//解析bean中的property标签  重要程度:2,可看可不看
			parsePropertyElements(ele, bd);
			//可以不看,用不到
			parseQualifierElements(ele, bd);

			bd.setResource(this.readerContext.getResource());
			bd.setSource(extractSource(ele));

			return bd;
		}
		catch (ClassNotFoundException ex) {
			error("Bean class [" + className + "] not found", ele, ex);
		}
		catch (NoClassDefFoundError err) {
			error("Class that bean class [" + className + "] depends on not found", ele, err);
		}
		catch (Throwable ex) {
			error("Unexpected failure during bean definition parsing", ele, ex);
		}
		finally {
			this.parseState.pop();
		}

		return null;
	}

图示参数:

 spring初始化解析xml 配置文件一_第7张图片

继续

 spring初始化解析xml 配置文件一_第8张图片

进入: createBeanDefinition(className, parent);

protected AbstractBeanDefinition createBeanDefinition(@Nullable String className, @Nullable String parentName)
			throws ClassNotFoundException {

		return BeanDefinitionReaderUtils.createBeanDefinition(
				parentName, className, this.readerContext.getBeanClassLoader());
	}

继续:

public static AbstractBeanDefinition createBeanDefinition(
			@Nullable String parentName, @Nullable String className, @Nullable ClassLoader classLoader) throws ClassNotFoundException {

		GenericBeanDefinition bd = new GenericBeanDefinition();
		bd.setParentName(parentName);
		if (className != null) {
			if (classLoader != null) {
				bd.setBeanClass(ClassUtils.forName(className, classLoader));
			}
			else {
				bd.setBeanClassName(className);
			}
		}
		return bd;
	}

参数展示:

 spring初始化解析xml 配置文件一_第9张图片

如图参数显示:

spring初始化解析xml 配置文件一_第10张图片

//解析bean标签的属性,并把解析出来的属性设置到BeanDefinition对象中
parseBeanDefinitionAttributes(ele, beanName, containingBean, bd);

public AbstractBeanDefinition parseBeanDefinitionAttributes(Element ele, String beanName,
			@Nullable BeanDefinition containingBean, AbstractBeanDefinition bd) {

		if (ele.hasAttribute(SINGLETON_ATTRIBUTE)) {
			error("Old 1.x 'singleton' attribute in use - upgrade to 'scope' declaration", ele);
		}
		else if (ele.hasAttribute(SCOPE_ATTRIBUTE)) {
			bd.setScope(ele.getAttribute(SCOPE_ATTRIBUTE));
		}
		else if (containingBean != null) {
			// Take default from containing bean in case of an inner bean definition.
			bd.setScope(containingBean.getScope());
		}

		if (ele.hasAttribute(ABSTRACT_ATTRIBUTE)) {
			bd.setAbstract(TRUE_VALUE.equals(ele.getAttribute(ABSTRACT_ATTRIBUTE)));
		}

		String lazyInit = ele.getAttribute(LAZY_INIT_ATTRIBUTE);
		if (isDefaultValue(lazyInit)) {
			lazyInit = this.defaults.getLazyInit();
		}
		bd.setLazyInit(TRUE_VALUE.equals(lazyInit));

		String autowire = ele.getAttribute(AUTOWIRE_ATTRIBUTE);
		bd.setAutowireMode(getAutowireMode(autowire));

		if (ele.hasAttribute(DEPENDS_ON_ATTRIBUTE)) {
			String dependsOn = ele.getAttribute(DEPENDS_ON_ATTRIBUTE);
			bd.setDependsOn(StringUtils.tokenizeToStringArray(dependsOn, MULTI_VALUE_ATTRIBUTE_DELIMITERS));
		}

		String autowireCandidate = ele.getAttribute(AUTOWIRE_CANDIDATE_ATTRIBUTE);
		if (isDefaultValue(autowireCandidate)) {
			String candidatePattern = this.defaults.getAutowireCandidates();
			if (candidatePattern != null) {
				String[] patterns = StringUtils.commaDelimitedListToStringArray(candidatePattern);
				bd.setAutowireCandidate(PatternMatchUtils.simpleMatch(patterns, beanName));
			}
		}
		else {
			bd.setAutowireCandidate(TRUE_VALUE.equals(autowireCandidate));
		}

		if (ele.hasAttribute(PRIMARY_ATTRIBUTE)) {
			bd.setPrimary(TRUE_VALUE.equals(ele.getAttribute(PRIMARY_ATTRIBUTE)));
		}

		if (ele.hasAttribute(INIT_METHOD_ATTRIBUTE)) {
			String initMethodName = ele.getAttribute(INIT_METHOD_ATTRIBUTE);
			bd.setInitMethodName(initMethodName);
		}
		else if (this.defaults.getInitMethod() != null) {
			bd.setInitMethodName(this.defaults.getInitMethod());
			bd.setEnforceInitMethod(false);
		}

		if (ele.hasAttribute(DESTROY_METHOD_ATTRIBUTE)) {
			String destroyMethodName = ele.getAttribute(DESTROY_METHOD_ATTRIBUTE);
			bd.setDestroyMethodName(destroyMethodName);
		}
		else if (this.defaults.getDestroyMethod() != null) {
			bd.setDestroyMethodName(this.defaults.getDestroyMethod());
			bd.setEnforceDestroyMethod(false);
		}

		if (ele.hasAttribute(FACTORY_METHOD_ATTRIBUTE)) {
			bd.setFactoryMethodName(ele.getAttribute(FACTORY_METHOD_ATTRIBUTE));
		}
		if (ele.hasAttribute(FACTORY_BEAN_ATTRIBUTE)) {
			bd.setFactoryBeanName(ele.getAttribute(FACTORY_BEAN_ATTRIBUTE));
		}

		return bd;
	}

参数展示:

spring初始化解析xml 配置文件一_第11张图片

6、来到 BeanDefinitionReaderUtils 工具类 

public static void registerBeanDefinition(
      BeanDefinitionHolder definitionHolder, BeanDefinitionRegistry registry)
      throws BeanDefinitionStoreException {
   String beanName = definitionHolder.getBeanName();//取出BeanDefinition 的名称
   //完成BeanDefinition的注册, ctrl + T 点击进入其子类 DefaultListableBeanFactory 
   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);
      }
   }
}

查看子类,显然进入第一个,因为上游创建的就是第一个

spring初始化解析xml 配置文件一_第12张图片

参数如图:

spring初始化解析xml 配置文件一_第13张图片

建立映射参数:

spring初始化解析xml 配置文件一_第14张图片

 

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 updatedDefinitions = new ArrayList<>(this.beanDefinitionNames.size() + 1);
            updatedDefinitions.addAll(this.beanDefinitionNames);
            updatedDefinitions.add(beanName);
            this.beanDefinitionNames = updatedDefinitions;
            if (this.manualSingletonNames.contains(beanName)) {
               Set 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);
   }
}

参数查看:

spring初始化解析xml 配置文件一_第15张图片

xml 文件解析生成beanDefinition,走到这里算告一段落,下一篇我们讲解xml 中自定义标签的详细讲解。小伙伴有不懂的地方可以留言。

你可能感兴趣的:(spring源码学习,spring)