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

Spring init-method和destroy-method 的使用的更多相关文章

  1. java代码中init method和destroy method的三种使用方式

    在java的实际开发过程中,我们可能常常需要使用到init method和destroy method,比如初始化一个对象(bean)后立即初始化(加载)一些数据,在销毁一个对象之前进行垃圾回收等等. ...

  2. spring init method destroy method

    在java的实际开发过程中,我们可能常常需要使用到init method和destroy method,比如初始化一个对象(bean)后立即初始化(加载)一些数据,在销毁一个对象之前进行垃圾回收等等. ...

  3. EurekaClient项目启动报错Invocation of destroy method failed on bean with name 'scopedTarget.eurekaClient': org.springframework.beans.factory.BeanCreationNotAllowedException: Error creating bean with name 'e

    Disconnected from the target VM, address: '127.0.0.1:51233', transport: 'socket' Eureka Client的使用 使用 ...

  4. Invoking destroy method 'close' on bean with name 'dataSource'

    Invoking destroy method 'close' on bean with name 'dataSource' Spring与Mybatis整合时出现的问题,找了一晚上结果是一个属性写错 ...

  5. Spring 通过工厂方法(Factory Method)来配置bean

    Spring 通过工厂方法(Factory Method)来配置bean 在Spring的世界中, 我们通常会利用bean config file 或者 annotation注解方式来配置bean. ...

  6. kendo method:destroy 解决有些在kendo.all.js 的js 库里报错问题

    首先,不得不承认,kendo UI 是个不错的东西,特别对于一个前端开发到行不足的程序猿来说.而在我们使用过程中貌似还是会遇到各种奇怪的问题.比如我们会经常用到对一些控件进行重赋值. destroy ...

  7. Invocation of destroy method failed on bean with name ‘XXXX’

    项目启动报错问题:Invocation of destroy method failed on bean with name 'scopedTarget.eurekaClient': org.spri ...

  8. ServletContext结合Servlet接口中的init()方法和destroy()方法的运用----网站计数器

    我们一般知道Servlet接口中的init()方法在tomcat启动时调用,destroy()方法在tomcat关闭时调用.那么这两个方法到底在实际开发中有什么作用呢?这就是这个随笔主要讲的内容. 思 ...

  9. Modified Least Square Method and Ransan Method to Fit Circle from Data

    In OpenCv, it only provide the function fitEllipse to fit Ellipse, but doesn't provide function to f ...

  10. 关于.ToList(): LINQ to Entities does not recognize the method ‘xxx’ method, and this method cannot be translated into a store expression.

    LINQ to Entities works by translating LINQ queries to SQL queries, then executing the resulting quer ...

随机推荐

  1. Python if 和 for 的多种写法

    a, b, c = 1, 2, 3 [对比Cpp里:c = a >b? a:b]这个写法,Python只能常规的空行,缩进吗? 人生苦短,我用python,下面介绍几种if的方便的方法. 1.常 ...

  2. 办公大楼3D指纹门禁系统解决方案

    随着人们对工作.生活的自动化水平也提出了越来越高的要求.以大楼安保对出入大楼的外来人员进行登记放行或以铁锁.钥匙和卡为代表的出入管理方式已无法满足需求. 利用科技的手段,实现办公大楼的安全现代化.管理 ...

  3. oracle,mysql,SqlServer三种数据库的分页查询的实例。

    MySql: MySQL数据库实现分页比较简单,提供了 LIMIT函数.一般只需要直接写到sql语句后面就行了.LIMIT子 句可以用来限制由SELECT语句返回过来的数据数量,它有一个或两个参数,如 ...

  4. c++实现简单的链表

    注:我是一个编程菜鸟,哪个大神看出来缺陷提点一下,感激不尽. 链表由一个个的节点串联而成,同一由first头指针管理,属于线性表中相比于数组,添加删除方便,但访问又有点慢的数据结构. 第一步:节点 N ...

  5. s5pv210启动debian出错提示bash: cannot set terminal process group (-1): Inappropriate ioctl for device

    1.启动参数如下: bootargs=root=/dev/nfs nfsroot=192.168.1.8:/opt/wheezy_fs ip=192.168.1.9:192.168.1.8:192.1 ...

  6. git中忽略UserInterfaceState.xcuserstate的方法

    在commit 时候一直会提示userinterfacestate.xcuserstate文件尚未commit. 你可以用命令行 git rm --cached [YourProjectName].x ...

  7. MyBatis 入门(一)

    MyBatis mybatis和hibernate都属于orm(对象与关系映射) 框架 mybatis的优点: 1.sql-mapping :操作更自由,可控性高,执行效率更高 2.轻量,学习更容易 ...

  8. 转:ProgressMonitorDialog

    http://stackoverflow.com/questions/12986912/using-progressmonitordialog-in-eclipse-4-properly public ...

  9. cookie记录用户名

    在说如何用cookie记录用户名之前,我们先来说说cookie的工作原理: cookie : 存储数据,当用户访问了某个网站(网页)的时候,我们就可以通过cookie来像访问者电脑上存储数据 ; 1. ...

  10. mysql 5.6.34 二进制

    安装方法 http://dev.mysql.com/doc/refman/5.6/en/binary-installation.html shell> groupadd mysqlshell&g ...