Spring Boot-10-run方法之AbstractApplicationContext#refresh()

2024-03-11 22:48

本文主要是介绍Spring Boot-10-run方法之AbstractApplicationContext#refresh(),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

本文源码基于Spring 5.2.7
Spring中,容器的填充全靠org.springframework.context.support.AbstractApplicationContext#refresh()方法,这个方法的每一步都是值得分析的,这每一步都会分为一篇或多篇文章来解析,这里对这些步骤作一个整体说明。
Spring的核心能力就是作为控制反转容器使用,通过名称可以看出,这个方法是用来刷新容器的,可想而知这个方法包含了Spring核心能力,包括AOP的处理过程都在这里。

作为一个服务框架,Spring提供了强大的扩展能力,Spring的设计是定义很多post processor接口,启动时,会按照次序执行这些post processor,这样,用户可以自行在启动的不同时间节点,扩展自己的功能,这种设计方式就是模板方法。

org.springframework.context.support.AbstractApplicationContext#refresh()

@Override
public void refresh() throws BeansException, IllegalStateException {synchronized (this.startupShutdownMonitor) {// Prepare this context for refreshing.prepareRefresh();// Tell the subclass to refresh the internal bean factory.ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();// Prepare the bean factory for use in this context.prepareBeanFactory(beanFactory);try {// Allows post-processing of the bean factory in context subclasses.postProcessBeanFactory(beanFactory);// Invoke factory processors registered as beans in the context.invokeBeanFactoryPostProcessors(beanFactory);// Register bean processors that intercept bean creation.registerBeanPostProcessors(beanFactory);// Initialize message source for this context.initMessageSource();// Initialize event multicaster for this context.initApplicationEventMulticaster();// Initialize other special beans in specific context subclasses.onRefresh();// Check for listener beans and register them.registerListeners();// Instantiate all remaining (non-lazy-init) singletons.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();}}
}

一、postProcessBeanFactory()

// Allows post-processing of the bean factory in context subclasses.
postProcessBeanFactory(beanFactory);

先看这个方法,这句话的意思就是调用AbstractApplicationContext的子类的postProcessBeanFactory()方法,这个描述容易和调用BeanFactoryPostProcessor类这个过程混淆,这个模板方法是留给Spring内部扩展使用的,开发者无法介入。
org.springframework.context.support.AbstractApplicationContext#postProcessBeanFactory()
在application context标准初始化完成后修改它内部的bean factory。此时所有的bean definitions已经被加载,但是bean都还没有初始化。这样的话就能够在某个ApplicationContext的实现类中注册特定的BeanPostProcessors。
AbstractApplicationContext中的这个方法是个空方法,留给子类去实现。

/*** Modify the application context's internal bean factory after its standard* initialization. All bean definitions will have been loaded, but no beans* will have been instantiated yet. This allows for registering special* BeanPostProcessors etc in certain ApplicationContext implementations.* @param beanFactory the bean factory used by the application context*/
protected void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {
}

二、invokeBeanFactoryPostProcessors()

// Invoke factory processors registered as beans in the context.
invokeBeanFactoryPostProcessors(beanFactory);

然后是invokeBeanFactoryPostProcessors(),这个方法的作用是调用在容器注册的BeanFactoryPostProcessor类方法,BeanFactoryPostProcessor是留给Spring内部以及开发者扩展的接口,前提是要注册到容器中来。 
 

这里定义了2种类型的post processor,分别是
org.springframework.beans.factory.config.BeanFactoryPostProcessor
org.springframework.beans.factory.config.BeanPostProcessor
开发者可以定义自己的post processor,且执行顺序是先执行BeanFactoryPostProcessor后执行BeanPostProcessor,同一种类型的post processor可以定义优先级来控制执行顺序,这样开发者就能在启动时扩展自己的功能。

这篇关于Spring Boot-10-run方法之AbstractApplicationContext#refresh()的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/799324

相关文章

Java实现检查多个时间段是否有重合

《Java实现检查多个时间段是否有重合》这篇文章主要为大家详细介绍了如何使用Java实现检查多个时间段是否有重合,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录流程概述步骤详解China编程步骤1:定义时间段类步骤2:添加时间段步骤3:检查时间段是否有重合步骤4:输出结果示例代码结语作

Nginx设置连接超时并进行测试的方法步骤

《Nginx设置连接超时并进行测试的方法步骤》在高并发场景下,如果客户端与服务器的连接长时间未响应,会占用大量的系统资源,影响其他正常请求的处理效率,为了解决这个问题,可以通过设置Nginx的连接... 目录设置连接超时目的操作步骤测试连接超时测试方法:总结:设置连接超时目的设置客户端与服务器之间的连接

Java中String字符串使用避坑指南

《Java中String字符串使用避坑指南》Java中的String字符串是我们日常编程中用得最多的类之一,看似简单的String使用,却隐藏着不少“坑”,如果不注意,可能会导致性能问题、意外的错误容... 目录8个避坑点如下:1. 字符串的不可变性:每次修改都创建新对象2. 使用 == 比较字符串,陷阱满

Java判断多个时间段是否重合的方法小结

《Java判断多个时间段是否重合的方法小结》这篇文章主要为大家详细介绍了Java中判断多个时间段是否重合的方法,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录判断多个时间段是否有间隔判断时间段集合是否与某时间段重合判断多个时间段是否有间隔实体类内容public class D

Python使用国内镜像加速pip安装的方法讲解

《Python使用国内镜像加速pip安装的方法讲解》在Python开发中,pip是一个非常重要的工具,用于安装和管理Python的第三方库,然而,在国内使用pip安装依赖时,往往会因为网络问题而导致速... 目录一、pip 工具简介1. 什么是 pip?2. 什么是 -i 参数?二、国内镜像源的选择三、如何

IDEA编译报错“java: 常量字符串过长”的原因及解决方法

《IDEA编译报错“java:常量字符串过长”的原因及解决方法》今天在开发过程中,由于尝试将一个文件的Base64字符串设置为常量,结果导致IDEA编译的时候出现了如下报错java:常量字符串过长,... 目录一、问题描述二、问题原因2.1 理论角度2.2 源码角度三、解决方案解决方案①:StringBui

Linux使用nload监控网络流量的方法

《Linux使用nload监控网络流量的方法》Linux中的nload命令是一个用于实时监控网络流量的工具,它提供了传入和传出流量的可视化表示,帮助用户一目了然地了解网络活动,本文给大家介绍了Linu... 目录简介安装示例用法基础用法指定网络接口限制显示特定流量类型指定刷新率设置流量速率的显示单位监控多个

Java覆盖第三方jar包中的某一个类的实现方法

《Java覆盖第三方jar包中的某一个类的实现方法》在我们日常的开发中,经常需要使用第三方的jar包,有时候我们会发现第三方的jar包中的某一个类有问题,或者我们需要定制化修改其中的逻辑,那么应该如何... 目录一、需求描述二、示例描述三、操作步骤四、验证结果五、实现原理一、需求描述需求描述如下:需要在

Java中ArrayList和LinkedList有什么区别举例详解

《Java中ArrayList和LinkedList有什么区别举例详解》:本文主要介绍Java中ArrayList和LinkedList区别的相关资料,包括数据结构特性、核心操作性能、内存与GC影... 目录一、底层数据结构二、核心操作性能对比三、内存与 GC 影响四、扩容机制五、线程安全与并发方案六、工程

JavaScript中的reduce方法执行过程、使用场景及进阶用法

《JavaScript中的reduce方法执行过程、使用场景及进阶用法》:本文主要介绍JavaScript中的reduce方法执行过程、使用场景及进阶用法的相关资料,reduce是JavaScri... 目录1. 什么是reduce2. reduce语法2.1 语法2.2 参数说明3. reduce执行过程