首页 > 编程语言 >Spring源码-加载BeanDefinition之一

Spring源码-加载BeanDefinition之一

时间:2022-08-27 17:22:08浏览次数:53  
标签:resource BeanDefinitionStoreException beanFactory Spring 源码 ex loadBeanDefinitio

一、入口

    protected ConfigurableListableBeanFactory obtainFreshBeanFactory() {
	// 初始化BeanFactory,并进行XML文件读取,并将得到的BeanFactory记录在当前实体的属性中
	refreshBeanFactory();
	// 返回当前实体的beanFactory属性
	return getBeanFactory();
}

二、refreshBeanFactory

    @Override
protected final void refreshBeanFactory() throws BeansException {
	// 如果存在beanFactory,则销毁beanFactory
	if (hasBeanFactory()) {
		destroyBeans();
		closeBeanFactory();
	}
	try {
		// 创建DefaultListableBeanFactory对象
		DefaultListableBeanFactory beanFactory = createBeanFactory();
		// 为了序列化指定id,可以从id反序列化到beanFactory对象
		beanFactory.setSerializationId(getId());
		// 定制beanFactory,设置相关属性,包括是否允许覆盖同名称的不同定义的对象以及循环依赖
		customizeBeanFactory(beanFactory);
		// 初始化documentReader,并进行XML文件读取及解析,默认命名空间的解析,自定义标签的解析
		loadBeanDefinitions(beanFactory);
		this.beanFactory = beanFactory;
	}
	catch (IOException ex) {
		throw new ApplicationContextException("I/O error parsing bean definition source for " + getDisplayName(), ex);
	}
}

三、createBeanFactory

    **
 * 构造方法,忽略BeanNameAware,BeanFactoryAware,BeanClassLoaderAware的依赖
 *
 */
public AbstractAutowireCapableBeanFactory() {
	super();
	// 忽略要依赖的接口
	ignoreDependencyInterface(BeanNameAware.class);
	ignoreDependencyInterface(BeanFactoryAware.class);
	ignoreDependencyInterface(BeanClassLoaderAware.class);
}

四、customizeBeanFactory

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

五、loadBeanDefinitions

    @Override
protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws BeansException, IOException {
	// 创建一个xml的beanDefinitionReader,并通过回调设置到beanFactory中
	XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);

	
	// 给reader对象设置环境对象
	beanDefinitionReader.setEnvironment(this.getEnvironment());
	beanDefinitionReader.setResourceLoader(this);
	beanDefinitionReader.setEntityResolver(new ResourceEntityResolver(this));

	//  初始化beanDefinitionReader对象,此处设置配置文件是否要进行验证
	initBeanDefinitionReader(beanDefinitionReader);
	// 开始完成beanDefinition的加载
	loadBeanDefinitions(beanDefinitionReader);
}

六、loadBeanDefinitions

     protected void loadBeanDefinitions(XmlBeanDefinitionReader reader) throws BeansException, IOException {
	// 以Resource的方式获得配置文件的资源位置
	Resource[] configResources = getConfigResources();
	if (configResources != null) {
		reader.loadBeanDefinitions(configResources);
	}
	// 以String的形式获得配置文件的位置
	String[] configLocations = getConfigLocations();
	if (configLocations != null) {
		reader.loadBeanDefinitions(configLocations);
	}
}

七、loadBeanDefinitions

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

八、loadBeanDefinitions

    public int loadBeanDefinitions(String location, @Nullable Set<Resource> actualResources) throws BeanDefinitionStoreException {
	//此处获取resourceLoader对象
	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 {
			// 调用DefaultResourceLoader的getResource完成具体的Resource定位
			Resource[] resources = ((ResourcePatternResolver) resourceLoader).getResources(location);
			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 resource) throws BeanDefinitionStoreException {
	return loadBeanDefinitions(new EncodedResource(resource));
}

九、loadBeanDefinitions
获取Resource对象中的文件流对象,然后转为InputSource,并设置编码。InputSource是jdk中的sax中xml文件解析对象。提供的一种输入规范。之后执行doLoadBeanDefinitions进行XML的解析。

    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.add(encodedResource)) {
		throw new BeanDefinitionStoreException(
				"Detected cyclic loading of " + encodedResource + " - check your import definitions!");
	}

	// 从encodedResource中获取已经封装的Resource对象并再次从Resource中获取其中的inputStream
	try (InputStream inputStream = encodedResource.getResource().getInputStream()) {
		InputSource inputSource = new InputSource(inputStream);
		if (encodedResource.getEncoding() != null) {
			inputSource.setEncoding(encodedResource.getEncoding());
		}
		// 逻辑处理的核心步骤
		return doLoadBeanDefinitions(inputSource, encodedResource.getResource());
	}
	catch (IOException ex) {
		throw new BeanDefinitionStoreException(
				"IOException parsing XML document from " + encodedResource.getResource(), ex);
	}
	finally {
		currentResources.remove(encodedResource);
		if (currentResources.isEmpty()) {
			this.resourcesCurrentlyBeingLoaded.remove();
		}
	}
}

十、doLoadBeanDefinitions

    protected int doLoadBeanDefinitions(InputSource inputSource, Resource resource)
		throws BeanDefinitionStoreException {

	try {
		// 此处获取xml文件的document对象,这个解析过程是由documentLoader完成的,从String[] -string-Resource[]- resource,最终开始将resource读取成一个document文档,根据文档的节点信息封装成一个个的BeanDefinition对象
		Document doc = doLoadDocument(inputSource, resource);
		int count = registerBeanDefinitions(doc, resource);
		if (logger.isDebugEnabled()) {
			logger.debug("Loaded " + count + " bean definitions from " + resource);
		}
		return count;
	}
	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);
	}
}


    public int registerBeanDefinitions(Document doc, Resource resource) throws BeanDefinitionStoreException {
	// 实例化创建documentReader对象,documentReader对xml的beanDefinition进行解析
	BeanDefinitionDocumentReader documentReader = createBeanDefinitionDocumentReader();
	int countBefore = getRegistry().getBeanDefinitionCount();
	// 完成具体的解析过程
	documentReader.registerBeanDefinitions(doc, createReaderContext(resource));
	return getRegistry().getBeanDefinitionCount() - countBefore;
}


    @Override
public void registerBeanDefinitions(Document doc, XmlReaderContext readerContext) {
	this.readerContext = readerContext;
	doRegisterBeanDefinitions(doc.getDocumentElement());
}


    protected void doRegisterBeanDefinitions(Element root) {
	BeanDefinitionParserDelegate parent = this.delegate;
	this.delegate = createDelegate(getReaderContext(), root, parent);

	if (this.delegate.isDefaultNamespace(root)) { // 判断namespaceuri是否是http://www.springframework.org/schema/beans
		String profileSpec = root.getAttribute(PROFILE_ATTRIBUTE);
		if (StringUtils.hasText(profileSpec)) { // profile属性存在
			String[] specifiedProfiles = StringUtils.tokenizeToStringArray(
					profileSpec, BeanDefinitionParserDelegate.MULTI_VALUE_ATTRIBUTE_DELIMITERS);
			// We cannot use Profiles.of(...) since profile expressions are not supported
			// in XML config. See SPR-12458 for details.
			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);  // 解析xml文件里的标签
	postProcessXml(root); // 钩子函数

	this.delegate = parent;
}

十一、parseBeanDefinitions

    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); // 解析默认标签import,alias,bean,beans
				}
				else {
					delegate.parseCustomElement(ele); // 解析自定义标签
				}
			}
		}
	}
	else {
		delegate.parseCustomElement(root);
	}
}

以上是将xml的位置解析成Resource对象,在将Resource转成sax中解析xml的InputSource,读取成Document文档,解析Document文档。

标签:resource,BeanDefinitionStoreException,beanFactory,Spring,源码,ex,loadBeanDefinitio
From: https://www.cnblogs.com/shigongp/p/16630914.html

相关文章

  • spring boot 分布式session实现
    springboot分布式session实现主要是通过包装HttpServletRequest将session相关的方法进行代理。具体是的实现就是通过SessionRepositoryFilter过滤器将HttpServletReque......
  • springboot+docker发布项目20220827
    1、springboot打包项目 1)、application-dev.yml     对应配置修改 2)、项目package 生成包    3)、生成包         4)、运行......
  • 04Spring MVC入门
    SpringMVC三层架构表现层业务层数据访问层MVC(处理表现层)Model:模型层View:视图层Controller:控制层底层请求方式在controller中添加@RequestMapping("/......
  • Spring boot 对接口限制IP访问次数
    1、需要的依赖<!--redis依赖--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-star......
  • springBoot 获取注解参数的原理
    springBoot获取注解参数的原理原理图先得到请求的request在获取可以处理请求的方法的Mapping映射器DispatcherServlet中的doDispatch方法//De......
  • 日志打印输出到控制台以及文件 Spring boot application.yml https://www.cnblogs
    转自:https://www.cnblogs.com/izyh/p/15945950.htmlyml文件中添加配置:##########日志配置-START##########logging:config:classpath:logback-spring.......
  • JPA 入门实战(3)--Spring Boot 中使用 JPA
    本文主要介绍在SpringBoot中使用JPA的方法(暂不使用spring-data-jpa),相关的环境及软件信息如下:SpringBoot2.6.10、JPA2.2、eclipselink2.7.10。1、原生使用该......
  • 78、使用Jenkins Docker 部署SpringBoot项目
    1、centOS安装Docker1、更新软件源:yumupdate2、卸载旧版本:yumremovedockerdocker-commondocker-selinuxdocker-engine3、安装软件包:yuminstall-yyum-utils......
  • SpringBoot集成thymeleaf不生效问题
    场景:在做springBoot整合Theamleaf时,用了@RestController注解,在进行试图渲染的过程中,遇到试图没有渲染成功,找到了原因,记录一下。 第一种情况:使用@RestController注解pac......
  • 大家都能看得懂的源码 - ahooks 是怎么处理 DOM 的?
    本文是深入浅出ahooks源码系列文章的第十三篇,该系列已整理成文档-地址。觉得还不错,给个star支持一下哈,Thanks。本篇文章探讨一下ahooks对DOM类Hooks使用规范,以......