Spring源码解析之IOC篇

学习重点

不要想着背!不要想着背!不要想着背!:顺着流程和思路逐步理解。

知行合一!知行合一!知行合一!:学完每个阶段性知识,一定要文档输出或实践。

OK,说完重点,我们开始!

什么是IOC?

IOC(Inversion Of Control)控制反转:就是把项目中原来需要手动实现对象创建、依赖的代码,交给Spring创建的容器去实现并统一管理,这样就实现了控制反转。

按这么解释我们肯定想到那这个容器里面肯定存了很多对象的实例(Spring中通过定义BeanDefinition对象来包装原生对象),所以在我们需要的时候就能够直接拿到。

这里留一个疑问: 存放对象的容器到底是啥样子的?或者是用什么数据结构定义存放的,Map 还是 List 或者 其它?

准备工作

首先我们已经知道容器存放了Spring为我们创建的对象,那么肯定需要一个描述来让容器知道需要创建的对象与对象的关系,也就是配置元数据。

所以在真正了解容器初始化过程之前,需要做一些准备工作。

第一步:配置元数据

Spring中配置元数据的方式有以下三种:

  • XML:传统配置方式,简单直观,容易管理
<beans>
    <bean id="" class=""/> 
</beans>
  • Java代码:定义应用程序类外部的 bean
@Configuration
public class ConfigA {

    @Bean
    public A a() {
        return new A();
    }
}
  • 注解:简化配置,但是分散难控制。使用类级别注解@Controller、@Service、@Component等等,类内部注解@Autowire、@Value、@Resource等等。

目前日常开发中我们使用最多的可能还是基于注解的方式,简化配置。当然Spring允许这三种方式在一起混合使用,这个以实际开发情况而定。

第二步:配置解析

完成元数据的配置之后,那么接下来就需要对配置进行解析,显而易见,不同配置方式的语法和对数据的描述是不一样的。

所以Spring封装了不同的配置方式的解析器,比如:

  • XML:使用ClasspathXmlApplicationContext作为入口,XmlBeanDefinitionReader进行Bean的元数据解析
  • Java代码、注解:使用AnnotationConfigApplicationContext作为入口,AnnotatedBeanDefinitionReader进行Bean的元数据解析
第三步:关键类

这里我们先提前眼熟下IOC中的几个关键类,分别是:

  • BeanFactory: 它是最顶层的一个接口,见名知意,典型的工厂模式。image-20210708213402792通过它的源码我们可以发现,BeanFactory只定义了IOC容器的基本行为,获取bean及bean的各种属性。接下来看下它的子类类图

    image-20210709082411016

    它下面有三个重要的子类:ListableBeanFactory<可以枚举所有的bean实例>、HierarchicalBeanFactory<可以配置Bean的父类,有继承关系>、AutowireCapableBeanFactory<提供Bean的自动装配的实现>。同时可以发现DefaultListableBeanFactory实现了所有接口,这代表它拥有上面所有“大哥”的功能而且还有自己的扩展功能(疯狂眼熟),所以它也是默认的实现类。

  • ApplicationContext: 上面BeanFactory工厂帮我们拿到对象,那么工厂是如何产生对象的,那就要看它,它提供了具体的IOC实现,GenericApplicationContext、ClasspathXmlApplicationContext等,同时还提供了以下的附加服务:

    • 支持信息源,可以实现国际化<实现MessageSource接口>

    • 访问资源<实现ResourcePatternResolver接口>

    • 支持应用事件<实现ApplicationEventPublisher接口>

  • BeanDefinition: Bean对象在Spring中的描述定义,可以理解为容器中每个Bean对象都是以BeanDefinition来保存的。

  • BeanDefinitionReader: 配置Bean的元数据的解析器,比如XmlBeanDefinitionReader、AnnotatedBeanDefinitionReader

IOC的初始化(从无到有)

TIPS

阅读一个成熟框架的源码是非常困难的事,几乎百分百会“晕车”,尤其不要因为好奇心(强迫症)想要看懂每个方法的调用(调用链会让你感觉在无限递归一样),每行代码的细节(相信我,看完该忘还是会忘的),那样真的会害死猫的!我们要一定从架构方面来探究,不追求细节,在理解整个思路流程之后再根据实际的情况逐步去查看某些细节。

阅读文章也是,如果是不懂的知识文,先看大纲,如果没有则先略看提炼下大纲,对整个架构有了了解之后,再看每个大纲下的细节分析去深入。

既然了解了容器的本质,也做了一些准备工作,那么接下来我们就要开始探究Spring是怎么完成容器的初始化的?

从无到有,那么我们就要从入口开始,也就是ClasspathXmlApplicationContext和AnnotationConfigApplicationContext,原因刚才准备工作的配置解析也已经说过了。

下面我们分别探究下。

基于XML的IOC容器的初始化
1.定位

首先我们先结合之前准备工作的内容想想,Bean的资源文件定义过之后<配置元数据>,接下来肯定会想办法去解析定义的数据再注册到容器里面去,逻辑是没错的,但是解析前我们要考虑一下怎么找到解析文件,也就是配置路径。所以第一步(其实这里算第二步,第一步是配置元数据,这里我们默认已经实现过了)我们需要解析路径,也就是定位。

首先我们会通过main()方法启动:

ApplicationContext applicationContext = new ClassPathXmlApplicationContext("application.xml"); 

我们先提前简单了解下ClassPathXmlApplicationContext的类图:

image-20210712154935453

接下来点进去看实际调用的ClassPathXmlApplicationContext的源码:

	public ClassPathXmlApplicationContext(String[] configLocations, boolean refresh, @Nullable ApplicationContext parent) throws BeansException {
		// 设置Bean的资源加载器
		super(parent);
		// 解析设置Bean的资源文件的路径
		setConfigLocations(configLocations);
		if (refresh) {
			// 载入Bean的配置资源
			refresh();
		}
	}

1、super(parent):调用父类的构造方法来设置Bean的资源加载器,点到最后就知道调用的是父类AbstractApplicationcontext的getResourcePatternResolver()方法,

	protected ResourcePatternResolver getResourcePatternResolver() {
		//因为AbstractApplicationContext继承DefaultResourceLoader,所以它也是一个资源加载器,其getResource(String location)方法用于载入资源
        return new PathMatchingResourcePatternResolver(this);
	}

2、setConfigLocations(configLocations):调用父类AbstractRefreshableConfigApplicationContext的具体实现,能理解这里解析处理了Bean的资源文件的路径,同时这个路径支持传入字符串数组

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

3、refresh():载入Bean的配置资源,显而易见,这个方法的具体实现就是我们需要去重点关注的了," Let we look look ".点进去发现调用的是父类AbstractApplicationContext的refresh()方法,这里发现refresh()其实是一个模板方法,我们看看具体的逻辑处理,

	@Override
	public void refresh() throws BeansException, IllegalStateException {
		synchronized (this.startupShutdownMonitor) {
			//1、调用容器准备刷新的方法,获取容器的当时时间,同时给容器设置同步标识
			prepareRefresh();

			//2、告诉子类启动refreshBeanFactory()方法,Bean定义资源文件的载入从子类的refreshBeanFactory()方法启动
			ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

			//3、为BeanFactory配置容器特性,例如类加载器、事件处理器等
			prepareBeanFactory(beanFactory);

			try {
				//4、为容器的某些子类指定特殊的BeanPost事件处理器
				postProcessBeanFactory(beanFactory);

				//5、调用所有注册的BeanFactoryPostProcessor的Bean
				invokeBeanFactoryPostProcessors(beanFactory);

				//6、为BeanFactory注册BeanPost事件处理器.BeanPostProcessor是Bean后置处理器,用于监听容器触发的事件
				registerBeanPostProcessors(beanFactory);

				//7、初始化信息源,和国际化相关.
				initMessageSource();

				//8、初始化容器事件传播器.
				initApplicationEventMulticaster();

				//9、调用子类的某些特殊Bean初始化方法
				onRefresh();

				//10、为事件传播器注册事件监听器.
				registerListeners();

				//11、初始化所有剩余的单例Bean
				finishBeanFactoryInitialization(beanFactory);

				//12、初始化容器的生命周期事件处理器,并发布容器的生命周期事件
				finishRefresh();
			}
			catch (BeansException ex) {
				if (logger.isWarnEnabled()) {
					logger.warn("Exception encountered during context initialization - " +
							"cancelling refresh attempt: " + ex);
				}

				//13、销毁已创建的Bean
				destroyBeans();

				//14、取消refresh操作,重置容器的同步标识。
				cancelRefresh(ex);

				throw ex;
			}
			finally {
				//15、重设公共缓存
				resetCommonCaches();
			}
		}
	}

我们主要看obtainFreshBeanFactory()方法,Bean的加载注册就是这里面实现的,之后的代码都是容器的初始化信息源和生命周期的一些事件。到这里Bean的资源文件就完成定位了,接下来我们就看加载的具体代码实现。

2.加载

处理设置完配置文件的路径之后,在解析元数据之前,我们还需要先创建容器,这样解析后的对象才能有地方去存放调用,我们看下obtainFreshBeanFactory()的具体实现,

	protected ConfigurableListableBeanFactory obtainFreshBeanFactory() {
		refreshBeanFactory();
		ConfigurableListableBeanFactory beanFactory = getBeanFactory();
		if (logger.isDebugEnabled()) {
			logger.debug("Bean factory for " + getDisplayName() + ": " + beanFactory);
		}
		return beanFactory;
	}

这里refreshBeanFactory()方法使用了委派设计模式,在父类中定义了抽象方法,但是具体的实现调用子类的方法。点进去能看到实际调用的是AbstractRefreshableApplicationContext覆写的方法,

	@Override
	protected final void refreshBeanFactory() throws BeansException {
		//如果已经有容器,销毁容器中的bean,关闭容器
		if (hasBeanFactory()) {
			destroyBeans();
			closeBeanFactory();
		}
		try {
			//创建IOC容器
			DefaultListableBeanFactory beanFactory = createBeanFactory();
			beanFactory.setSerializationId(getId());
			//对容器自定义,如设置允许Bean覆盖,允许Bean循环引用
			customizeBeanFactory(beanFactory);
			//调用载入Bean定义的方法,这里又使用了一个委派模式,在当前类中只定义了抽象的loadBeanDefinitions方法,具体的实现调用子类容器
			loadBeanDefinitions(beanFactory);
			synchronized (this.beanFactoryMonitor) {
				this.beanFactory = beanFactory;
			}
		}
		catch (IOException ex) {
			throw new ApplicationContextException("I/O error parsing bean definition source for " + getDisplayName(), ex);
		}
	}

从实现我们能知道,创建容器前先判断BeanFactory是否已经存在,如果存在则销毁容器中所有的Bean<将存放Bean的HashMap进行clear()>,并关闭BeanFactory<设置为null>。,反之不存在则创建IOC容器DefaultListableBeanFactory,这里是不是很熟悉,没错就是我们之前疯狂眼熟的关键类,比所有“大哥”都厉害的默认实现类,其实它就是IOC容器(tips: 这里可能会发晕,怎么都是容器,好像都不一样啊,到底哪个都是,这里说明一下,Spring的容器本来就有很多,除了BeanFactory的子类外,通俗的说ApplicationContext下面的继承或者实现的子类,像上面ClassPathXmlApplicationContext类图中的那些,我们都能称之为容器,继承的子类不用说,而实现类中都有属性private ApplicationContext parent;,所以它们都可以称为容器),之前我们留有一个疑问:存放对象的容器到底是啥样子的?这里我们知道了容器的具体模样了,但是其实真正存放Bean对象的容器或者叫做容器的数据结构,继续往下探究,等会我们就能知晓了。

接下来看loadBeanDefinitions()这个方法,同样是抽象方法,真正的实现由其子类AbstractXmlApplicationContext来实现,具体实现如下:

	@Override
	protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws BeansException, IOException {
		//创建Bean的配置数据解析器,并通过回调设置到容器中去
		XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);
		//设置资源环境、资源加载器
		beanDefinitionReader.setEnvironment(this.getEnvironment());
		beanDefinitionReader.setResourceLoader(this);
		//设置SAX xml解析器
		beanDefinitionReader.setEntityResolver(new ResourceEntityResolver(this));
		//启用解析Xml资源文件时的校验机制
		initBeanDefinitionReader(beanDefinitionReader);
		//实现Bean的加载
		loadBeanDefinitions(beanDefinitionReader);
	}

接下来继续探究loadBeanDefinitions()方法,

	protected void loadBeanDefinitions(XmlBeanDefinitionReader reader) throws BeansException, IOException {
		//获取Bean配置数据的定位
		Resource[] configResources = getConfigResources();
		if (configResources != null) {
			reader.loadBeanDefinitions(configResources);
		}
		//当子类中获取Bean配置数据的定位为空,则获取FileSystemXmlApplicationContext构造方法中setConfigLocations方法设置的资源
		String[] configLocations = getConfigLocations();
		if (configLocations != null) {
			reader.loadBeanDefinitions(configLocations);
		}
	}

	@Nullable
	protected Resource[] getConfigResources() {
		return null;
	}

这里getConfigResources()也同样使用了委托模式,真正是在ClassPathXmlApplicationContext中得以实现,reader.loadBeanDefinitions(configResources)实际则是调用父类AbstractBeanDefinitionReader来解析Bean的配置数据,

	@Override
	public int loadBeanDefinitions(Resource... resources) throws BeanDefinitionStoreException {
		Assert.notNull(resources, "Resource array must not be null");
		int counter = 0;
		for (Resource resource : resources) {
			counter += loadBeanDefinitions(resource);
		}
		return counter;
	}
	@Override
	public int loadBeanDefinitions(Resource resource) throws BeanDefinitionStoreException {
		//将解析的XML资源进行特殊编码处理
		return loadBeanDefinitions(new EncodedResource(resource));
	}
	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());
		}
		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 {
			//将资源文件转为InputStream的IO流
			InputStream inputStream = encodedResource.getResource().getInputStream();
			try {
				//得到XML的解析源
				InputSource inputSource = new InputSource(inputStream);
				if (encodedResource.getEncoding() != null) {
					inputSource.setEncoding(encodedResource.getEncoding());
				}
				//具体的解析过程
				return doLoadBeanDefinitions(inputSource, encodedResource.getResource());
			}
			finally {
				//关闭IO流
				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();
			}
		}
	}

我们继续沿reader.loadBeanDefinitions(configResources)方法往下看,发现最后调用的是XmlBeanDefinitionReader的loadBeanDefinitions()方法,这也就是我们准备工作说的xml的解析器,具体的解析过程在doLoadBeanDefinitions()方法中,

	protected int doLoadBeanDefinitions(InputSource inputSource, Resource resource)
			throws BeanDefinitionStoreException {
		try {
			//将XML文件转换为DOM对象
			Document doc = doLoadDocument(inputSource, resource);
			//解析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);
		}
	}

通过分析,加载Bean的配置数据最后就是将xml转换为Document对象,然后进行解析,我们先看doLoadDocument()方法,知道最后会调用DefaultDocumentLoder的loadDocument()方法,

	@Override
	public Document loadDocument(InputSource inputSource, EntityResolver entityResolver,
			ErrorHandler errorHandler, int validationMode, boolean namespaceAware) throws Exception {
		//创建文件解析器工厂
		DocumentBuilderFactory factory = createDocumentBuilderFactory(validationMode, namespaceAware);
		if (logger.isDebugEnabled()) {
			logger.debug("Using JAXP provider [" + factory.getClass().getName() + "]");
		}
		//创建文档解析器
		DocumentBuilder builder = createDocumentBuilder(factory, entityResolver, errorHandler);
		//解析资源数据
		return builder.parse(inputSource);
	}

上面的解析过程是调用JavaEE标准的JAXP标准进行处理,接下来我们继续分析转换位为Document对象之后,是怎么解析为IOC容器中管理的Bean对象,并将其注册到容器中的。我们继续看registerBeanDefinition()方法,

	public int registerBeanDefinitions(Document doc, Resource resource) throws BeanDefinitionStoreException {
		//创建BeanDefinitionDocumentReader
		BeanDefinitionDocumentReader documentReader = createBeanDefinitionDocumentReader();
		//获得容器中已经注册的Bean数量
		int countBefore = getRegistry().getBeanDefinitionCount();
		//具体的解析过程
		documentReader.registerBeanDefinitions(doc, createReaderContext(resource));
		//返回解析的Bean数量
		return getRegistry().getBeanDefinitionCount() - countBefore;
	}

在前面准备工作中的关键类介绍我们知道IOC容器中实际存放的是BeanDefinition对象,它对Bean对象的封装,所以这里我们先创建BeanDefinitionDocumentReader来完成对xml中的Bean对象解析封装,主要的解析过程是在documentReader.registerBeanDefinitions(doc, createReaderContext(resource))中完成的,我们继续看,

	@Override
	public void registerBeanDefinitions(Document doc, XmlReaderContext readerContext) {
		this.readerContext = readerContext;
		logger.debug("Loading bean definitions");
		//获得Document的根元素
		Element root = doc.getDocumentElement();
		doRegisterBeanDefinitions(root);
	}

	protected void doRegisterBeanDefinitions(Element root) {
		//具体的解析过程由BeanDefinitionParserDelegate实现,
		//BeanDefinitionParserDelegate中定义了Spring Bean定义XML文件的各种元素
		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.isInfoEnabled()) {
						logger.info("Skipped XML bean definition file due to specified profiles [" + profileSpec +
								"] not matching: " + getReaderContext().getResource());
					}
					return;
				}
			}
		}
		//预处理xml,在解析Bean定义之前,进行自定义的解析,增强解析过程的可扩展性
		preProcessXml(root);
		//从Document的根元素开始进行Bean定义的Document对象
		parseBeanDefinitions(root, this.delegate);
		//在解析Bean定义之后,进行自定义的解析,增加解析过程的可扩展性
		postProcessXml(root);
		this.delegate = parent;
	}

我们发现其实registerBeanDefinitions()实现是由BeanDefinitionDocumentReader的实现类DefaultBeanDefinitionDocumentReader来实现的,通过源码分析我们能看出,BeanDefinitionParserDelegate这个类很关键,点进去发现它定义了XML文件中的各种元素,那么我们就知道了更具体的解析就靠它了,往下preProcessXml(root);和postProcessXml(root);能通过名字知道前置和后置处理XML,点进去发现是预留的空实现,主要是增强解析过程中的扩展性,我们主要看parseBeanDefinitions()这个方法的实现,

//使用Spring的Bean规则从Document的根元素开始进行Bean定义的Document对象
	protected void parseBeanDefinitions(Element root, BeanDefinitionParserDelegate delegate) {
		//Document对象是否是默认的XML命名空间
		if (delegate.isDefaultNamespace(root)) {
			//Document对象根元素的所有子节点
			NodeList nl = root.getChildNodes();
			for (int i = 0; i < nl.getLength(); i++) {
				Node node = nl.item(i);
				//Document节点是否为XML的元素节点
				if (node instanceof Element) {
					Element ele = (Element) node;
					//Document的元素节点是否是默认的XML命名空间
					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>Bean规则解析
		else if (delegate.nodeNameEquals(ele, BEAN_ELEMENT)) {
			processBeanDefinition(ele, delegate);
		}
		else if (delegate.nodeNameEquals(ele, NESTED_BEANS_ELEMENT)) {
             // 递归
			doRegisterBeanDefinitions(ele);
		}
	}

我们能看到从这里就是真正开始解析Xml的数据了,同时这里有两种解析方式,分别是parseDefaultElement()默认元素解析和parseCustomElement()自定义元素解析,而自定义需要额外的实现,我们主要看默认元素解析的过程,发现有三种不同的解析处理,分别是导入、别名、对象解析,我们分别看他们的实现,

	protected void importBeanDefinitionResource(Element ele) {
		//获取配置的导入元素的location属性
		String location = ele.getAttribute(RESOURCE_ATTRIBUTE);
		//location为空,则直接结束
		if (!StringUtils.hasText(location)) {
			getReaderContext().error("Resource location must not be empty", ele);
			return;
		}
		//使用系统变量值解析location属性值
		location = getReaderContext().getEnvironment().resolveRequiredPlaceholders(location);
		Set<Resource> actualResources = new LinkedHashSet<>(4);
		//标识配置的location是否是绝对路径
		boolean absoluteLocation = false;
		try {
			absoluteLocation = ResourcePatternUtils.isUrl(location) || ResourceUtils.toURI(location).isAbsolute();
		}
		catch (URISyntaxException ex) {

		}
		if (absoluteLocation) {
			try {
				//解析器加载location路径的Bean配置数据
				int importCount = getReaderContext().getReader().loadBeanDefinitions(location, actualResources);
				if (logger.isDebugEnabled()) {
					logger.debug("Imported " + importCount + " bean definitions from URL location [" + location + "]");
				}
			}
			catch (BeanDefinitionStoreException ex) {
				getReaderContext().error(
						"Failed to import bean definitions from URL location [" + location + "]", ele, ex);
			}
		}
		else {
			//配置的location是相对路径
			try {
				int importCount;
				//将location转换为相对路径
				Resource relativeResource = getReaderContext().getResource().createRelative(location);
				//路径资源是否存在
				if (relativeResource.exists()) {
					//解析器加载location路径的Bean配置数据
					importCount = getReaderContext().getReader().loadBeanDefinitions(relativeResource);
					actualResources.add(relativeResource);
				}
				else {
					//获取解析器的基本路径
					String baseLocation = getReaderContext().getResource().getURL().toString();
					//解析器加载location路径的Bean配置数据
					importCount = getReaderContext().getReader().loadBeanDefinitions(
							StringUtils.applyRelativePath(baseLocation, location), actualResources);
				}
				if (logger.isDebugEnabled()) {
					logger.debug("Imported " + importCount + " bean definitions from relative location [" + location + "]");
				}
			}
			catch (IOException ex) {
				getReaderContext().error("Failed to resolve current resource location", ele, ex);
			}
			catch (BeanDefinitionStoreException ex) {
				getReaderContext().error("Failed to import bean definitions from relative location [" + location + "]",
						ele, ex);
			}
		}
		Resource[] actResArray = actualResources.toArray(new Resource[actualResources.size()]);
		//解析完<Import>元素之后,发送导入资源处理完成事件
		getReaderContext().fireImportProcessed(location, actResArray, extractSource(ele));
	}

这里主要解析导入元素,根据配置的导入路径来加载Bean配置资源到IOC容器中。

	protected void processAliasRegistration(Element ele) {
		//获取name的属性值
		String name = ele.getAttribute(NAME_ATTRIBUTE);
		//获取alias的属性值
		String alias = ele.getAttribute(ALIAS_ATTRIBUTE);
		boolean valid = true;
		//name属性值为空
		if (!StringUtils.hasText(name)) {
			getReaderContext().error("Name must not be empty", ele);
			valid = false;
		}
		//alias属性值为空
		if (!StringUtils.hasText(alias)) {
			getReaderContext().error("Alias must not be empty", ele);
			valid = false;
		}
		if (valid) {
			try {
				//向容器注册别名
	getReaderContext().getRegistry().registerAlias(name, alias);
			}
			catch (Exception ex) {
				getReaderContext().error("Failed to register alias '" + alias +
						"' for bean with name '" + name + "'", ele, ex);
			}
			//解析完<Alias>元素之后,发送别名处理完成事件
			getReaderContext().fireAliasRegistered(name, alias, extractSource(ele));
		}
	}

这里主要解析别名元素,为Bean向IOC容器中注册别名。

	protected void processBeanDefinition(Element ele, BeanDefinitionParserDelegate delegate) {
         // BeanDefinition的封装
		BeanDefinitionHolder bdHolder = delegate.parseBeanDefinitionElement(ele);
		if (bdHolder != null) {
			bdHolder = delegate.decorateBeanDefinitionIfRequired(ele, bdHolder);
			try {
				// 将Bean注册到IOC容器中
BeanDefinitionReaderUtils.registerBeanDefinition(bdHolder, getReaderContext().getRegistry());
			}
			catch (BeanDefinitionStoreException ex) {
				getReaderContext().error("Failed to register bean definition with name '" +
						bdHolder.getBeanName() + "'", ele, ex);
			}
			//注册完成之后,发送注册完成事件
			getReaderContext().fireComponentRegistered(new BeanComponentDefinition(bdHolder));
		}
	}

这里主要解析对象元素,将Bean的封装对象BeanDefinition注册到IOC容器中。我们发现往容器中注册传参的是BeanDefinitionHolder这个对象,它是BeanDefinition的封装。我们可以看下是怎么解析并转换为BeanDefinitionHolder的,

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

	@Nullable
	public BeanDefinitionHolder parseBeanDefinitionElement(Element ele, @Nullable BeanDefinition containingBean) {
		//获取id属性值
		String id = ele.getAttribute(ID_ATTRIBUTE);
		//获取name属性值
		String nameAttr = ele.getAttribute(NAME_ATTRIBUTE);
		//获取alias属性值
		List<String> aliases = new ArrayList<>();
		//将所有name属性值添加到别名集合中
		if (StringUtils.hasLength(nameAttr)) {
			String[] nameArr = StringUtils.tokenizeToStringArray(nameAttr, MULTI_VALUE_ATTRIBUTE_DELIMITERS);
			aliases.addAll(Arrays.asList(nameArr));
		}
		String beanName = id;
		//如果没有配置id属性时,则将beanName赋值为别名中的第一个值
		if (!StringUtils.hasText(beanName) && !aliases.isEmpty()) {
			beanName = aliases.remove(0);
			if (logger.isDebugEnabled()) {
				logger.debug("No XML 'id' specified - using '" + beanName +
						"' as bean name and " + aliases + " as aliases");
			}
		}
		if (containingBean == null) {
			//检查所配置的id、name或者别名是否重复
			checkNameUniqueness(beanName, aliases, ele);
		}
		//详细对<Bean>元素中配置的Bean定义进行解析的地方
		AbstractBeanDefinition beanDefinition = parseBeanDefinitionElement(ele, beanName, containingBean);
		if (beanDefinition != null) {
			if (!StringUtils.hasText(beanName)) {
				try {
					if (containingBean != null) {
						//为解析的Bean生成一个唯一beanName
						beanName = BeanDefinitionReaderUtils.generateBeanName(
								beanDefinition, this.readerContext.getRegistry(), true);
					}
					else {
						//为解析的Bean生成一个唯一beanName
						beanName = this.readerContext.generateBeanName(beanDefinition);
						String beanClassName = beanDefinition.getBeanClassName();
						if (beanClassName != null &&
								beanName.startsWith(beanClassName) && beanName.length() > beanClassName.length() &&
								!this.readerContext.getRegistry().isBeanNameInUse(beanClassName)) {
							aliases.add(beanClassName);
						}
					}
					if (logger.isDebugEnabled()) {
						logger.debug("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;
	}

上面代码主要处理解析元素的id,name和别名属性,顺着parseBeanDefinitionElement()方法往下看,会知道会对一些配置如meta、qualifier、property等的解析,我们能看到Bean的属性在解析是是如何设置的等等,这里我们不在细看。

到这里就已经将配置载入到内存, 也就是说完成了Bean对象的加载,但是更为重要的动作还没有做,我们需要将准备好的BeanDefinition对象注册到容器中去。

3.注册

从开始的配置元数据到这步是不是有种水到渠成的感觉,万事俱备,只欠注册。

那我们来看registerBeanDefinition()方法,

	//将解析的BeanDefinitionHold注册到容器中
	public static void registerBeanDefinition(
			BeanDefinitionHolder definitionHolder, BeanDefinitionRegistry registry)
			throws BeanDefinitionStoreException {
		String beanName = definitionHolder.getBeanName();
		//向IOC容器注册BeanDefinition
		registry.registerBeanDefinition(beanName, definitionHolder.getBeanDefinition());
		//如果解析的BeanDefinition有别名,向容器为其注册别名
		String[] aliases = definitionHolder.getAliases();
		if (aliases != null) {
			for (String alias : aliases) {
				registry.registerAlias(beanName, alias);
			}
		}
	}

到最后我们发现真正完成注册功能的还是我们眼熟的大佬,默认实现的DefaultListableBeanFactory,对Spring来说,它就是分配的一个注册策略。让我们来揭开真正容器的神秘的面纱,

//向IOC容器注册解析的BeanDefiniton
	@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");
		//校验解析的BeanDefiniton
		if (beanDefinition instanceof AbstractBeanDefinition) {
			try {
				((AbstractBeanDefinition) beanDefinition).validate();
			}
			catch (BeanDefinitionValidationException ex) {
				throw new BeanDefinitionStoreException(beanDefinition.getResourceDescription(), beanName,
						"Validation of bean definition failed", ex);
			}
		}
		BeanDefinition oldBeanDefinition;
		oldBeanDefinition = this.beanDefinitionMap.get(beanName);
		if (oldBeanDefinition != null) {
			if (!isAllowBeanDefinitionOverriding()) {
				throw new BeanDefinitionStoreException(beanDefinition.getResourceDescription(), beanName,
						"Cannot register bean definition [" + beanDefinition + "] for bean '" + beanName +
						"': There is already [" + oldBeanDefinition + "] bound.");
			}
			else if (oldBeanDefinition.getRole() < beanDefinition.getRole()) {
				// e.g. was ROLE_APPLICATION, now overriding with ROLE_SUPPORT or ROLE_INFRASTRUCTURE
				if (this.logger.isWarnEnabled()) {
					this.logger.warn("Overriding user-defined bean definition for bean '" + beanName +
							"' with a framework-generated bean definition: replacing [" +
							oldBeanDefinition + "] with [" + beanDefinition + "]");
				}
			}
			else if (!beanDefinition.equals(oldBeanDefinition)) {
				if (this.logger.isInfoEnabled()) {
					this.logger.info("Overriding bean definition for bean '" + beanName +
							"' with a different definition: replacing [" + oldBeanDefinition +
							"] with [" + beanDefinition + "]");
				}
			}
			else {
				if (this.logger.isDebugEnabled()) {
					this.logger.debug("Overriding bean definition for bean '" + beanName +
							"' with an equivalent definition: replacing [" + oldBeanDefinition +
							"] with [" + beanDefinition + "]");
				}
			}
			this.beanDefinitionMap.put(beanName, beanDefinition);
		}
		else {
			if (hasBeanCreationStarted()) {
				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 {
				this.beanDefinitionMap.put(beanName, beanDefinition);
				this.beanDefinitionNames.add(beanName);
				this.manualSingletonNames.remove(beanName);
			}
			this.frozenBeanDefinitionNames = null;
		}
		//检查是否有同名的BeanDefinition已经在IOC容器中注册
		if (oldBeanDefinition != null || containsSingleton(beanName)) {
			//重置所有已经注册过的BeanDefinition的缓存
			resetBeanDefinition(beanName);
		}
	}

整个方法看下来,是不是注意到this.beanDefinitionMap.put(beanName, beanDefinition);这段代码,而beanDefinition正是我们准备很久的Bean对象,查看我们得知beanDefinitionMap是一个ConcurrentHashMap,一个线程安全的Map,回想起我们开始的疑问,“用什么数据结构定义存放的,Map 还是 List 或者 其它?”,这里就揭开了谜底,beanDefinitionMap就是真正意义上的容器。同时我们也能看到类里面定义了其他的ConcurrentHashMap、ArrayList、LinkedHashSet,同时另外还包括父类中继承的,它们各自存放了容器中不同的对象相关信息,具体接触使用的时候自然会明白它们相应的用处。

	/** Map from dependency type to corresponding autowired value */
	private final Map<Class<?>, Object> resolvableDependencies = new ConcurrentHashMap<>(16);

	/** Map of singleton and non-singleton bean names, keyed by dependency type */
	private final Map<Class<?>, String[]> allBeanNamesByType = new ConcurrentHashMap<>(64);

	/** Map of singleton-only bean names, keyed by dependency type */
	private final Map<Class<?>, String[]> singletonBeanNamesByType = new ConcurrentHashMap<>(64);

	/** List of bean definition names, in registration order */
	private volatile List<String> beanDefinitionNames = new ArrayList<>(256);

	/** List of names of manually registered singletons, in registration order */
	private volatile Set<String> manualSingletonNames = new LinkedHashSet<>(16);

同时我们看到有一段synchronized修饰的代码,这里因为注册对象的过程中需要线程同步,来保证数据的一致性。

到这里,基于Xml的IOC容器初始化流程就已经全部完成了,基本看完整个流程之后也梳理了初始化的时序图以及流程图,来加深对整个容器初始化流程的理解,同时我们知道了IOC容器的初始化主要就三步: 定位、加载、注册

Spring-IOC时序图

image-20210713221945158

而接下来会探究另一种基于注解的初始化方式,其实也都是大同小异,再理解起来相信会更加容易。

基于Annotation的IOC容器的初始化

查阅Spring版本资料,我们知道2.0以后的版本中引入了基于注解Annotation方式的配置,主要用于简化Bean的配置,提高开发效率。到如今开始流行SpringBoot,也是基于注解来基本实现了零配置,实际开发使用起来,只能一个爽字。前面准备工作中我们了解了注解分为两种:类级别和类内部的注解,而Spring容器根据这两种不同方式都有不同的处理策略。其中类级别注解根据注解的过滤规则扫描读取注解Bean的定义类,而类内部注解则通过Bean的后置注解处理器解析Bean内部的注解。

我们已经知道入口是AnnotationConfigApplicationContext,其实它还有个兄弟,叫做AnnotationConfigWebApplicationContext,是AnnotationConfigApplicationContext的Web版本,它们的功能和用法基本没啥区别,下面我们会以AnnotationConfigApplicationContext为例来探究IOC容器的初始化过程。

1.入口

首先我们会通过main()方法启动:

ApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfig.class); 

或者

ApplicationContext applicationContext = new AnnotationConfigApplicationContext("包路径"); 

我们来看下AnnotationConfigApplicationContext的源码:

	// 直接解析配置注解的类
	public AnnotationConfigApplicationContext(Class<?>... annotatedClasses) {
		this();
		register(annotatedClasses);
		refresh();
	}
	// 扫描指定包路径下的所有类
	public AnnotationConfigApplicationContext(String... basePackages) {
		this();
		scan(basePackages);
		refresh();
	}

通过上面的源码,我们能看到有两种处理方式:

  • 直接解析配置注解的类
  • 扫描指定包路径下的所有类

同时我们能看到都调用了refresh()这个方法,它和前面XML的解析的refresh()是同一个方法,具体实现都是一样的,都是为了载入Bean的配置资源。所以在这里我们主要关注register(annotatedClasses);和scan(basePackages);这两个方法,它们才是独有的实现。接下来我们将会分别探究这两种处理方式的具体实现。

2.直接解析配置注解的类

开始前我们先继续看下刚才没看完的AnnotionConfigApplicationContext的源码,

	// 解析器
	private final AnnotatedBeanDefinitionReader reader;
	// 扫描器
	private final ClassPathBeanDefinitionScanner scanner;

	public AnnotationConfigApplicationContext() {
		this.reader = new AnnotatedBeanDefinitionReader(this);
		this.scanner = new ClassPathBeanDefinitionScanner(this);
	}

好,这里我们知道了AnnotionConfigApplicationContext创建的时候就初始化了AnnotatedBeanDefinitionReader这个对象,而我们知道这个对象就是解析注解Bean的解析器,后面就要用到它。

我们继续看register()方法实现,

	public void register(Class<?>... annotatedClasses) {
		Assert.notEmpty(annotatedClasses, "At least one annotated class must be specified");
		this.reader.register(annotatedClasses);
	}

我们知道实际的解析就是刚才创建的reader这个解析器负责的,那我们到AnnotatedBeanDefinitionReader看它具体的实现过程,

	//它支持解析多个注解的类
	public void register(Class<?>... annotatedClasses) {
		for (Class<?> annotatedClass : annotatedClasses) {
			registerBean(annotatedClass);
		}
	}
	
	public void registerBean(Class<?> annotatedClass) {
		doRegisterBean(annotatedClass, null, null, null);
	}

	<T> void doRegisterBean(Class<T> annotatedClass, @Nullable Supplier<T> instanceSupplier, @Nullable String name,
			@Nullable Class<? extends Annotation>[] qualifiers, BeanDefinitionCustomizer... definitionCustomizers) {
		//BeanDefinition下面的子类,对Bean对象的封装
		AnnotatedGenericBeanDefinition abd = new AnnotatedGenericBeanDefinition(annotatedClass);
		if (this.conditionEvaluator.shouldSkip(abd.getMetadata())) {
			return;
		}
		abd.setInstanceSupplier(instanceSupplier);
		//解析Bean的作用域,像@Scope("prototype"),原型类型;@Scope("singleton"),单态类型
		ScopeMetadata scopeMetadata = this.scopeMetadataResolver.resolveScopeMetadata(abd);
		//设置作用域
		abd.setScope(scopeMetadata.getScopeName());
		//生成唯一的beanName
		String beanName = (name != null ? name : this.beanNameGenerator.generateBeanName(abd, this.registry));
		//解析处理通用注解
		AnnotationConfigUtils.processCommonDefinitionAnnotations(abd);
		//如果存在限定符
		if (qualifiers != null) {
			for (Class<? extends Annotation> qualifier : qualifiers) {
				//配置@Primary注解,则Bean为自动装配时的优先选择
				if (Primary.class == qualifier) {
					abd.setPrimary(true);
				}
				//配置@Lazy注解,则延迟Bean初始化,否则预实例化
				else if (Lazy.class == qualifier) {
					abd.setLazyInit(true);
				}
				//自动装配时,根据名称装配限定符指定的Bean
				else {
					abd.addQualifier(new AutowireCandidateQualifier(qualifier));
				}
			}
		}
		for (BeanDefinitionCustomizer customizer : definitionCustomizers) {
			customizer.customize(abd);
		}
		//BeanDefinition的封装
		BeanDefinitionHolder definitionHolder = new BeanDefinitionHolder(abd, beanName);
		//根据作用域创建相应的代理对象
		definitionHolder = AnnotationConfigUtils.applyScopedProxyMode(scopeMetadata, definitionHolder, this.registry);
		//注册Bean对象
		BeanDefinitionReaderUtils.registerBeanDefinition(definitionHolder, this.registry);
	}

我们来简单分析上面源码,首先我们会通过resolveScopeMetadata()方法来解析Bean的作用域,默认的值是ScopedProxyMode.NO,它代表后面不会创建代理类;然后我们会通过processCommonDefinitionAnnotations()处理类中的通用注解,解析@Lazy、@Primy、@DependsOn、@Role、@Description这些注解类,其中如果包含@ DependsOn注解,则容器会确保实例化该Bean之前会先实例化所依赖的Bean;接下来就是根据作用域创建代理类applyScopedProxyMode(),主要是在AOP面向切面中会使用;最后就是通过registerBeanDefinition()方法向容器注册Bean,而BeanDefinitionHolder这个对象我们也很熟悉,跟XML容器初始化中的一样,最终将BeanDefinition添加到ConcurrentHashMap中去。

到这就完成配置类的直接解析过程了,我们同样梳理了初始化流程的时序图

image-20210714114631520

3.扫描指定包路径下的所有类

接下来我们来探究扫描指定包路径的解析,那我们看下scan(basePackages);方法的具体实现,

	public void scan(String... basePackages) {
		Assert.notEmpty(basePackages, "At least one base package must be specified");
		this.scanner.scan(basePackages);
	}

这里this.scanner就是AnnotionConfigApplicationContext创建时初始化的ClassPathBeanDefinitionScanner扫描器,具体的实现就是由它完成的,

	public int scan(String... basePackages) {
		//获取容器中已经注册的Bean个数
		int beanCountAtScanStart = this.registry.getBeanDefinitionCount();
		//扫描指定包
		doScan(basePackages);
		//注册注解配置处理器
		if (this.includeAnnotationConfig) {
			AnnotationConfigUtils.registerAnnotationConfigProcessors(this.registry);
		}
		//返回注册的Bean个数
		return (this.registry.getBeanDefinitionCount() - beanCountAtScanStart);
	}

	protected Set<BeanDefinitionHolder> doScan(String... basePackages) {
		Assert.notEmpty(basePackages, "At least one base package must be specified");
		Set<BeanDefinitionHolder> beanDefinitions = new LinkedHashSet<>();
		//遍历扫描所有指定包
		for (String basePackage : basePackages) {
			//调用父类ClassPathScanningCandidateComponentProvider的方法
			//扫描给定类路径,获取符合条件的Bean定义
			Set<BeanDefinition> candidates = findCandidateComponents(basePackage);
			//遍历扫描到的Bean
			for (BeanDefinition candidate : candidates) {
				//获取Bean的作用域
				ScopeMetadata scopeMetadata = this.scopeMetadataResolver.resolveScopeMetadata(candidate);
				//设置作用域
				candidate.setScope(scopeMetadata.getScopeName());
				//生成唯一Bean名称
				String beanName = this.beanNameGenerator.generateBeanName(candidate, this.registry);
				if (candidate instanceof AbstractBeanDefinition) {
                     //设置Bean的属性默认值
					postProcessBeanDefinition((AbstractBeanDefinition) candidate, beanName);
				}
				if (candidate instanceof AnnotatedBeanDefinition) {
					//解析处理通用注解
					AnnotationConfigUtils.processCommonDefinitionAnnotations((AnnotatedBeanDefinition) candidate);
				}
				//根据Bean名称检查指定的Bean是否需要在容器中注册,或者在容器中冲突
				if (checkCandidate(beanName, candidate)) {
					BeanDefinitionHolder definitionHolder = new BeanDefinitionHolder(candidate, beanName);
					//根据作用域创建相应的代理对象
					definitionHolder = AnnotationConfigUtils.applyScopedProxyMode(scopeMetadata, definitionHolder, this.registry);
					beanDefinitions.add(definitionHolder);
					//注册Bean对象
					registerBeanDefinition(definitionHolder, this.registry);
				}
			}
		}
		return beanDefinitions;

通过源码我们知道主要的处理逻辑都在doScan()方法里面得以实现,通过遍历扫描指定的包路径来获取包下面所有的注解类,返回一个Set集合来临时存放,那我们看下findCandidateComponents()方法中是怎么解析处理的,

	public Set<BeanDefinition> findCandidateComponents(String basePackage) {
         // 判断过滤规则中是否包含@Component注解,其实像@Repository、@Service、@Controller等包含了@Component
		if (this.componentsIndex != null && indexSupportsIncludeFilters()) {
			return addCandidateComponentsFromIndex(this.componentsIndex, basePackage);
		}
		else {
			return scanCandidateComponents(basePackage);
		}
	}

	private Set<BeanDefinition> addCandidateComponentsFromIndex(CandidateComponentsIndex index, String basePackage) {
		Set<BeanDefinition> candidates = new LinkedHashSet<>();
		try {
			Set<String> types = new HashSet<>();
			for (TypeFilter filter : this.includeFilters) {
				String stereotype = extractStereotype(filter);
				if (stereotype == null) {
					throw new IllegalArgumentException("Failed to extract stereotype from "+ filter);
				}
				types.addAll(index.getCandidateTypes(basePackage, stereotype));
			}
			boolean traceEnabled = logger.isTraceEnabled();
			boolean debugEnabled = logger.isDebugEnabled();
			for (String type : types) {
				//获得元数据解析器
				MetadataReader metadataReader = getMetadataReaderFactory().getMetadataReader(type);
				//判断是否符合配置的过滤规则
				if (isCandidateComponent(metadataReader)) {
					AnnotatedGenericBeanDefinition sbd = new AnnotatedGenericBeanDefinition(
							metadataReader.getAnnotationMetadata());
					if (isCandidateComponent(sbd)) {
						if (debugEnabled) {
							logger.debug("Using candidate component class from index: " + type);
						}
						candidates.add(sbd);
					}
					else {
						if (debugEnabled) {
							logger.debug("Ignored because not a concrete top-level class: " + type);
						}
					}
				}
				else {
					if (traceEnabled) {
						logger.trace("Ignored because matching an exclude filter: " + type);
					}
				}
			}
		}
		catch (IOException ex) {
			throw new BeanDefinitionStoreException("I/O failure during classpath scanning", ex);
		}
		return candidates;
	}

这里的代码逻辑比较易懂,就是判断扫描的类是否满足注解类的解析规则,如果满足就添加到Set集合中,最后返回到上一层,而这个解析规则其实是在ClassPathBeanDefinitionScanner的构造方法中初始化的Spring的默认注解规则。

我们也返回上一层,看findCandidateComponents(basePackage)之后的代码,发现接下来就是对Set集合中的Bean进行遍历处理,而处理逻辑饿代码是不是似曾相识,和我们上面直接解析配置注解的类中的处理是不是一模一样,最后也是调用registerBeanDefinition()方法来注册Bean对象到IOC容器中去。

到这就完成扫描指定包路径下的所有类的注册过程了,我们也同样梳理了初始化流程的时序图

image-20210714163601286

总结

经过上面长文的源码解析,再通过时序图、流程图的整理,我们已经能比较详细的知道IOC容器的初始化流程,但是一些处理细节没有过多的深究,有兴趣的可以再找机会看看。最后我们再从顶层设计的层面来梳理一下整个IOC的初始化流程:

  1. 通过ResourceLoader从类路径、文件系统、URL等方式来定位资源文件位置;
  2. 配置Bean数据的文件会被抽象成Resource来处理;
  3. 容器通过BeanDefinitionReader来解析Resource,而实际处理过程是委托BeanDefinitionParserDelegate来完成的;
  4. 实现BeanDefinitionRegistry接口的子类将BeanDefinition注册到容器中;
  5. 容器的本质就是维护一个ConcurrentHashMap来保存BeanDefinition,后续Bean的操作都是围绕这个ConcurrentHashMap来实现的;
  6. 使用的时候我们通过BeanFactory和ApplicaitonContext来获得这些Bean对象。

参考资料:

  • Spring官网文档
  • Tom老师的Spring笔记
  • Spring源码深度解析书籍

把一件事做到极致就是天分!

公众号:风动草

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值