Spring init-method和destroy-method 的使用

时间:2023-03-08 21:24:27

Spring init-method和destroy-method 的使用

 

  Spring 为了满足开发者在执行某方法之前或者在结束某个任务之前需要操作的一些业务,则提供了init-method和destroy-method  这两个属性,这两个属性需要加载在bean节点中。

  下面上代码部分,为了完整性,我把 IOC和 依赖注入也加入

  一、首先我们创建一个接口StudentService.java

 package cn.demo.service;

 /**
* 接口
* @author xkjava
* @date 2015-07-12
*/
public interface StudentService { public void helloSpring(String str); }

  

  二、StudentServiceImpl.java 实现类 

 package cn.demo.service.impl;

 import java.io.Serializable;

 import cn.demo.service.StudentService;

 public class StudentServiceImpl implements StudentService,Serializable{

     /**
*
*/
private static final long serialVersionUID = 6130145558179499205L; /**
* demo测试
*/
@Override
public void helloSpring(String str) {
// TODO Auto-generated method stub
System.err.println("这里正常执行 this is "+str);
} /**
* 执行helloSpring 之前执行
*/
public void inits(){
System.err.println("这里在 执行helloSpring之前执行! ");
} /**
* 摧毁 对象前调用
*/
public void shutdown(){
System.err.println("销毁 studentService 对象实例 前 调用 shutdown() 方法");
} }

  三、Spring XML文件

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <!-- 注意 init-method 和 destroy-method 所加载的方法名字 和 StudentServiceImpl.java中对比 -->
<bean id="stu" class="cn.demo.service.impl.StudentServiceImpl" init-method="inits" destroy-method="shutdown"></bean> </beans>

  四、TestDemo.java测试类

 package cn.demo.test;

 import java.io.Serializable;

 import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.demo.service.StudentService; /**
* 测试
* @author xkjava
*
*/
public class TestDemo implements Serializable { /**
*
*/
private static final long serialVersionUID = 6343872716391435079L; /**
* main入口
*/
public static void main(String[] args){ ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"}); StudentService studentService = context.getBean("stu", StudentService.class); studentService.helloSpring("Hello! Spring!"); //摧毁studentService实例对象
((ClassPathXmlApplicationContext) context).close(); }
}

 

   注意:

  在执行完毕后需要 将 ((ClassPathXmlApplicationContext) context).close();   Close 关闭 才可执行 destroy-method