ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext()
ctx.registerShutdownHook();
ctx.addApplicationListener(new ApplicationListener<ContextClosedEvent>() {
@Override
public void onApplicationEvent(ContextClosedEvent event) {
ServerTask task = (ServerTask) ctx.getBean("serverTask");
task.exit();
logger.error("系统关闭成功");
}
});
一开始使用destroy方法想通过这个来监听jvm 关闭事件,但是destroy方法之间是没有优先级的,所以无法处理。
通过这种监听ContextClosedEvent事件来处理一些事务及保存数据