`

3.5.1.2.2. Shutting down the Spring IoC container gracefully in non-web applicat

阅读更多
引用
http://static.springsource.org/spring/docs/2.0.x/reference/beans.html

Note
This next section does not apply to web applications (in case the title of this section did not make that abundantly clear). Spring's web-based ApplicationContext implementations already have code in place to handle shutting down the Spring IoC container gracefully when the relevant web application is being shutdown.
If you are using Spring's IoC container in a non-web application environment, for example in a rich client desktop environment, and you want the container to shutdown gracefully and call the relevant destroy callbacks on your singleton beans, you will need to register a shutdown hook with the JVM. This is quite easy to do (see below), and will ensure that your Spring IoC container shuts down gracefully and that all resources held by your singletons are released (of course it is still up to you to both configure the destroy callbacks for your singletons and implement such destroy callbacks correctly).
So to register a shutdown hook that enables the graceful shutdown of the relevant Spring IoC container, you simply need to call the registerShutdownHook() method that is declared on the AbstractApplicationContext class. To wit...
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public final class Boot {

    public static void main(final String[] args) throws Exception {
        AbstractApplicationContext ctx
            = new ClassPathXmlApplicationContext(new String []{"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...
    }
}
分享到:
评论

相关推荐

    spring-framework-reference-4.1.2

    4.1. Introduction to the Spring IoC container and beans .............................................. 22 4.2. Container overview .........................................................................

    spring-framework-reference4.1.4

    4.1. Introduction to the Spring IoC container and beans .............................................. 22 4.2. Container overview .........................................................................

    STLink.zip

    "STLink.zip"这个压缩包提供的文件显然是为了帮助开发者解决STM32调试过程中遇到的问题,特别是针对"cannot access target shutting down debug session"这一错误。 "cannot access target shutting down debug ...

    aha154x.zip_Download aha154x.sys_The Power_adaptec aha-154x_mini

    This driver is also responsible for detecting non-Plug and Play 1540 SCSI controllers—the Plug and Play controllers are detected by the operating system—and for shutting down the controller during ...

    UPnP_Programming_Guide(中英文两个版本)

    2.4 Shutting Down ............................................................................................................... 18 3 Writing a UPnP Control Point........................................

    Tricks of the Windows video Game Programming---part1

    Tricks of the Windows video Game Programming <br>PART I Windows Programming Foundations 7 1 Journey into the Abyss 9 A Little History.............................................................

    The Definitive Guide to NetBeans Platform

    Reduction in Development Time. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 User Interface Consistency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Updating. . ...

    KafkaOffsetMonitor监控工具2017年1月发布的版本

    No longer shutting down the application on error. Instead, closing and destroying the client and re-creating it. Sleeping on error before re-creating client and continuing to process Deal with thread-...

    Spring.Boot.in.Action.2015.12.pdf

    Shutting down the application 139. Fetching application information 140 7.2 Connecting to the Actuator remote shell 141 Viewing the autoconfig report 142. Listing application beans 143 Watching ...

    uTorrent v1.8.3.15358 Beta/v1.9 build 14981 Beta 多国语言版

    - Fix: invalid encoding of non-ascii urls in web seeds - Fix: uTP fix when receiving acks for old packets - Fix: uTP variable packet size bug - Change: link to EULA in installer - Change: added knob ...

    文字转换语音软件(可存储为音频文件)

    在IT领域,文字转换语音(Text-to-Speech, TTS)技术是一种将文本数据转化为可听见的音频输出的重要工具。这种技术广泛应用于各种场景,包括有声读物、辅助技术(帮助视障人士)、自动语音播报系统、智能助手以及...

    Revelation of the birth and extinction dynamics of solitons in SWNT-mode-locked fiber lasers

    The dispersive Fourier transform (DFT) technique opens a fascinating pathway to explore ultrafast non-repetitive events and has been employed to study the build-up process of mode-locked lasers....

    hpSATA-Rev-30-Presentation

    - Hot-plug capability allows for easy installation and removal of devices without shutting down the system. **Cabling and Connectors:** - **Advantages:** - Thinner and more flexible cables improve ...

    centos7_XFS_报Corruption of in-memory data detected错误.docx

    在Linux系统中,CentOS 7使用XFS文件系统可能会遇到一种特定的错误:“Corruption of in-memory data detected”。这个错误通常表示系统检测到了内存中的数据损坏,这可能是因为不正常关机、硬件故障或者软件问题...

    ABAQUS模拟出现问题.docx

    此外,还有一些直接导致计算中止的错误,例如 xxxx is not a valid in ABAQUS/Standard、missing property、Detected lock file 等问题。这些问题需要仔细分析,以确定问题的原因和解决方法。 ABAQUS 模拟出现问题...

    Administering VMware Site Recovery Manager 4.0.pdf

    Table of Contents Chapter 1: ...Shutting Down the VSA ....................................................................................... 90 Conclusion ......................................

    AFF A800 - Replacing the RTC battery.pdf

    1. **Shutting down the impaired controller**:在更换RTC电池之前,首先需要安全关闭受影响的控制器。这可能涉及到将系统的工作负载转移到另一个健康的控制器,以避免数据丢失或系统中断。 2. **Shutting down ...

    BobBuilder_app

    In the event of page getting full and reaching the PageItemCount, MGIndex will sort the keys in the page's dictionary and split the data in two pages ( similar to a b+tree split) and update the page ...

    Oracle Solaris 11 Booting and Shutting Down Oracle Solaris on SP

    本文档“Oracle Solaris 11 Booting and Shutting Down Oracle Solaris on SPARC Platforms”详细介绍了在 SPARC 平台上启动和关闭 Oracle Solaris 11 的过程和技术。 在 SPARC 平台上启动 Oracle Solaris 11 涉及...

Global site tag (gtag.js) - Google Analytics