博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring源码 17 IOC refresh方法12
阅读量:37194 次
发布时间:2020-08-01

本文共 5193 字,大约阅读时间需要 17 分钟。

参考源

《Spring源码深度解析(第2版)》

版本

本文章基于 Spring 5.3.15


Spring IOC 的核心是 AbstractApplicationContextrefresh 方法。

其中一共有 13 个主要方法,这里分析第 12 个:finishRefresh

1 AbstractApplicationContext

1-1 完成刷新过程

finishRefresh()
protected void finishRefresh() {   // 清除资源缓存   clearResourceCaches();   // 初始化生命周期处理器   initLifecycleProcessor();   // 获取生命周期处理器   // 刷新生命周期处理器   getLifecycleProcessor().onRefresh();   // 发布事件   publishEvent(new ContextRefreshedEvent(this));   if (!NativeDetector.inNativeImage()) {      LiveBeansView.registerApplicationContext(this);   }}

1-2 清除资源缓存

clearResourceCaches()

2 DefaultResourceLoader

private final Map
, Map
> resourceCaches = new ConcurrentHashMap<>(4);public void clearResourceCaches() { this.resourceCaches.clear();}

1 AbstractApplicationContext

1-2 初始化生命周期处理器

initLifecycleProcessor()
protected void initLifecycleProcessor() {   ConfigurableListableBeanFactory beanFactory = getBeanFactory();   if (beanFactory.containsLocalBean(LIFECYCLE_PROCESSOR_BEAN_NAME)) {      this.lifecycleProcessor = beanFactory.getBean(LIFECYCLE_PROCESSOR_BEAN_NAME, LifecycleProcessor.class);      if (logger.isTraceEnabled()) {         logger.trace("Using LifecycleProcessor [" + this.lifecycleProcessor + "]");      }   } else {      DefaultLifecycleProcessor defaultProcessor = new DefaultLifecycleProcessor();      defaultProcessor.setBeanFactory(beanFactory);      this.lifecycleProcessor = defaultProcessor;      beanFactory.registerSingleton(LIFECYCLE_PROCESSOR_BEAN_NAME, this.lifecycleProcessor);      if (logger.isTraceEnabled()) {         logger.trace("No '" + LIFECYCLE_PROCESSOR_BEAN_NAME + "' bean, using " + "[" + this.lifecycleProcessor.getClass().getSimpleName() + "]");      }   }}

1-2 获取生命周期处理器

getLifecycleProcessor()
private ResourcePatternResolver resourcePatternResolver;LifecycleProcessor getLifecycleProcessor() throws IllegalStateException {   if (this.lifecycleProcessor == null) {      throw new IllegalStateException("LifecycleProcessor not initialized - " + "call 'refresh' before invoking lifecycle methods via the context: " + this);   }   return this.lifecycleProcessor;}

if (this.lifecycleProcessor == null) 由于 this.lifecycleProcessor 在前面已经定义,这里直接返回。

1-2 刷新生命周期处理器

onRefresh()

3 DefaultLifecycleProcessor

public void onRefresh() {   // 启动 Beans   startBeans(true);   this.running = true;}

3-1 启动 Beans

startBeans(true)
private void startBeans(boolean autoStartupOnly) {   // 获取 Bean 生命周期    Map
lifecycleBeans = getLifecycleBeans(); Map
phases = new TreeMap<>(); lifecycleBeans.forEach((beanName, bean) -> { if (!autoStartupOnly || (bean instanceof SmartLifecycle && ((SmartLifecycle) bean).isAutoStartup())) { int phase = getPhase(bean); phases.computeIfAbsent( phase, p -> new LifecycleGroup(phase, this.timeoutPerShutdownPhase, lifecycleBeans, autoStartupOnly) ).add(beanName, bean); } }); if (!phases.isEmpty()) { phases.values().forEach(LifecycleGroup::start); }}

3-2 获取 Bean 生命周期

getLifecycleBeans()
protected Map
getLifecycleBeans() { // 获取 Bean 工厂 ConfigurableListableBeanFactory beanFactory = getBeanFactory(); Map
beans = new LinkedHashMap<>(); String[] beanNames = beanFactory.getBeanNamesForType(Lifecycle.class, false, false); for (String beanName : beanNames) { String beanNameToRegister = BeanFactoryUtils.transformedBeanName(beanName); boolean isFactoryBean = beanFactory.isFactoryBean(beanNameToRegister); String beanNameToCheck = (isFactoryBean ? BeanFactory.FACTORY_BEAN_PREFIX + beanName : beanName); if ((beanFactory.containsSingleton(beanNameToRegister) && (!isFactoryBean || matchesBeanType(Lifecycle.class, beanNameToCheck, beanFactory))) || matchesBeanType(SmartLifecycle.class, beanNameToCheck, beanFactory)) { Object bean = beanFactory.getBean(beanNameToCheck); if (bean != this && bean instanceof Lifecycle) { beans.put(beanNameToRegister, (Lifecycle) bean); } } } return beans;}

1 AbstractApplicationContext

1-2 发布事件

publishEvent(new ContextRefreshedEvent(this))
public void publishEvent(ApplicationEvent event) {   publishEvent(event, null);}
protected void publishEvent(Object event, @Nullable ResolvableType eventType) {   Assert.notNull(event, "Event must not be null");   ApplicationEvent applicationEvent;   if (event instanceof ApplicationEvent) {      applicationEvent = (ApplicationEvent) event;   } else {      applicationEvent = new PayloadApplicationEvent<>(this, event);      if (eventType == null) {         eventType = ((PayloadApplicationEvent
) applicationEvent).getResolvableType(); } } if (this.earlyApplicationEvents != null) { this.earlyApplicationEvents.add(applicationEvent); } else { // 获取应用程序事件多播器 // 多播事件 getApplicationEventMulticaster().multicastEvent(applicationEvent, eventType); } if (this.parent != null) { if (this.parent instanceof AbstractApplicationContext) { ((AbstractApplicationContext) this.parent).publishEvent(event, eventType); } else { this.parent.publishEvent(event); } }}

转载地址:http://zypwwy.baihongyu.com/

你可能感兴趣的文章
binlog server伪装master恢复增量数据
查看>>
最受欢迎的微服务框架概览
查看>>
自己动手写SQL执行引擎
查看>>
关于数据库、数据治理、AIOps的这些痛点,你需要知道! | DAMS 2020
查看>>
MySQL流转工具Maxwell的代码改造和优化小结
查看>>
互动赠书:简历中常见的问题,献给1024的骚年
查看>>
与 DevOps 面对面
查看>>
CPU占用又爆了?MySQL到底在干什么
查看>>
招贤纳士-第17期,来自北京,西安的职位
查看>>
秋招拿了7个offer,分享一些反思和经验
查看>>
一文带你深扒ClassLoader内核,揭开它的神秘面纱!
查看>>
看GitHub 2020年度报告有感
查看>>
MySQL如何管理客户端连接?线程池篇
查看>>
VScode 折叠函数快捷键 合上函数
查看>>
智能家居传感器:BME680--树莓派3B+ 搭配BME680的数据读取温湿度和气压。树莓派IIC BME680算法库 (未完成版本)
查看>>
domoticz智能家居系统 MQTT 异常以及解决方法 code=14
查看>>
智能家居传感器:BME680--树莓派3B+ 搭配BME680的数据读取温湿度和气压。树莓派IIC BME680算法库完整版
查看>>
lua语言笔记--注册dll内的函数到全局,lua 全局函数的注册
查看>>
工作笔记::c++ 运行命令行脚本 启动lua cmd lua 顺序执行多个文件的方法 c++ 开启一个命令行的方法 lua 启动多个文件
查看>>
工作笔记::lua 打印 一个table的方法
查看>>