【问题标题】:How to get callback when Spring web aplication is finished startingSpring Web应用程序完成启动时如何获取回调
【发布时间】:2019-03-16 01:01:13
【问题描述】:

在整个 Spring 应用程序完成加载后,我需要执行一些操作。该操作必须在所有 bean 都被初始化并且所有控制器都可以开始服务请求之后发生。

我尝试实现一个 ServletContextListener 并将其添加为 AbstractAnnotationConfigDispatcherServletInitializer 中的侦听器,但这会在应用程序启动时发生,而不是在启动后发生。我也没有使用 SpringBoot 来执行此操作。

谢谢,

【问题讨论】:

    标签: spring spring-mvc servletcontextlistener servlet-listeners


    【解决方案1】:

    因为你使用的是spring mvc。

    所以 RequestHandledEvent 将满足您的要求。

    RequestHandledEvent
    
    This is a web-specific event telling all beans that an HTTP request has been serviced.
    

    关于春季活动的更多细节

    https://www.tutorialspoint.com/spring/event_handling_in_spring.htm

    【讨论】:

    • 不会这只会在请求被处理后才会发生,因此如果没有调用 Rest Api,它根本不会被触发。
    • 您是否尝试打印出日志以及 ContextStartedEvent、ContextRefreshedEvent 和 RequestHandledEvent 的结果如何
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-11
    • 2011-04-27
    • 1970-01-01
    • 1970-01-01
    • 2016-01-11
    相关资源
    最近更新 更多