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>

posted @ 2013-09-13 18:00  琼'  阅读(1491)  评论(0编辑  收藏  举报