AnnotationConfigServletWebServerApplicationContext.refresh
ServletWebServerApplicationContext.selfInitialize
Spring Bean 在整个 SpringBoot 项目中至关重要,它经过的路径如下:
- 实例化 【IOC 容器寻找 Bean 的定义信息并将其实例化】
- 设置 bean 的 Aware 【Aware 意指能提前感知的,是 spring 的一个重要接口,使用依赖注入,spring 按照 Bean 定义信息配置 Bean 的所有属性】
- BeanPostProcessor.postProcessBeforeInitialization(Object bean, String beanName) 【如果 BeanPostProcessor 和 Bean 关联,那么其 postProcessBeforeInitialization () 方法将被调用,Spring 框架会遍历得到容器中所有的 BeanPostProcessor ,挨个执行】
- InitializingBean.afterPropertiesSet【初始化 bean, springboot 读取 properties 文件的过程,默认的 application.properties 还有其他方式】
- BeanPostProcessor.postProcessAfterInitialization(Object bean, String beanName) 【如果有 BeanPostProcessor 和 Bean 关联,那么其 postProcessAfterInitialization () 方法将被调用】
- SmartInitializingSingleton.afterSingletonsInstantiated
- SmartLifecycle.start
- 运行 Bean
- SmartLifecycle.stop(Runnable callback)
- DisposableBean.destroy() 【销毁】
拦截器HandlerInterceptor、ResponseBodyAdvice和@ExceptionHandler执行顺序
执行顺序:
1、HandlerInterceptor.preHandle()
2、 业务方法
3、@ExceptionHandler
4、ResponseBodyAdvice.supports()
5、ResponseBodyAdvice.beforeBodyWrite()
6、HandlerInterceptor.postHandle()
7、HandlerInterceptor.afterCompletion()