Spring init-method和destroy-method 的使用
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 的使用的更多相关文章
- java代码中init method和destroy method的三种使用方式
在java的实际开发过程中,我们可能常常需要使用到init method和destroy method,比如初始化一个对象(bean)后立即初始化(加载)一些数据,在销毁一个对象之前进行垃圾回收等等. ...
- spring init method destroy method
在java的实际开发过程中,我们可能常常需要使用到init method和destroy method,比如初始化一个对象(bean)后立即初始化(加载)一些数据,在销毁一个对象之前进行垃圾回收等等. ...
- 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的使用 使用 ...
- Invoking destroy method 'close' on bean with name 'dataSource'
Invoking destroy method 'close' on bean with name 'dataSource' Spring与Mybatis整合时出现的问题,找了一晚上结果是一个属性写错 ...
- Spring 通过工厂方法(Factory Method)来配置bean
Spring 通过工厂方法(Factory Method)来配置bean 在Spring的世界中, 我们通常会利用bean config file 或者 annotation注解方式来配置bean. ...
- kendo method:destroy 解决有些在kendo.all.js 的js 库里报错问题
首先,不得不承认,kendo UI 是个不错的东西,特别对于一个前端开发到行不足的程序猿来说.而在我们使用过程中貌似还是会遇到各种奇怪的问题.比如我们会经常用到对一些控件进行重赋值. destroy ...
- Invocation of destroy method failed on bean with name ‘XXXX’
项目启动报错问题:Invocation of destroy method failed on bean with name 'scopedTarget.eurekaClient': org.spri ...
- ServletContext结合Servlet接口中的init()方法和destroy()方法的运用----网站计数器
我们一般知道Servlet接口中的init()方法在tomcat启动时调用,destroy()方法在tomcat关闭时调用.那么这两个方法到底在实际开发中有什么作用呢?这就是这个随笔主要讲的内容. 思 ...
- 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 ...
- 关于.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 ...
随机推荐
- dev LayoutControl布局控件
标题长短不一致,很难看 设置LayoutControl属性 效果: 修改字体: 控件字体: LayoutControl→Appearance→Control→Font→FontSize (Contr ...
- hihoCoder 1427 : What a Simple Research(大㵘研究)
hihoCoder #1427 : What a Simple Research(大㵘研究) 时间限制:1000ms 单点时限:1000ms 内存限制:256MB Description - 题目描述 ...
- 在WebBrowser中截获弹出对话框内容并将其屏蔽
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- footer绝对定位但是不在页面最下边解决方案
方案一 html { height: 100%; } body { position: relative; min-height: 100%; box-sizing: border-box; padd ...
- Strus2学习Exception处理集锦(一)
2015-01-05 同学用dom4j写的一个更改节点文本的方法爆出这样一个错误: 代码: for(Iterator itr=root.elementIterator();itr.hasNext(); ...
- winpcap抓包原理
winpcap抓包原理 WinPcap 是由伯克利分组捕获库派生而来的分组捕获库,它是在Windows 操作平台上来实现对底层包的截取过滤.WinPcap 是 BPF 模型和 Libpcap 函数库在 ...
- SendInput模拟键盘输入的问题
SendInput模拟键盘输入的问题 http://www.cnblogs.com/yedaoq/archive/2010/12/30/1922305.html 最近接触到这个函数,因此了解了一下, ...
- AJAX第二发
开始总结自己在项目上的使用AJAX的情况: 该图为向后台请求的数据,前台返回的结果,返回的数据类型为object,businessObj由许多key组成,我们项目前台需要调用的数据就藏在数组recor ...
- DFA敏感词过滤
import java.io.UnsupportedEncodingException; import java.nio.ByteBuffer; import java.util.ArrayList; ...
- PE文件学习系列笔记四-C++实现PE文件的分析
合肥程序员群:49313181. 合肥实名程序员群:128131462 (不愿透露姓名和信息者勿加入) Q Q:408365330 E-Mail:egojit@qq.com 综述: 首 ...