Java教程

1.6.1.6. Shutting Down the Spring IoC Container Gracefully in Non-Web Applications 在非web应用程序关闭Spring

本文主要是介绍1.6.1.6. Shutting Down the Spring IoC Container Gracefully in Non-Web Applications 在非web应用程序关闭Spring,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

 Spring Framework Documentation (5.3.10)

Core

IoC Container, Events, Resources, i18n, Validation, Data Binding, Type Conversion, SpEL, AOP.

   Core Technologies

1. The IoC Container

1.1. Introduction to the Spring IoC Container and Beans(Spring IoC容器和bean简介)

1.2. Container Overview (容器概览)

1.3. Bean Overview (Bean概览)

1.4. Dependencies(依赖)

1.5. Bean Scopes(Bean作用域)

1.6. Customizing the Nature of a Bean (自定义bean的性质)

1.6.1. Lifecycle Callbacks 生命周期回调

1.6.1.1. Initialization Callbacks 初始化回调

1.6.1.2. Destruction Callbacks 销毁回调

1.6.1.3. Default Initialization and Destroy Methods默认初始化和销毁方法

1.6.1.4. Combining Lifecycle Mechanisms (合并生命周期机制)

1.6.1.5. Startup and Shutdown Callbacks 启动和停止回调

1.6.1.6. Shutting Down the Spring IoC Container Gracefully in Non-Web Applications 在非web应用程序优雅地关闭Spr

1.6.2. ApplicationContextAware and BeanNameAware

1.6.3. Other Aware Interfaces 其它Aware接口

1.7. Bean Definition Inheritance(Bean定义继承)

1.8. Container Extension Points (容器扩展点)


更多章节内容,请点击查看:  Core Technologies


1.6.1.6. Shutting Down the Spring IoC Container Gracefully in Non-Web Applications 在非web应用程序优雅地关闭Spring IoC容器

This section applies only to non-web applications. Spring’s web-based ApplicationContext implementations already have code in place to gracefully shut down the Spring IoC container when the relevant web application is shut down.

本节仅适用于非web应用程序。Spring基于web的ApplicationContext实现已经有代码,可以在相关web应用程序关闭时优雅地关闭Spring IoC容器。

If you use Spring’s IoC container in a non-web application environment (for example, in a rich client desktop environment), register a shutdown hook with the JVM. Doing so ensures a graceful shutdown and calls the relevant destroy methods on your singleton beans so that all resources are released. You must still configure and implement these destroy callbacks correctly.

To register a shutdown hook, call the registerShutdownHook()  method that is declared on the ConfigurableApplicationContext interface, as the following example shows:

如果在非web应用程序环境中(例如,在富客户端桌面环境中)使用Spring的IoC容器,请向JVM注册一个关闭钩子(shutdown hook)。这样做可以确保正常shutdown,并在单例bean上调用相关的destroy方法,从而释放所有资源。您仍然必须正确配置和实现这些销毁回调。

要注册关闭钩子(shutdown hook),请调用ConfigurableApplicationContext 接口上声明的registerShutdownHook()  方法,如下例所示:

Java

import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public final class Boot {

    public static void main(final String[] args) throws Exception {
        ConfigurableApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");

        // add a shutdown hook for the above context...
        ctx.registerShutdownHook() ;

        // app runs here...

        // main method exits, hook is called prior to the app shutting down...
    }
}

Kotlin

import org.springframework.context.support.ClassPathXmlApplicationContext

fun main()  {
    val ctx = ClassPathXmlApplicationContext("beans.xml")

    // add a shutdown hook for the above context...
    ctx.registerShutdownHook() 

    // app runs here...

    // main method exits, hook is called prior to the app shutting down...
}

这篇关于1.6.1.6. Shutting Down the Spring IoC Container Gracefully in Non-Web Applications 在非web应用程序关闭Spring的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!