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实现本地缓存的常用方案介绍》本地缓存的代表技术主要有HashMap,GuavaCache,Caffeine和Encahche,这篇文章主要来和大家聊聊java利用这些技术分别实现本地缓存的方... 目录本地缓存实现方式HashMapConcurrentHashMapGuava CacheCaffe

SpringBoot整合Sa-Token实现RBAC权限模型的过程解析

《SpringBoot整合Sa-Token实现RBAC权限模型的过程解析》:本文主要介绍SpringBoot整合Sa-Token实现RBAC权限模型的过程解析,本文给大家介绍的非常详细,对大家的学... 目录前言一、基础概念1.1 RBAC模型核心概念1.2 Sa-Token核心功能1.3 环境准备二、表结

Python函数返回多个值的多种方法小结

《Python函数返回多个值的多种方法小结》在Python中,函数通常用于封装一段代码,使其可以重复调用,有时,我们希望一个函数能够返回多个值,Python提供了几种不同的方法来实现这一点,需要的朋友... 目录一、使用元组(Tuple):二、使用列表(list)三、使用字典(Dictionary)四、 使

Linux查看系统盘和SSD盘的容量、型号及挂载信息的方法

《Linux查看系统盘和SSD盘的容量、型号及挂载信息的方法》在Linux系统中,管理磁盘设备和分区是日常运维工作的重要部分,而lsblk命令是一个强大的工具,它用于列出系统中的块设备(blockde... 目录1. 查看所有磁盘的物理信息方法 1:使用 lsblk(推荐)方法 2:使用 fdisk -l(

eclipse如何运行springboot项目

《eclipse如何运行springboot项目》:本文主要介绍eclipse如何运行springboot项目问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目js录当在eclipse启动spring boot项目时出现问题解决办法1.通过cmd命令行2.在ecl

Java中的Closeable接口及常见问题

《Java中的Closeable接口及常见问题》Closeable是Java中的一个标记接口,用于表示可以被关闭的对象,它定义了一个标准的方法来释放对象占用的系统资源,下面给大家介绍Java中的Clo... 目录1. Closeable接口概述2. 主要用途3. 实现类4. 使用方法5. 实现自定义Clos

Jvm sandbox mock机制的实践过程

《Jvmsandboxmock机制的实践过程》:本文主要介绍Jvmsandboxmock机制的实践过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、背景二、定义一个损坏的钟1、 Springboot工程中创建一个Clock类2、 添加一个Controller

SpringBoot实现文件记录日志及日志文件自动归档和压缩

《SpringBoot实现文件记录日志及日志文件自动归档和压缩》Logback是Java日志框架,通过Logger收集日志并经Appender输出至控制台、文件等,SpringBoot配置logbac... 目录1、什么是Logback2、SpringBoot实现文件记录日志,日志文件自动归档和压缩2.1、

MQTT SpringBoot整合实战教程

《MQTTSpringBoot整合实战教程》:本文主要介绍MQTTSpringBoot整合实战教程,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考... 目录MQTT-SpringBoot创建简单 SpringBoot 项目导入必须依赖增加MQTT相关配置编写

spring-gateway filters添加自定义过滤器实现流程分析(可插拔)

《spring-gatewayfilters添加自定义过滤器实现流程分析(可插拔)》:本文主要介绍spring-gatewayfilters添加自定义过滤器实现流程分析(可插拔),本文通过实例图... 目录需求背景需求拆解设计流程及作用域逻辑处理代码逻辑需求背景公司要求,通过公司网络代理访问的请求需要做请