Spring 5.2.9 07 spring的bean创建流程

Date: 10/01/2022
Week13

概念:事件驱动

在这里插入图片描述

SimpleEvent关系类

ApplicationEventMulticaster
在这里插入图片描述

Spring的refresh()方法事件周期

1#事件
2#监听器
3#多播器
4#事件源
//程序入口
MyClassPathXmlApplicationContext.MyClassPathXmlApplicationContext(String... configLocations){}
ClassPathXmlApplicationContext(String... configLocations) throws BeansException {}
ClassPathXmlApplicationContext.ClassPathXmlApplicationContext(String... configLocations) throws BeansException {}
this(configLocations, true, null);
		#重复方法, enter
		this(configLocations, true, null);
			refresh();
#spring核心代码#refresh();
//Spring源码核心,所有初始化信息的加载过程在全部都在这里。
				1#事件
				// Initialize event multicaster for this context.
				// 初始化事件监听多路广播器
				initApplicationEventMulticaster();
				2#监听器
				// 在所有注册的bean中查找listener bean,注册到消息广播器中
				registerListeners();
				3#多播器
				// 完成刷新过程,通知生命周期处理器lifecycleProcessor刷新过程,同时发出ContextRefreshEvent通知别人
				finishRefresh();
		// 发布最终事件
		// 新建ContextRefreshedEvent事件对象,将其发布到所有监听器。
		publishEvent(new ContextRefreshedEvent(this));
			// 多播applicationEvent到适当的监听器
			getApplicationEventMulticaster().multicastEvent(applicationEvent, eventType);
				4#事件源
				// 将event多播到所有适合的监听器。如果event不是ApplicationEvent实例,会将其封装成PayloadApplicationEvent对象再进行多播
				((AbstractApplicationContext) this.parent).publishEvent(event, eventType);
				

补充部分:

AbstractAutowireCapableBeanFactory

创建Bean对象,
instanceWrapper = createBeanInstance(beanName, mbd, args);

AbstractApplicationContext

创建多播器

			beanFactory.registerSingleton(APPLICATION_EVENT_MULTICASTER_BEAN_NAME, this.applicationEventMulticaster);
			addSingleton(beanName, singletonObject);
			
APPLICATION_EVENT_MULTICASTER_BEAN_NAME
	protected void addSingleton(String beanName, Object singletonObject) {
		synchronized (this.singletonObjects) {
			// 将映射关系添加到单例对象的高速缓存中
			this.singletonObjects.put(beanName, singletonObject);
			// 移除beanName在单例工厂缓存中的数据
			this.singletonFactories.remove(beanName);
			// 移除beanName在早期单例对象的高速缓存的数据
			this.earlySingletonObjects.remove(beanName);
			// 将beanName添加到已注册的单例集中
			this.registeredSingletons.add(beanName);
		}
	}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值