Spring init-method和destroy-method 的使用
Spring 為了滿足開發者在執行某方法之前或者在結束某個任務之前需要操作的一些業務,則提供了init-method和destroy-method 這兩個屬性,這兩個屬性需要加載在bean節點中。
下面上代碼部分,為了完整性,我把 IOC和 依賴注入也加入
一、首先我們創建一個接口StudentService.java
1 package cn.demo.service; 2 3 /** 4 * 接口 5 * @author xkjava 6 * @date 2015-07-12 7 */ 8 public interface StudentService { 9 10 public void helloSpring(String str); 11 12 }
二、StudentServiceImpl.java 實現類
1 package cn.demo.service.impl; 2 3 import java.io.Serializable; 4 5 import cn.demo.service.StudentService; 6 7 public class StudentServiceImpl implements StudentService,Serializable{ 8 9 10 /** 11 * 12 */ 13 private static final long serialVersionUID = 6130145558179499205L; 14 15 16 /** 17 * demo測試 18 */ 19 @Override 20 public void helloSpring(String str) { 21 // TODO Auto-generated method stub 22 System.err.println("這里正常執行 this is "+str); 23 } 24 25 /** 26 * 執行helloSpring 之前執行 27 */ 28 public void inits(){ 29 System.err.println("這里在 執行helloSpring之前執行! "); 30 } 31 32 33 /** 34 * 摧毀 對象前調用 35 */ 36 public void shutdown(){ 37 System.err.println("銷毀 studentService 對象實例 前 調用 shutdown() 方法"); 38 } 39 40 }
三、Spring XML文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" 4 xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" 5 xsi:schemaLocation=" 6 http://www.springframework.org/schema/context 7 http://www.springframework.org/schema/context/spring-context-3.0.xsd 8 http://www.springframework.org/schema/beans 9 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 10 http://www.springframework.org/schema/tx 11 http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 12 http://www.springframework.org/schema/aop 13 http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> 14 15 <!-- 注意 init-method 和 destroy-method 所加載的方法名字 和 StudentServiceImpl.java中對比 --> 16 <bean id="stu" class="cn.demo.service.impl.StudentServiceImpl" init-method="inits" destroy-method="shutdown"></bean> 17 18 </beans>
四、TestDemo.java測試類
1 package cn.demo.test; 2 3 import java.io.Serializable; 4 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.context.ApplicationContext; 7 import org.springframework.context.support.ClassPathXmlApplicationContext; 8 9 import cn.demo.service.StudentService; 10 11 /** 12 * 測試 13 * @author xkjava 14 * 15 */ 16 public class TestDemo implements Serializable { 17 18 19 /** 20 * 21 */ 22 private static final long serialVersionUID = 6343872716391435079L; 23 24 25 /** 26 * main入口 27 */ 28 public static void main(String[] args){ 29 30 31 ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"}); 32 33 StudentService studentService = context.getBean("stu", StudentService.class); 34 35 studentService.helloSpring("Hello! Spring!"); 36 37 //摧毀studentService實例對象 38 ((ClassPathXmlApplicationContext) context).close(); 39 40 } 41 }
注意:
在執行完畢后需要 將 ((ClassPathXmlApplicationContext) context).close(); Close 關閉 才可執行 destroy-method