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() 关闭钩子,让销毁回调函数自动触发
















![[oeasy]python0043_八进制_oct_octal_october_octave](https://img-blog.csdnimg.cn/img_convert/5122264eb7a1275ed77fbedddfa20c19.png)


