zoukankan      html  css  js  c++  java
  • Spring init-method和destroy-method 的使用

    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

  • 相关阅读:
    Wiin10 深色模式 暗色 省电 经济 护眼dark mode energy saving ecol
    云主机终端现实中文
    systemctl enable与systemctl start的区别
    centos技巧
    用国内的vps登录亚马逊的ec2
    小米 redmi note 8 打开开发者选项
    javascript数组、对象和Null的typeof同为object,区分解决办法
    组件中是否可以判断slot是否有内容?
    xlsx-style 行高设置
    修改xlsx-style 源码 解决报错
  • 原文地址:https://www.cnblogs.com/kzhan/p/4641778.html
Copyright © 2011-2022 走看看