spring bean 加载 顺序 接口 相关

2017-11-07  本文已影响0人  Gxgeek

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

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

spring 的接口

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 的接口

spring 和springBoot 启动的事件

spirng 内定事件

spirng Boot 内定事件

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

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

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

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

boot内定的事件注册 自定义bean (写注解 或接口 变成bean 就可以观察到)

总结

springBoot 启动先后调用

容器级别

ApplicationEnvironmentPreparedEvent 事件

ApplicationPreparedEvent 事件

bean 级别

容器结束

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

ApplicationRunner,CommandLineRunner 接口实现 执行

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

上一篇 下一篇

猜你喜欢

热点阅读