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如何解压zip压缩包

《java如何解压zip压缩包》:本文主要介绍java如何解压zip压缩包问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Java解压zip压缩包实例代码结果如下总结java解压zip压缩包坐在旁边的小伙伴问我怎么用 java 将服务器上的压缩文件解压出来,

SpringBoot中SM2公钥加密、私钥解密的实现示例详解

《SpringBoot中SM2公钥加密、私钥解密的实现示例详解》本文介绍了如何在SpringBoot项目中实现SM2公钥加密和私钥解密的功能,通过使用Hutool库和BouncyCastle依赖,简化... 目录一、前言1、加密信息(示例)2、加密结果(示例)二、实现代码1、yml文件配置2、创建SM2工具

Spring WebFlux 与 WebClient 使用指南及最佳实践

《SpringWebFlux与WebClient使用指南及最佳实践》WebClient是SpringWebFlux模块提供的非阻塞、响应式HTTP客户端,基于ProjectReactor实现,... 目录Spring WebFlux 与 WebClient 使用指南1. WebClient 概述2. 核心依

SQL Server配置管理器无法打开的四种解决方法

《SQLServer配置管理器无法打开的四种解决方法》本文总结了SQLServer配置管理器无法打开的四种解决方法,文中通过图文示例介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的... 目录方法一:桌面图标进入方法二:运行窗口进入检查版本号对照表php方法三:查找文件路径方法四:检查 S

MyBatis-Plus 中 nested() 与 and() 方法详解(最佳实践场景)

《MyBatis-Plus中nested()与and()方法详解(最佳实践场景)》在MyBatis-Plus的条件构造器中,nested()和and()都是用于构建复杂查询条件的关键方法,但... 目录MyBATis-Plus 中nested()与and()方法详解一、核心区别对比二、方法详解1.and()

Spring Boot @RestControllerAdvice全局异常处理最佳实践

《SpringBoot@RestControllerAdvice全局异常处理最佳实践》本文详解SpringBoot中通过@RestControllerAdvice实现全局异常处理,强调代码复用、统... 目录前言一、为什么要使用全局异常处理?二、核心注解解析1. @RestControllerAdvice2

Spring IoC 容器的使用详解(最新整理)

《SpringIoC容器的使用详解(最新整理)》文章介绍了Spring框架中的应用分层思想与IoC容器原理,通过分层解耦业务逻辑、数据访问等模块,IoC容器利用@Component注解管理Bean... 目录1. 应用分层2. IoC 的介绍3. IoC 容器的使用3.1. bean 的存储3.2. 方法注

golang中reflect包的常用方法

《golang中reflect包的常用方法》Go反射reflect包提供类型和值方法,用于获取类型信息、访问字段、调用方法等,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值... 目录reflect包方法总结类型 (Type) 方法值 (Value) 方法reflect包方法总结

Spring事务传播机制最佳实践

《Spring事务传播机制最佳实践》Spring的事务传播机制为我们提供了优雅的解决方案,本文将带您深入理解这一机制,掌握不同场景下的最佳实践,感兴趣的朋友一起看看吧... 目录1. 什么是事务传播行为2. Spring支持的七种事务传播行为2.1 REQUIRED(默认)2.2 SUPPORTS2

C# 比较两个list 之间元素差异的常用方法

《C#比较两个list之间元素差异的常用方法》:本文主要介绍C#比较两个list之间元素差异,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录1. 使用Except方法2. 使用Except的逆操作3. 使用LINQ的Join,GroupJoin