zoukankan      html  css  js  c++  java
  • Spring 中,对象销毁前执行某些处理的方法

    通过 @PreDestroy 和 bean 中配置 destroy-method 实现该功能

    java 代码中:

       1: public class TestClass {
       2:     private ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1);
       3:     public TestClass() {
       4:         scheduledExecutorService.scheduleAtFixedRate(task1, 1, 5, TimeUnit.MINUTES);
       5:     }
       6:  
       7:     Runnable task1 = new Runnable() {
       8:         public void run() {
       9:             System.out.println("task1 excuted.");
      10:         }
      11:     }
      12:  
      13:     @PreDestroy
      14:     public void DestroyMethod() {
      15:         scheduledExecutorService.shutdownNow();
      16:     }
      17: }

    bean 配置文件中:

       1: <beans xmlns="http://www.springframework.org/schema/beans"
       2:     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
       3:     xmlns:tx="http://www.springframework.org/schema/tx" xmlns:task="http://www.springframework.org/schema/task"
       4:     xsi:schemaLocation="
       5:      http://www.springframework.org/schema/beans 
       6:      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       7:      http://www.springframework.org/schema/tx 
       8:      http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
       9:      http://www.springframework.org/schema/aop 
      10:      http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
      11:      http://www.springframework.org/schema/task 
      12:      http://www.springframework.org/schema/task/spring-task-3.0.xsd">
      13:     
      14:     <bean id="testClass" class="com.xxx.xxx.TestClass" destroy-method="DestroyMethod">
      15:     </bean>
      16: </beans>

  • 相关阅读:
    P1579哥德巴赫猜想
    JAVA快速入门方法
    PHP快速入门方法
    Java 8 lambda表达式
    JVM内存配置参数
    Synchronized 关键字
    数据库事务的理解
    hello world 执行原理
    面试知识点总结之JVM调优
    面试知识点总结之RabbitMQ/Kafka使用场景
  • 原文地址:https://www.cnblogs.com/mforestlaw/p/3319874.html
Copyright © 2011-2022 走看看