Spring 源码解析~13、Spring 中的钩子函数汇总

Spring 中的钩子函数汇总

一、生命周期总览

请添加图片描述

二、BeanDefinition 生成与注册阶段

钩子执行顺序与博文顺序一致,即 1->n

1、EmptyReaderEventListener#defaultsRegistered

  • 触发点:创建 BeanDefinitionParserDelegate 委派类时触发
  • 解释:通知默认 BeanDefinitionParserDelegate 已注册。

DefaultBeanDefinitionDocumentReader#createDelegate

protected BeanDefinitionParserDelegate createDelegate(
    XmlReaderContext readerContext, Element root, @Nullable BeanDefinitionParserDelegate parentDelegate) {

    BeanDefinitionParserDelegate delegate = new BeanDefinitionParserDelegate(readerContext);
    delegate.initDefaults(root, parentDelegate);
    return delegate;
}

2、preProcessXml

  • 触发点:DefaultBeanDefinitionDocumentReader#preProcessXml
  • 解释:xml 配置文件解析前置钩子

3、EmptyReaderEventListener

package org.springframework.beans.factory.parsing;

import java.util.EventListener;

/**
 * Interface that receives callbacks for component, alias and import
 * registrations during a bean definition reading process.
 *
 * @author Rob Harrop
 * @author Juergen Hoeller
 * @since 2.0
 * @see ReaderContext
 */
public interface ReaderEventListener extends EventListener {

	/**
	 * Notification that the given defaults has been registered.
	 * @param defaultsDefinition a descriptor for the defaults
	 * @see org.springframework.beans.factory.xml.DocumentDefaultsDefinition
	 */
	void defaultsRegistered(DefaultsDefinition defaultsDefinition);

	/**
	 * Notification that the given component has been registered.
	 * @param componentDefinition a descriptor for the new component
	 * @see BeanComponentDefinition
	 */
	void componentRegistered(ComponentDefinition componentDefinition);

	/**
	 * Notification that the given alias has been registered.
	 * @param aliasDefinition a descriptor for the new alias
	 */
	void aliasRegistered(AliasDefinition aliasDefinition);

	/**
	 * Notification that the given import has been processed.
	 * @param importDefinition a descriptor for the import
	 */
	void importProcessed(ImportDefinition importDefinition);

}
1)、EmptyReaderEventListener#componentRegistered
  • 触发点:Bean 类型标签已注册时
  • 解释:通知 Bean 类型的标签已注册
2)、EmptyReaderEventListener#aliasRegistered
  • 触发点:alias 类型标签已注册时
  • 解释:通知 alias 类型的标签已注册
3)、EmptyReaderEventListener#importProcessed
  • 触发点:import 类型标签已注册时
  • 解释:通知 import 类型的标签已注册

注意:这三者不分顺序,哪类标签在配置文件的位置靠前,就先解析

4、postProcessXml

  • xml 配置文件解析后置钩子
  • DefaultBeanDefinitionDocumentReader#postProcessXml

三、bean 实例加载阶段

1、BeanFactory 容器类型

钩子执行顺序与博文顺序一致,即 1->n

  • 1)、@PostConstruct
  • 2)、InitializingBean
  • 3)、init-method
  • 4)、@PreDestroy
  • 5)、DisposableBean
  • 6)、destroy-method

2、ApplicationContext 容器类型

  • 1)、AbstractApplicationContext#initPropertySources
  • 2)、AbstractApplicationContext 其他可被子类重写的方法
  • 3)、BeanNameAware
  • 4)、BeanClassLoaderAware
  • 5)、ApplicationContextAware
  • 6)、其他类型 Aware
  • 7)、AbstractApplicationContext#postProcessBeanFactory
  • 8)、执行注册的 BeanFactoryPostProcessor#postProcessBeanDefinitionRegistry
  • 9)、AbstractApplicationContext#onRefresh
  • 10)、广播 earlyApplicationEvents(容器启动就发布的事件)
  • 11)、配置 Converter<S, T>
  • 12)、初始化 bean 之前执行 BeanPostProcessor#postProcessBeforeInitialization
  • 9)、@PostConstruct
  • 10)、InitializingBean
  • 11)、init-method
  • 12)、初始化 bean 后执行 BeanPostProcessor#postProcessAfterInitialization
  • 12)、程序执行中触发的 ApplicationListener
  • 8)、@PreDestroy
  • 9)、DisposableBean
  • 10)、destroy-method

使用 ApplicationContext.registerShutdownHook() 关闭钩子,让销毁回调函数自动触发

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring源码解析是指对Spring框架的源代码进行深入分析和解读的过程。Spring框架是一个开源的Java企业级应用程序框架,提供了高度灵活和可扩展的开发环境。 Spring框架的源代码解析涉及了众多的模块和组件,包括核心容器、AOP(面向切面编程)、数据访问、Web开发等。通过对这些模块和组件的源代码进行解析,我们可以更加深入地了解Spring框架的工作原理和设计思想。 Spring源码解析的好处在于,可以帮助我们更好地理解Spring框架的各种功能和特性,并且能够帮助开发人员更加高效地使用和定制Spring框架,解决实际项目开发的问题。 在进行Spring源码解析时,我们可以关注一些关键的概念和类,比如BeanFactory、ApplicationContext、BeanPostProcessor、AOP代理等。这些核心类和概念是理解Spring框架工作机制的重要基础。 进行Spring源码解析时,我们可以使用一些常见的工具和方法,比如IDE(集成开发环境)的调试功能、查看和分析源代码的注释和文档、调试和运行项目的示例代码等。 通过Spring源码解析,我们可以学到很多有关软件开发的知识和经验,比如面向对象编程、设计模式、依赖注入、控制反转等。这些知识和经验对于我们提升自己的技术水平和解决实际项目的问题都有很大的帮助。 总之,Spring源码解析是一项非常有价值的学习和研究工作,可以帮助我们更好地理解和应用Spring框架,提高自己的技术能力和软件开发水平。希望以上的回答能够满足您的需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值