Java 未调用Spring Boot ContextStoppedEvent
我的应用程序中有一些Quartz工作,如果我的应用程序上下文关闭,我需要立即终止这些工作。为此,我尝试实现ContextStoppedEvent。多次尝试后,我无法触发此事件。我需要一些帮助来召集这次活动。 下面是我的代码Java 未调用Spring Boot ContextStoppedEvent,java,spring,spring-boot,Java,Spring,Spring Boot,我的应用程序中有一些Quartz工作,如果我的应用程序上下文关闭,我需要立即终止这些工作。为此,我尝试实现ContextStoppedEvent。多次尝试后,我无法触发此事件。我需要一些帮助来召集这次活动。 下面是我的代码 @Component public class StopContextListener implements ApplicationListener<ContextStoppedEvent> { private static final Logger L
@Component
public class StopContextListener implements ApplicationListener<ContextStoppedEvent> {
private static final Logger LOG = LoggerFactory.getLogger(StopContextListener.class.getName());
@Override
public void onApplicationEvent(ContextStoppedEvent arg0) {
LOG.info("Data base closed successfully");
}
}
@组件
公共类StopContextListener实现ApplicationListener{
私有静态最终记录器LOG=LoggerFactory.getLogger(StopContextListener.class.getName());
@凌驾
ApplicationEvent上的公共无效(ContextStoppedEvent arg0){
LOG.info(“数据库成功关闭”);
}
}
更新:
@Component
public class StopContextListener implements ApplicationListener<ContextClosedEvent> {
private static final Logger LOG = LoggerFactory.getLogger(StopContextListener.class.getName());
@Override
public void onApplicationEvent(ContextClosedEvent arg0) {
LOG.info("Data base closed successfully");
}
}
@组件
公共类StopContextListener实现ApplicationListener{
私有静态最终记录器LOG=LoggerFactory.getLogger(StopContextListener.class.getName());
@凌驾
Application Event(ContextClosedEvent arg0)上的公共无效{
LOG.info(“数据库成功关闭”);
}
}
鉴于您要求“如果我的应用程序上下文关闭,立即终止”,我建议您收听ContextClosedEvent
,而不是ContextStoppedEvent
。ContextStoppedEvent
未作为要关闭的上下文的一部分发布。我也用ContextStoppedEvent尝试过。不幸的是没有成功。我是否缺少任何启用ContextStoppedEvent的注释???您是否将事件弄混了?我建议您收听ContextClosedEvent
。谢谢您的回复。根据你的评论,我就是这样做的。我已经更新了我的帖子。请立即提出建议。什么包是StopContextListener
,应用程序的主类在哪个包中?组件扫描可能找不到它。org.springframework.context.event.StopContextListener,我在应用程序中使用的是同一个包。