spring源码系列三-obtainFreshBeanFactory()

引言

1.spring源码中refresh()方法包含的13个主要的方法。obtainFreshBeanFactory()方法为其中之一显得尤为重要。
2.obtainFreshBeanFactory()方法主要作用:是创建BeanFactory对象,并解析xml封装成BeanDefinition对象。
3.obtainFreshBeanFactory()内部方法调用链比较长,如下:
在这里插入图片描述

行文中均以比较重要的方法代码片段为例,有些次要方法没有详细展开

一、obtainFreshBeanFactory()–>refreshBeanFactory()

(1) refreshBeanFactory()包含的主要方法及作用:

  • createBeanFactory():创建beanFactory(DefaultListableBeanFactory类型)
  • setSerializationId:设置序列化Id
  • customizeBeanFactory():定制beanFactory
  • loadBeanDefinitions():加载bean定义

(2) refreshBeanFactory源码:

public abstract class AbstractRefreshableApplicationContext extends AbstractApplicationContext {
...
	protected final void refreshBeanFactory() throws BeansException {
			//判断是否存在beanFactory
			if (hasBeanFactory()) {
		    // 如果存在注销所有的单例
			destroyBeans();
			//重置beanFactory
			try {
				//创建beanFactory--》DefaultListableBeanFactory
				DefaultListableBeanFactory beanFactory = createBeanFactory();
				//设置序列化Id
				beanFactory.setSerializationId(getId());
                //设置存在多个重名bean时是否覆盖;设置是否允许循环引用,定制beanFactory
				customizeBeanFactory(beanFactory);
				//解析xml,并把xml中标签封装成BeanDefinition对象;
				loadBeanDefinitions(beanFactory);
				this.beanFactory = beanFactory;
			}
			catch (IOException ex) {
				throw new ApplicationContextException("I/O error parsing bean definition source for " + getDisplayName(), ex);
			}
		}
}
...

1、refreshBeanFactory()–>customizeBeanFactory(beanFactory)

protected void customizeBeanFactory(DefaultListableBeanFactory beanFactory) {
    //是否允许覆盖同名称的不同定义的对象
	if (this.allowBeanDefinitionOverriding != null) {
	    // 如果属性allowBeanDefinitionOverriding不为空,设置给beanFactory对象相应属性
		beanFactory.setAllowBeanDefinitionOverriding(this.allowBeanDefinitionOverriding);
	}
	//是否允许bean之间存在循环依赖
	if (this.allowCircularReferences != null) {
	    // 如果属性allowCircularReferences不为空,设置给beanFactory对象相应属性
		beanFactory.setAllowCircularReferences(this.allowCircularReferences);
	}
}

2、refreshBeanFactory()–>loadBeanDefinitions(beanFactory)

2.1 loadBeanDefinitions(beanFactory)

XmlWebApplicationContext 类中的loadBeanDefinitions:

public class XmlWebApplicationContext extends AbstractRefreshableWebApplicationContext {
...
	 protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws BeansException, IOException {
	        // 1.为指定BeanFactory创建XmlBeanDefinitionReader
	        XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);
	        // 2.使用此上下文的资源加载环境配置 XmlBeanDefinitionReader
	        beanDefinitionReader.setEnvironment(this.getEnvironment());
	        // resourceLoader赋值为XmlWebApplicationContext
	        beanDefinitionReader.setResourceLoader(this);
	        beanDefinitionReader.setEntityResolver(new ResourceEntityResolver(this));
	        this.initBeanDefinitionReader(beanDefinitionReader);
	        // 3.加载 bean 定义
	        this.loadBeanDefinitions(beanDefinitionReader);
	    }
...
}

从上图中第三处XmlWebApplicationContext 中的loadBeanDefinitions点进去–》注意接下来好多方法里都叫loadBeanDefinitions,层层点进去跟踪:

public class XmlWebApplicationContext extends AbstractRefreshableWebApplicationContext {
...
  protected void loadBeanDefinitions(XmlBeanDefinitionReader reader) throws IOException {
  		//1.获取配置文件路径
        String[] configLocations = this.getConfigLocations();
        if (configLocations != null) {
            String[] var3 = configLocations;
            int var4 = configLocations.length;
            for(int var5 = 0; var5 < var4; ++var5) {
                String configLocation = var3[var5];
                //2.根据配置文件路径加载 bean 定义
                reader.loadBeanDefinitions(configLocation);
            }
        }
    }
 ...
 }

层层点进去当看到XmlBeanDefinitionReader 中的doLoadBeanDefinitions(),要注意spring源码中但凡do开头的都是实际做事的!!!一定要走进去看:

public class XmlBeanDefinitionReader extends AbstractBeanDefinitionReader {
...
		public int loadBeanDefinitions(EncodedResource encodedResource) throws BeanDefinitionStoreException {
		    Assert.notNull(encodedResource, "EncodedResource must not be null");
		    if (logger.isInfoEnabled()) {
		        logger.info("Loading XML bean definitions from " + encodedResource.getResource());
		    }
		 
		    // 1.当前正在加载的EncodedResource
		    Set<EncodedResource> currentResources = this.resourcesCurrentlyBeingLoaded.get();
		    if (currentResources == null) {
		        currentResources = new HashSet<EncodedResource>(4);
		        this.resourcesCurrentlyBeingLoaded.set(currentResources);
		    }
		    // 2.将当前encodedResource添加到currentResources
		    if (!currentResources.add(encodedResource)) {
		        // 如果添加失败,代表当前的encodedResource已经存在,则表示出现了循环加载
		        throw new BeanDefinitionStoreException(
		                "Detected cyclic loading of " + encodedResource + " - check your import definitions!");
		    }
		    try {
		        // 3.拿到Resource的inputStream
		        InputStream inputStream = encodedResource.getResource().getInputStream();
		        try {
		            // 4.将inputStream封装成org.xml.sax.InputSource
		            InputSource inputSource = new InputSource(inputStream);
		            if (encodedResource.getEncoding() != null) {
		                inputSource.setEncoding(encodedResource.getEncoding());
		            }
		            // 5.加载 bean 定义(方法以do开头,真正处理的方法)
		            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();
		        }
		    }
    ...
}

2.2 doLoadBeanDefinitions()

(1)我们看到doLoadBeanDefinitions()方法包含了几个重要方法:doLoadDocument()registerBeanDefinitions()

  • doLoadDocument():创建beanFactory(DefaultListableBeanFactory类型)
  • registerBeanDefinitions():解析xml,并把xml中标签封装成BeanDefinition对象

(2)doLoadBeanDefinitions源码:

public class XmlBeanDefinitionReader extends AbstractBeanDefinitionReader {
...
		protected int doLoadBeanDefinitions(InputSource inputSource, Resource resource)
		        throws BeanDefinitionStoreException {
		    try {
		        // 1.根据inputSource和resource加载XML文件,并封装成Document
		        Document doc = doLoadDocument(inputSource, resource);
		        // 2.根据返回的Document注册Bean信息(对配置文件的解析,核心逻辑)
		        return registerBeanDefinitions(doc, resource);
		    } catch (BeanDefinitionStoreException ex) {
		        throw ex;
		    } catch (SAXParseException ex) {
		        throw new XmlBeanDefinitionStoreException(resource.getDescription(),
		                "Line " + ex.getLineNumber() + " in XML document from " + resource + " is invalid", ex);
		    } catch (SAXException ex) {
		        throw new XmlBeanDefinitionStoreException(resource.getDescription(),
		                "XML document from " + resource + " is invalid", ex);
		    } catch (ParserConfigurationException ex) {
		        throw new BeanDefinitionStoreException(resource.getDescription(),
		                "Parser configuration exception parsing XML from " + resource, ex);
		    } catch (IOException ex) {
		        throw new BeanDefinitionStoreException(resource.getDescription(),
		                "IOException parsing XML document from " + resource, ex);
		    } catch (Throwable ex) {
		        throw new BeanDefinitionStoreException(resource.getDescription(),
		                "Unexpected exception parsing XML document from " + resource, ex);
		    }
		}
...
}

(3)继续找do开头的方法doLoadDocument()

XML配置常见的验证模式有两种:DTD 和 XSD

protected Document doLoadDocument(InputSource inputSource, Resource resource) throws Exception {
    // 1.getValidationModeForResource(resource): 获取XML配置文件的验证模式
    // 2.documentLoader.loadDocument: 加载XML文件,并得到对应的 Document
    return this.documentLoader.loadDocument(inputSource, getEntityResolver(), this.errorHandler,
            getValidationModeForResource(resource), isNamespaceAware());
}
 
protected int getValidationModeForResource(Resource resource) {
    int validationModeToUse = getValidationMode();
    // 1.1 如果手动指定了XML文件的验证模式则使用指定的验证模式
    if (validationModeToUse != VALIDATION_AUTO) {
        return validationModeToUse;
    }
    // 1.2 如果未指定则使用自动检测
    int detectedMode = detectValidationMode(resource);
    // 1.3 如果检测出的验证模式不为 VALIDATION_AUTO, 则返回检测出来的验证模式
    if (detectedMode != VALIDATION_AUTO) {
        return detectedMode;
    }
    // Hmm, we didn't get a clear indication... Let's assume XSD,
    // since apparently no DTD declaration has been found up until
    // detection stopped (before finding the document's root tag).
    // 1.4 如果最终没找到验证模式,则使用 XSD
    return VALIDATION_XSD;
}
 
protected int detectValidationMode(Resource resource) {
    // 1.2.1 校验resource是否为open stream
    if (resource.isOpen()) {
        throw new BeanDefinitionStoreException(
                "Passed-in Resource [" + resource + "] contains an open stream: " +
                        "cannot determine validation mode automatically. Either pass in a Resource " +
                        "that is able to create fresh streams, or explicitly specify the validationMode " +
                        "on your XmlBeanDefinitionReader instance.");
    }
 
    InputStream inputStream;
    try {
        // 1.2.2 校验resource是否可以打开InputStream
        inputStream = resource.getInputStream();
    } catch (IOException ex) {
        throw new BeanDefinitionStoreException(
                "Unable to determine validation mode for [" + resource + "]: cannot open InputStream. " +
                        "Did you attempt to load directly from a SAX InputSource without specifying the " +
                        "validationMode on your XmlBeanDefinitionReader instance?", ex);
    }
 
    try {
        // 1.2.3 根据inputStream检测验证模式
        return this.validationModeDetector.detectValidationMode(inputStream);
    } catch (IOException ex) {
        throw new BeanDefinitionStoreException("Unable to determine validation mode for [" +
                resource + "]: an error occurred whilst reading from the InputStream.", ex);
    }
}
 
public int detectValidationMode(InputStream inputStream) throws IOException {
    // Peek into the file to look for DOCTYPE.
    BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
    try {
        boolean isDtdValidated = false;
        String content;
        // 1.2.3.1 按行遍历xml配置文件,获取xml文件的验证模式
        while ((content = reader.readLine()) != null) {
            content = consumeCommentTokens(content);
            // 如果读取的行是空或者注释则略过
            if (this.inComment || !StringUtils.hasText(content)) {
                continue;
            }
            // 内容包含"DOCTYPE"则为DTD,否则为XSD
            if (hasDoctype(content)) {
                isDtdValidated = true;
                break;
            }
            // 如果content带有 '<' 开始符号,则结束遍历。因为验证模式一定会在开始符号之前,所以到此可以认为没有验证模式
            if (hasOpeningTag(content)) {
                // End of meaningful data...
                break;
            }
        }
        // 1.2.3.2 根据遍历结果返回验证模式是 DTD 还是 XSD
        return (isDtdValidated ? VALIDATION_DTD : VALIDATION_XSD);
    } catch (CharConversionException ex) {
        // Choked on some character encoding...
        // Leave the decision up to the caller.
        return VALIDATION_AUTO;
    } finally {
        reader.close();
    }
}
 
// DefaultDocumentLoader.java
@Override
public Document loadDocument(InputSource inputSource, EntityResolver entityResolver,
        ErrorHandler errorHandler, int validationMode, boolean namespaceAware) throws Exception {
    // 2.1 创建DocumentBuilderFactory
    DocumentBuilderFactory factory = createDocumentBuilderFactory(validationMode, namespaceAware);
    if (logger.isDebugEnabled()) {
        logger.debug("Using JAXP provider [" + factory.getClass().getName() + "]");
    }
    // 2.2 通过DocumentBuilderFactory创建DocumentBuilder
    DocumentBuilder builder = createDocumentBuilder(factory, entityResolver, errorHandler);
    // 2.3 使用DocumentBuilder解析inputSource返回Document对象
    return builder.parse(inputSource);
}

(4)doLoadBeanDefinitions()–>registerBeanDefinitions()代码段:

public int registerBeanDefinitions(Document doc, Resource resource) throws BeanDefinitionStoreException {
    // 1.使用DefaultBeanDefinitionDocumentReader实例化BeanDefinitionDocumentReader
    BeanDefinitionDocumentReader documentReader = createBeanDefinitionDocumentReader();
    // 2.记录统计前BeanDefinition的加载个数
    int countBefore = getRegistry().getBeanDefinitionCount();
    // 3.createReaderContext:根据resource创建一个XmlReaderContext
    // 4.registerBeanDefinitions:加载及注册Bean定义
    documentReader.registerBeanDefinitions(doc, createReaderContext(resource));
    // 5.返回本次加载的BeanDefinition个数
    return getRegistry().getBeanDefinitionCount() - countBefore;
}

至此图中调用链也就调用完了。

二、getBeanFactory()

getBeanFactory主要作用返回beanFactory实例。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一只IT攻城狮

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值