在Spring中,可以使用 init-method 和 destroy-method 在bean 配置文件属性用于在bean初始化和销毁某些动作时。这是用来替代 InitializingBean和DisposableBean接口

示例

这里有一个例子向您展示如何使用 init-method 和 destroy-method。
package com.yiibai.customer.services;

public class CustomerService
{
String message; public String getMessage() {
return message;
} public void setMessage(String message) {
this.message = message;
} public void initIt() throws Exception {
System.out.println("Init method after properties are set : " + message);
} public void cleanUp() throws Exception {
System.out.println("Spring Container is destroy! Customer clean up");
} }

File : applicationContext.xml, 在bean中定义了init-method和destroy-method属性。

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="customerService" class="com.yiibai.customer.services.CustomerService"
init-method="initIt" destroy-method="cleanUp"> <property name="message" value="i'm property message" />
</bean> </beans>

执行下面的程序代码:

package com.yiibai.common;

import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.yiibai.customer.services.CustomerService; public class App
{
public static void main( String[] args )
{
ConfigurableApplicationContext context =
new ClassPathXmlApplicationContext(new String[] {"applicationContext.xml"}); CustomerService cust = (CustomerService)context.getBean("customerService"); System.out.println(cust); context.close();
}
}
ConfigurableApplicationContext.close将关闭应用程序上下文,释放所有资源,并销毁所有缓存的单例bean。

输出

Init method after properties are set : I'm property message
com.yiibai.customer.services.CustomerService@5f49d886
Spring Container is destroy! Customer clean up
 initIt()方法被调用,消息属性设置后,在 context.close()调用后,执行 cleanUp()方法;
建议使用init-method 和 destroy-methodbean 在Bena配置文件,而不是执行 InitializingBean 和 DisposableBean 接口,也会造成不必要的耦合代码在Spring。
 
下载源代码 – http://pan.baidu.com/s/1hreksq4

Spring Bean init-method 和 destroy-method实例的更多相关文章

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

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

  2. Spring Bean 生命周期之destroy——终极信仰

    上一篇文章 Spring Bean 生命周期之我从哪里来 说明了我是谁? 和 我从哪里来? 的两大哲学问题,今天我们要讨论一下终极哲学我要到哪里去? 初始化 Spring Bean 有三种方式: @P ...

  3. spring init method destroy method

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

  4. spring bean生命周期管理--转

    Life Cycle Management of a Spring Bean 原文地址:http://javabeat.net/life-cycle-management-of-a-spring-be ...

  5. 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的使用 使用 ...

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

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

  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. Spring Bean配置默认为单实例 pring Bean生命周期

    Bean默认的是单例的. 如果不想单例需要如下配置:<bean id="user" class="..." scope="singleton&q ...

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

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

  10. 在Spring Bean实例过程中,如何使用反射和递归处理的Bean属性填充?

    作者:小傅哥 博客:https://bugstack.cn 沉淀.分享.成长,让自己和他人都能有所收获! <Spring 手撸专栏>目录 [x] 第 1 章:开篇介绍,我要带你撸 Spri ...

随机推荐

  1. mysql 创建,授权,删除 用户

    1.创建用户 创建一个用户名是 lefunyun 密码是 X5A4FU8I0lKM21YPYUzP 账号 CREATE USER lefuyun@localhost IDENTIFIED BY 'X5 ...

  2. v4l

    v4l 2011-11-08 11:01:54|  分类: 默认分类|举报|字号 订阅     第一个部分介绍一些v4l的基本概念和基本方法,利用系统API完成一系列函数以方便后续应用程序的开发和使用 ...

  3. 推荐一个数据相关的网站tushare

    推荐一个网站:tushare 使用方法如下: pip install tushare 我是使用pycharm直接安装的 抓取了浦发和光大的股票数据,并通过csv进行保存,和通过plt进行图片打印 im ...

  4. Next Permutation——简单、经典

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

  5. 【前端笔记】浅谈js继承

    我们先想想我们用js最后要怎样实现面向对象的编程.事实上我们必须用上原型链这种东西. 我们的父类superType有属性和方法,并且一些能被子类subType继承,一些能被覆盖,但是丝毫不会影响到父类 ...

  6. 纯js的N级联动列表框 —— 基于jQuery

    多个列表框联动,不算是啥大问题,但是却挺麻烦,那么怎么才能够尽量方便一点呢?网上搜了一下,没发现太好用的,于是就自己写了一个.基于jQuery,无限级联动,支持下拉列表框和列表框. 先说一下步骤和使用 ...

  7. beego小技巧两则:通过命令行自定义端口和环境,url中带有中划线处理

    1,通过命令行自定义端口和环境 先用flag提取参数,如果要环境,就反向注进去, 如果是端口,就打到beego.Run函数里去. 2,在作router里的url解析时,如果URL中含有-中划线,用严格 ...

  8. Linux下进程信息/proc/pid/status的深入分析

    https://blog.csdn.net/beckdon/article/details/48491909

  9. CentOS/RHEL Linux安装EPEL第三方软件源

    https://www.vpser.net/manage/centos-rhel-linux-third-party-source-epel.html

  10. Hadoop案例(七)MapReduce中多表合并

    MapReduce中多表合并案例 一.案例需求 订单数据表t_order: id pid amount 1001 01 1 1002 02 2 1003 03 3 订单数据order.txt 商品信息 ...