如何在Spring中正确关闭ApplicationContext? [英] How correctly close the ApplicationContext in Spring?

查看:306
本文介绍了如何在Spring中正确关闭ApplicationContext?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究Spring Core认证,并且在提供的学习资料中发现了关于这个问题的一些疑问:

I am studying for the Spring Core certification and I have some dount about on this question finded into the provided study material:

关闭应用程序上下文的首选方法是什么?

What is the preferred way to close an application context?

我知道如果我有这样的东西:

I know that if I have something like this:

ConfigurableApplicationContext context = …
// Destroy the application
context.close();

通过在 context 对象上使用 close()方法来

关闭ApplicationContext并销毁该应用程序.

by the use of the close() method on the context objet the ApplicationContext is closed and the application is destroyed.

但是我认为这不是我必须做的最好方法.

But I think that this is not the best way that I have to do it.

阅读官方文档后,我发现我也可以执行以下操作:

Reading the official documentation I find that I can also do something like this:

context.registerShutdownHook();

向JVM注册一个Shutdown Hook ,因此在JVM退出之前,正是JVM会触发Spring的关闭阶段.因此,在JVM退出时,将执行Spring的关闭阶段.

that register a Shutdown Hook with the JVM so it is the JVM that will trigger Spring's close phase before JVM exits. So on JVM exit, Spring's close phase will execute.

在文档上,我可以读到:通常无法调用context.close(),因为许多应用程序(Web应用程序)会无限期运行,但是最后一个断言到底意味着什么呢?为什么Web应用程序无限期运行?

On the documentation I can read that: usually not possible to call context.close() because many applications (web applications) run indefinitely But what exactly means this last assertion? why web application run indefinitely?

所以我的问题是:

  • 我可以使用第二种方式关闭非Web应用程序的应用程序上下文吗?
  • 是否更愿意尊重context.close()?

Tnx

推荐答案

您已经知道ContextLoaderListener是负责初始化和销毁​​ApplicationContext的程序,当您关闭服务器时,该ContextLoaderListener的contextDestroyed方法被调用.

As you are aware that ContextLoaderListener is the one that takes care of initializing and destroying your ApplicationContext, when you shutdown your server, that ContextLoaderListener's contextDestroyed method is invoked.

  public void contextDestroyed(ServletContextEvent event){
    closeWebApplicationContext(event.getServletContext());
    ContextCleanupListener.cleanupAttributes(event.getServletContext());
  }

在该closeWebApplicationContext中,他们实际上是像这样在ApplicationContext上调用close方法

In that closeWebApplicationContext, they actually call the close method on ApplicationContext like this

  if ((this.context instanceof ConfigurableWebApplicationContext)) {
    ((ConfigurableWebApplicationContext)this.context).close();
  }

这直接来自spring-web-4.1.5.jar.从这里可以明显看出,他们使用close破坏了Web应用程序中的ApplicationContext.

This is straight from spring-web-4.1.5.jar. As it is evident from here, they use close to destroy ApplicationContext in web applications.

但是registerShutdownHook用于显式关闭非Web应用程序(如独立的桌面应用程序)中的IoC容器,特别是当您从ClassPathXmlApplicationContext(或)FileSystemXmlApplicationContext(或其他类型)手动创建ApplicationContext时.

But registerShutdownHook is used to explicitly shut down IoC container in non-web applications something like a standalone desktop application, specially when you're creating the ApplicationContext manually from ClassPathXmlApplicationContext (or) FileSystemXmlApplicationContext (or) some other types.

这样做是为了释放Spring应用程序使用的所有资源,并在Spring bean上调用destroy方法(如果有的话).

This is done to release all resources used by your spring application and to call destroy method on your spring beans if any.

这篇关于如何在Spring中正确关闭ApplicationContext?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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