Spring加载全过程
BeanPostProcessor
接口定义
1 | /** |
执行顺序
- org.springframework.beans.factory.config.BeanPostProcessor#postProcessBeforeInitialization
- org.springframework.beans.factory.InitializingBean#afterPropertiesSet
- org.springframework.beans.factory.config.BeanPostProcessor#postProcessAfterInitialization
ApplicationListener
接口定义
1 | public interface ApplicationListener<E extends ApplicationEvent> extends EventListener { |
ApplicationEvent
ContextRefreshedEvent:ApplicationContext容器初始化或刷新时触发该事件。此处的初始化是指:所有的Bean被成功装载,后处理Bean被检测并激活,所有Singleton Bean 被预实例化,ApplicationContext容器已就绪可用
ContextStartedEvent:当使用ConfigurableApplicationContext(ApplicationContext的子接口)接口的start()方法启动ApplicationContext容器时触发该事件。容器管理声明周期的Bean实例将获得一个指定的启动信号,这在经常需要停止后重新启动的场合比较常见
ContextClosedEvent:当使用ConfigurableApplicationContext接口的close()方法关闭ApplicationContext时触发该事件
ContextStoppedEvent:当使用ConfigurableApplicationContext接口的stop()方法使ApplicationContext容器停止时触发该事件。此处的停止,意味着容器管理生命周期的Bean实例将获得一个指定的停止信号,被停止的Spring容器可再次调用start()方法重新启动
RequestHandledEvent:Web相关事件,只能应用于使用DispatcherServlet的Web应用。在使用Spring作为前端的MVC控制器时,当Spring处理用户请求结束后,系统会自动触发该事件。
Event也可以自定义实现
ApplicationContextAware
为何实现ApplicationContextAware可以获取到ApplicationContext?
其实是org.springframework.context.support.ApplicationContextAwareProcessor实现BeanPostProcessor进行处理。
源码如下:
1 | class ApplicationContextAwareProcessor implements BeanPostProcessor { |