1. Overview
1.概述
In this tutorial, we’re going to learn different ways to use shutdown callbacks with Spring.
在本教程中,我们将学习不同的方法使用Spring的关闭回调。
The main advantage of using a shutdown callback is that it gives us control over a graceful application exit.
使用关机回调的主要优点是,它使我们能够控制一个优雅的应用程序退出。
2. Shutdown Callback Approaches
2.关机回调的方法
Spring supports both the component-level and the context-level shutdown callbacks. We can create these callbacks using:
Spring同时支持组件级和上下文级的关机回调。我们可以用以下方法创建这些回调。
- @PreDestroy
- DisposableBean interface
- Bean-destroy method
- Global ServletContextListener
Let’s see all of these approaches with examples.
让我们通过实例来看看所有这些方法。
2.1. Using @PreDestroy
2.1.使用@PreDestroy
Let’s create a bean that uses @PreDestroy:
让我们创建一个使用@PreDestroy的bean。
@Component
public class Bean1 {
@PreDestroy
public void destroy() {
System.out.println(
"Callback triggered - @PreDestroy.");
}
}
During the bean initialization, Spring will register all the bean methods that are annotated with @PreDestroy and invokes them when the application shuts down.
在Bean初始化期间,Spring将注册所有被注解为@PreDestroy的Bean方法,并在应用程序关闭时调用它们。
2.2. Using the DisposableBean Interface
2.2.使用DisposableBean接口
Our second bean will implement the DisposableBean interface to register the shutdown callback:
我们的第二个Bean将实现DisposableBean接口来注册关闭回调。
@Component
public class Bean2 implements DisposableBean {
@Override
public void destroy() throws Exception {
System.out.println(
"Callback triggered - DisposableBean.");
}
}
2.3. Declaring a Bean Destroy Method
2.3.声明一个Bean Destroy方法
For this approach, firstly we’ll create a class with a custom destroy method:
对于这种方法,首先我们要创建一个具有自定义销毁方法的类。
public class Bean3 {
public void destroy() {
System.out.println(
"Callback triggered - bean destroy method.");
}
}
Then, we create the configuration class that initializes the bean and marks its destroy() method as our shutdown callback:
然后,我们创建初始化Bean的配置类,并将其destroy()方法作为我们的关闭回调。
@Configuration
public class ShutdownHookConfiguration {
@Bean(destroyMethod = "destroy")
public Bean3 initializeBean3() {
return new Bean3();
}
}
The XML way of registering the destroy method is:
注册destroy方法的XML方式是。
<bean class="com.baeldung.shutdownhooks.config.Bean3"
destroy-method="destroy">
2.4. Using a Global ServletContextListener
2.4.使用全局的ServletContextListener
Unlike the other three approaches, which register the callback at bean level, the ServletContextListener registers the callback at context level.
与其他三种在Bean级别注册回调的方法不同,ServletContextListener在上下文级别注册了回调。
For this let’s create a custom context listener:
为此,让我们创建一个自定义的上下文监听器。
public class ExampleServletContextListener
implements ServletContextListener {
@Override
public void contextDestroyed(ServletContextEvent event) {
System.out.println(
"Callback triggered - ContextListener.");
}
@Override
public void contextInitialized(ServletContextEvent event) {
// Triggers when context initializes
}
}
We need to register it to the ServletListenerRegistrationBean in the configuration class:
我们需要将其注册到配置类中的ServletListenerRegistrationBean。
@Bean
ServletListenerRegistrationBean<ServletContextListener> servletListener() {
ServletListenerRegistrationBean<ServletContextListener> srb
= new ServletListenerRegistrationBean<>();
srb.setListener(new ExampleServletContextListener());
return srb;
}
3. Conclusion
3.结论
We’ve learned about the different ways Spring provides to register shutdown callbacks, both at the bean level and at the context level.
我们已经了解了Spring提供的注册关闭回调的不同方式,包括在Bean级别和上下文级别。
These can be used for shutting down the application gracefully and effectively freeing up the used resources.
这些可以用来优雅地关闭应用程序,并有效地释放使用的资源。
As always all the examples mentioned in this article can be found over on Github.
像往常一样,本文中提到的所有例子都可以在Github上找到over。