2、spring5.2.x源码解读之IOC实现流程

1、代码调用入口

public static void main(String[] args) {
		// 用我们的配置文件来启动一个 ApplicationContext
		ApplicationContext context = new ClassPathXmlApplicationContext("classpath:application.xml");
		// 从 context 中取出我们的 Bean,而不是用 new MessageServiceImpl() 这种方式
		IMessageService messageService = context.getBean(IMessageService.class);
		// 这句将输出: hello world
		messageService.say("hello world");
	}	
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	   xmlns:context="http://www.springframework.org/schema/context"
	   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
	<bean id="messageService" class="org.springframework.test.web.test.service.impl.MessageServiceImpl"/>
	<context:component-scan base-package="org.springframework.test.web.test" />
</beans>

2、IOC实现流程

在这里插入图片描述

public ClassPathXmlApplicationContext(
			String[] configLocations, boolean refresh, @Nullable ApplicationContext parent)
			throws BeansException {

		super(parent);
		setConfigLocations(configLocations);
		if (refresh) {
			//spring加载对象到map的入口
			refresh();
		}
	}
@Override
	public void refresh() throws BeansException, IllegalStateException {
		synchronized (this.startupShutdownMonitor) {
			//为刷新容器做准备
			prepareRefresh();

			//获得刷新后的beanFactory
			ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

			//初始化beanFactory需要的一些参数
			prepareBeanFactory(beanFactory);

			try {
				//后置处理bean工厂,接口实现类
				postProcessBeanFactory(beanFactory);

				//执行后置处理bean工厂
				invokeBeanFactoryPostProcessors(beanFactory);

				//注册后置处理bean
				registerBeanPostProcessors(beanFactory);

				//初始化容器的消息源
				initMessageSource();

				//初始化应用事件广播器
				initApplicationEventMulticaster();

				//在特殊容器初始化其它特殊bean
				onRefresh();

				//检查监听对象和注册监听对象
				registerListeners();

				// 初始化所有的 singleton beans
				finishBeanFactoryInitialization(beanFactory);

				// Last step: publish corresponding event.
				finishRefresh();
			}

			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();
			}
		}
	}
@Override
	protected final void refreshBeanFactory() throws BeansException {
		if (hasBeanFactory()) {
			destroyBeans();
			closeBeanFactory();
		}
		try {
			DefaultListableBeanFactory beanFactory = createBeanFactory();
			beanFactory.setSerializationId(getId());
			//定制bean工厂
			customizeBeanFactory(beanFactory);
			//加载bean定义的配置文件,application.xml
			loadBeanDefinitions(beanFactory);
			this.beanFactory = beanFactory;
		}
		catch (IOException ex) {
			throw new ApplicationContextException("I/O error parsing bean definition source for " + getDisplayName(), ex);
		}
	}

相关推荐

  1. iOS】—— SDWebImage学习(2)(解读

    2024-07-12 18:42:09       30 阅读
  2. springpublishEvent解析

    2024-07-12 18:42:09       54 阅读
  3. Vue探索Vue2.x分析(一)

    2024-07-12 18:42:09       43 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-12 18:42:09       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-12 18:42:09       72 阅读
  3. 在Django里面运行非项目文件

    2024-07-12 18:42:09       58 阅读
  4. Python语言-面向对象

    2024-07-12 18:42:09       69 阅读

热门阅读

  1. 力扣题解(等差数列划分)

    2024-07-12 18:42:09       22 阅读
  2. ES6 Module 的语法(十二)

    2024-07-12 18:42:09       18 阅读
  3. 王者荣耀爬虫程序

    2024-07-12 18:42:09       22 阅读
  4. yarn的安装与配置 (秒懂yarn用法)

    2024-07-12 18:42:09       19 阅读
  5. 错误集1

    2024-07-12 18:42:09       20 阅读
  6. ES6 async 函数详解 (十)

    2024-07-12 18:42:09       21 阅读
  7. Linux下如何解压rar文件

    2024-07-12 18:42:09       24 阅读
  8. C# 建造者模式(Builder Pattern)

    2024-07-12 18:42:09       23 阅读
  9. Warning: could not connect to a running Ollama instance

    2024-07-12 18:42:09       18 阅读
  10. 大语言模型

    2024-07-12 18:42:09       20 阅读
  11. EasyExcel文档链接与使用示例

    2024-07-12 18:42:09       17 阅读