ContextRefreshedEvent,ContextStartedEvent,ContextStoppedEvent和ContextClosedEvent有什么区别 [英] What is difference between ContextRefreshedEvent, ContextStartedEvent, ContextStoppedEvent and ContextClosedEvent

查看:972
本文介绍了ContextRefreshedEvent,ContextStartedEvent,ContextStoppedEvent和ContextClosedEvent有什么区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Spring 5.x中,以下事件之间有什么区别?

In Spring 5.x, what is the difference between following events?

  1. ContextRefreshedEvent
  2. ContextStartedEvent
  3. ContextStoppedEvent
  4. ContextClosedEvent

哪个事件与servlet上下文事件相关(根据 https ://docs.oracle.com/javaee/6/api/javax/servlet/ServletContextListener.html ):

Which event correlate with the servlet context events (as per https://docs.oracle.com/javaee/6/api/javax/servlet/ServletContextListener.html):

  • ServletContextListener.contextInitialized(ServletContextEvent);和
  • ServletContextListener.contextDestroyed(ServletContextEvent)?
  • ServletContextListener.contextInitialized(ServletContextEvent); and
  • ServletContextListener.contextDestroyed(ServletContextEvent)?

我有以下情况:

  • 要尽快初始化日志子系统,应该在ContextRefreshedEventContextStartedEvent中完成?

我还想尽可能地销毁它,应该在ContextClosedEventContextStoppedEvent中完成吗?

I also want to destruct it as late as possible, should that be done in ContextClosedEvent or ContextStoppedEvent?

推荐答案

这些内置事件的文档可以找到

The documentation for these built-in events can be found here, specifically:

ContextRefreshedEvent

ContextRefreshedEvent

在初始化或刷新ApplicationContext时发布(例如,通过使用ConfigurableApplicationContext接口上的refresh()方法).在这里,已初始化"是指所有Bean都已加载,检测到并激活了后处理器Bean,已预先实例化单例并且可以使用ApplicationContext对象.只要尚未关闭上下文,只要选定的ApplicationContext实际上支持这种热"刷新,就可以多次触发刷新.例如,XmlWebApplicationContext支持热刷新,但GenericApplicationContext不支持.

Published when the ApplicationContext is initialized or refreshed (for example, by using the refresh() method on the ConfigurableApplicationContext interface). Here, "initialized" 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.

ContextStartedEvent

ContextStartedEvent

使用ConfigurableApplicationContext接口上的start()方法启动ApplicationContext时发布.在这里,启动"表示所有Lifecycle bean都收到一个明确的启动信号.通常,此信号用于在显式停止后重新启动Bean,但也可以用于启动尚未配置为自动启动的组件(例如,尚未在初始化时启动的组件).

Published when the ApplicationContext is started by using the start() method on the ConfigurableApplicationContext interface. Here, "started" means that all Lifecycle beans receive an explicit start signal. Typically, this signal is used to restart beans after an explicit stop, but it may also be used to start components that have not been configured for autostart (for example, components that have not already started on initialization).

ContextStoppedEvent

ContextStoppedEvent

使用ConfigurableApplicationContext接口上的stop()方法停止ApplicationContext时发布.在这里,已停止"表示所有Lifecycle bean都收到一个明确的停止信号.停止的上下文可以通过start()调用重新启动.

Published when the ApplicationContext is stopped by using the stop() method on the ConfigurableApplicationContext interface. Here, "stopped" means that all Lifecycle beans receive an explicit stop signal. A stopped context may be restarted through a start() call.

ContextClosedEvent

ContextClosedEvent

使用ConfigurableApplicationContext接口上的close()方法关闭ApplicationContext时发布.在此,封闭"表示所有单例豆都被破坏了.封闭的情境到了生命的尽头.无法刷新或重新启动.

Published when the ApplicationContext is closed by using the close() method on the ConfigurableApplicationContext interface. Here, "closed" means that all singleton beans are destroyed. A closed context reaches its end of life. It cannot be refreshed or restarted.

RequestHandledEvent

RequestHandledEvent

一个特定于Web的事件,告诉所有Bean HTTP请求已得到服务.请求完成后,将发布此事件.此事件仅适用于使用Spring的DispatcherServlet的Web应用程序.

A web-specific event telling all beans that an HTTP request has been serviced. This event is published after the request is complete. This event is only applicable to web applications that use Spring’s DispatcherServlet.

Afaik,这些都不与ServletContext直接相关.这与Spring的应用程序上下文所想的是不同的,并且为此存在单独的事件.

Afaik, none of these correlate directly to the ServletContext. That's a different beast than Spring's application context thought, and there are separate events for that.

设置和拆除日志记录系统可能很复杂,并且取决于您使用的日志记录系统.但简而言之,您可能要尝试使用ContextRefreshedEventContextClosedEvent.其他两个仅在您在应用程序上下文中调用start()stop()时分派,因此您不想使用它们.

Setting up and tearing down a logging system can be complicated and will depend on which logging system you use. But in short, you may want to try with ContextRefreshedEvent and ContextClosedEvent. The other two are only dispatched when you call start() or stop() on the application context, so you wouldn't want to use those.

如果您使用的是Spring Boot,则可能要看一下Spring Boot的 日志系统(org.springframework.boot.logging.LoggingSystem)自己的抽象,它定义了beforeInitializeinitalizecleanUp方法,还定义了shutdownHandler,当JVM存在时会调用它.

If you're using Spring Boot, you may want to look at Spring Boot's own abstraction of logging systems (org.springframework.boot.logging.LoggingSystem), which defines beforeInitialize, initalize, and cleanUp methods, and also a shutdownHandler that is called when the JVM exists.

并参见org.springframework.boot.context.logging.LoggingApplicationListener以供参考. Spring Boot随附其他应用程序事件.日志记录系统的初始化似乎是在ApplicationEnvironmentPreparedEvent上完成的.清除是在ContextClosedEventApplicationFailedEvent上完成的.

And see org.springframework.boot.context.logging.LoggingApplicationListener for reference. Spring Boot comes with additional application events. The initialization of the logging system seems to be done on the ApplicationEnvironmentPreparedEvent. Cleanup is done on ContextClosedEvent and ApplicationFailedEvent.

这篇关于ContextRefreshedEvent,ContextStartedEvent,ContextStoppedEvent和ContextClosedEvent有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆