美文网首页
spring bean 加载 顺序 接口 相关

spring bean 加载 顺序 接口 相关

作者: Gxgeek | 来源:发表于2017-11-07 20:01 被阅读0次

以下所有实现均在springBoot 的环境下

和bean启动相关的接口有 以下接口按照bean 实例化过程中的先后顺序书写

spring 的接口

  • bean本身的构造器 无参数
  • BeanNameAware ->得到bean 的名字 最先调用
  • BeanFactoryAware ->得到beanFactory
  • ApplicationContextAware --> 获得 ApplicationContext
  • InitializingBean ------> bean 初始化完成 调用
  • BeanFactoryPostProcessor -------> 会在 InitializingBean 初始化后执行, 传入 configurableListableBeanFactory,注意 此 接口 会影响 @PostConstruct 注解 调用
  • BeanPostProcessor
2017-11-07 19:50:38.458  INFO 38542 --- [  restartedMain] c.gxgeek.springfeaturesstudy.InitBean3   : bean3 初始化
2017-11-07 19:50:38.463  INFO 38542 --- [  restartedMain] c.gxgeek.springfeaturesstudy.InitBean3   : bean3 setBeanName
2017-11-07 19:50:38.464  INFO 38542 --- [  restartedMain] c.gxgeek.springfeaturesstudy.InitBean3   : bean3 setBeanFactory
2017-11-07 19:50:38.464  INFO 38542 --- [  restartedMain] c.gxgeek.springfeaturesstudy.InitBean3   : bean3 setApplicationContext
2017-11-07 19:50:38.464  INFO 38542 --- [  restartedMain] com.gxgeek.springfeaturesstudy.InitBean  : BeanPostProcessor 接口的: postProcessBeforeInitialization com.gxgeek.springfeaturesstudy.InitBean3@149fb594-->com.gxgeek.springfeaturesstudy.InitBean3
2017-11-07 19:50:38.464  INFO 38542 --- [  restartedMain] c.gxgeek.springfeaturesstudy.InitBean3   : bean3 postConstruct
2017-11-07 19:50:38.464  INFO 38542 --- [  restartedMain] c.gxgeek.springfeaturesstudy.InitBean3   : bean3 afterPropertiesSet
2017-11-07 19:50:38.464  INFO 38542 --- [  restartedMain] com.gxgeek.springfeaturesstudy.InitBean  : BeanPostProcessor 接口的: postProcessAfterInitializationcom.gxgeek.springfeaturesstudy.InitBean3@149fb594-->com.gxgeek.springfeaturesstudy.InitBean3

SpringBoot 的接口

  • CommandLineRunner ,ApplicationRunner 容器初始化完成之后开始调用run方法
    在 ==ContextRefreshedEvent== 事件 之后 在 ==ApplicationReadyEvent== 事件之后

spring 和springBoot 启动的事件

spirng 内定事件

  • ContextStartedEvent 事件 // ==暂无发现任何默认注册监听==

  • ContextRefreshedEvent 事件 --->初始化完成上下文调用(现在只发现调用一次 )

  • ContextStoppedEvent 事件// ==暂无发现任何默认注册监听==

  • ContextClosedEvent 事件 ---> 调用ConfigurableApplicationContext的close方法的时候 应用调用。
    只调用一次

  • RequestHandledEvent 事件 ---> 请求完成调用(注意可能会调用多次 )

spirng Boot 内定事件

spring-boot扩展了spring的ApplicationContextEvent,提供了四种事件

按照先后顺序 开始执行(这四个事件需要我们手动去(addListeners()或写配置文件 )

多个 逗号分隔 context.listener.classes=com.gxgeek.springfeaturesstudy.SpringFeaturesStudyApplication.ApplicationListenerEnvironmentPrepared

去监听 不能通过 写注解或者 去将类变成 bean 实现 其实很好理解 变成bean 之后必须 和context 上下文相关联 但是已经在上下文中 是不可能接收到 这些事件的)

  • ApplicationStartedEvent :spring boot启动开始时执行的事件(==这个事件没有观察到== )
  • ApplicationEnvironmentPreparedEvent:spring boot 对应Enviroment已经准备完毕,但此时上下文context还没有创建。
  • ApplicationPreparedEvent:spring boot上下文context创建完成,但此时spring中的bean是没有完全加载完成的。(可以理解为 最先 注入的bean 可以拿到上下文 注意此时的上下文中其实是有少数的bean 的 三四个
  • ApplicationFailedEvent:spring boot启动异常时执行事件
boot内定的事件注册 自定义bean (写注解 或接口 变成bean 就可以观察到)
  • ApplicationReadyEvent 应用可以请求,意味着端口开始监听 执行

总结

springBoot 启动先后调用

容器级别

ApplicationEnvironmentPreparedEvent 事件

ApplicationPreparedEvent 事件

bean 级别

  • bean本身的构造器 无参数
  • BeanNameAware ->得到bean 的名字 最先调用
  • BeanFactoryAware ->得到beanFactory
  • ApplicationContextAware --> 获得 ApplicationContext
  • @PostConstruct 注解执行
  • InitializingBean ------> bean 初始化完成 调用
  • BeanFactoryPostProcessor -------> 会在 InitializingBean 初始化后执行, 传入 configurableListableBeanFactory,注意 此 接口 会影响 @PostConstruct 注解 调用
  • BeanPostProcessor

容器结束

ContextRefreshedEvent 事件 容器全部实例化完成

ApplicationRunner,CommandLineRunner 接口实现 执行

ApplicationReadyEvent 事件 --> 可以接受请求发送该事件

相关文章

网友评论

      本文标题:spring bean 加载 顺序 接口 相关

      本文链接:https://www.haomeiwen.com/subject/dnfsmxtx.html