1. Overview

In this tutorial, we'll look at how we can close the Spring application context. It is important to close the context properly because different lifecycle methods must have the chance to run. Consequently, the application can release the resources and do some clean-up.

2. Sample Application

We'll first look at our sample application.

We have MonitorService which has the init and destroy methods:

@Component
public class MonitorService {

    @PostConstruct
    public void init() {
        System.out.println("Initialized...");
    }

    @PreDestroy
    public void destroy() {
        System.out.println("Destroying...");
    }
}

In a proper shutdown, the destroy method must have the chance to run.

Then we have Application to serve as the configuration class:

@Configuration
@ComponentScan
public class Application {
}

3. Close with Shutdown Hook

Firstly, we'll use the registerShutdownHook method to close the Spring application context. This method registers a shutdown hook with the JVM runtime. When the JVM is shutting down, it also closes the Spring container:

private static void closeAutomatically() {
    ConfigurableApplicationContext applicationContext = new AnnotationConfigApplicationContext(
      Application.class);
    applicationContext.registerShutdownHook();
    
    // Additional logic...
}

Here, we're creating an instance of AnnotationConfigApplicationContext and calling registerShutdownHook(). Note that this method is available on ConfigurableApplicationContext which holds lifecycle related methods.

On the shutdown, Spring invokes its lifecycle callbacks. As a result, our MonitorService runs its destroy method:

Initialized...
[SpringContextShutdownHook] DEBUG org.springframework.context.annotation.AnnotationConfigApplicationContext - Closing...
Destroying...

4. Close Manually

Next, we'll look at how we can manually close the Spring application context. For this purpose, we'll use the close method defined on the ConfigurableApplicationContext interface:

private static void closeManually() {
    ConfigurableApplicationContext applicationContext = new AnnotationConfigApplicationContext(Application.class);
    
    // Additional logic...
    
    applicationContext.close();
}

In this example, after doing some work, we're closing the context. Again the MonitorService bean gets the chance to run its destroy method:

Initialized...
[SpringContextShutdownHook] DEBUG org.springframework.context.annotation.AnnotationConfigApplicationContext - Closing...
Destroying...

5. Close as Closeable

Since ConfigurableApplicationContext extends Closeable, we'll now use the try-with-resources statement:

private static void closeAsCloseable() {
    try (ConfigurableApplicationContext applicationContext = new AnnotationConfigApplicationContext(
      Application.class)) {
    
        // Additional logic...
    }
}

When the try block finishes, the close method - from Closeable - is called. This has the same effect as closing manually.

6. Summary

In this quick tutorial, we've investigated the different ways to close the Spring application context.

Finally, check out the source code for all examples in this tutorial over on Github.