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>

  • 相关阅读:
    hive 数据hadoop数据etl交换
    团队冲刺(三)
    团队冲刺(二)
    CVPR2019论文热词云的实现
    团队冲刺(一)
    团队开发之电梯演讲----团队项目介绍--“益青春APP”
    android的finish()方法
    java web项目通过外网ip访问
    MySQL出现错误1205-Lock wait timeout exceeded; try restarting transaction
    团队开发(自己的理解)
  • 原文地址:https://www.cnblogs.com/mforestlaw/p/3319874.html
Copyright © 2011-2022 走看看