gpt4 book ai didi

spring - ContextRefreshedEvent、ContextStartedEvent、ContextStoppedEvent 和 ContextClosedEvent 有什么区别

转载 作者:行者123 更新时间:2023-12-04 08:40:40 25 4
gpt4 key购买 nike

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

  • 上下文刷新事件
  • 上下文启动事件
  • 上下文停止事件
  • 上下文关闭事件

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

  • 我有以下情况:
  • 如果想尽快初始化日志子系统,应该在 ContextRefreshedEvent 中完成吗?或 ContextStartedEvent ?
  • 如果在 ContextClosedEvent 中完成,我也想尽可能晚地销毁它。或 ContextStoppedEvent ?
  • 最佳答案

    这些内置事件的文档可以在 here 中找到。 , 具体来说:

    ContextRefreshedEvent


    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


    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


    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


    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


    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 的应用程序上下文不同,并且有单独的事件。
    设置和拆除日志系统可能很复杂,并且取决于您使用的日志系统。简而言之,您可能想尝试 ContextRefreshedEventContextClosedEvent .其他两个仅在您调用 start() 时才发送。或 stop()在应用程序上下文中,因此您不想使用它们。
    如果您使用的是 Spring Boot,您可能需要查看 Spring Boot 的
    自己的日志系统抽象( org.springframework.boot.logging.LoggingSystem ),它定义了 beforeInitialize , initalize , 和 cleanUp方法,还有一个 shutdownHandler JVM 存在时调用。
    org.springframework.boot.context.logging.LoggingApplicationListener以供引用。 Spring Boot 自带 additional application events .日志系统的初始化似乎是在 ApplicationEnvironmentPreparedEvent 上完成的。 .清理在 ContextClosedEvent 上完成和 ApplicationFailedEvent .

    关于spring - ContextRefreshedEvent、ContextStartedEvent、ContextStoppedEvent 和 ContextClosedEvent 有什么区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47134189/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com