关于spring监听器的问题

spring监听器会对某一些事件的触发进行监听:

例如:ContextClosedEvent(容器关闭时) 
     ContextRefreshedEvent(容器刷新时) 
     ContextStartedEvent(容器启动时候) 
     ContextStoppedEvent(容器停止的时候) 

我想知道的是容器什么时候会刷新?网上复制的答案就别来了,看过了。

阅读 3k
3 个回答

这些东西最好去看文档。

可以spring 的官网看下文档,https://docs.spring.io/spring... 我这里截取下

ContextRefreshedEvent的说明:
Published when the ApplicationContext is initialized or refreshed, for example, using the refresh() method on the ConfigurableApplicationContext interface. "Initialized" here means that all beans are loaded, post-processor beans are detected and activated, singletons are pre-instantiated, and the ApplicationContext object is ready for use. As long as the context has not been closed, a refresh can be triggered multiple times, provided that the chosen ApplicationContext actually supports such "hot" refreshes. For example, XmlWebApplicationContext supports hot refreshes, but GenericApplicationContext does not.

从说明中可以看出,当容器中的bean初始化好的时候,会回调ContextRefreshedEvent事件一次,
你也可以手动调用容器提供的refresh()方法,去触发ContextRefreshedEvent事件。

延迟实例化bean,算刷新吗

这个就需要看下源码了哦,你在这单纯的问,还不如自己看下源码,什么情况下会被调用到!

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题