在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/beanshttp://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. spring bean生命周期管理--转

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

  2. spring bean的创建,生命周期

    1. 初探spring 什么是spring Spirng 是分层的Java se/ee应用full-stack(web层mvc dao层jdbc模板 业务事务管理)轻量级开源框架,以IoC(inver ...

  3. (Do not request a bean from a BeanFactory in a destroy method implementation!)

    异常:Singleton bean creation not allowed while singletons of this factory are in destruction (Do not r ...

  4. Error creating bean with name ‘multipartResolver‘: Lookup method resolution failed; 上传文件异常

    浏览器错误信息: 错误信息: HTTP状态 500 - 内部服务器错误 类型 异常报告消息 Servlet[springmvctest]的Servlet.init()引发异常描述 服务器遇到一个意外的 ...

  5. spring bean的init、destory的几种方法及生命周期

    个人总结spring-bean的声明周期维护有三种: 注解:通过@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作 xml中配置:xml中定义ini ...

  6. Spring Bean默认配置为单实例 Spring Bean生命周期

    2019独角兽企业重金招聘Python工程师标准>>> Spring 的Bean默认的是单例的. 如果不想单例需要如下配置: <bean id="user" ...

  7. Spring Bean配置默认为单实例 pring Bean生命周期

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

  8. spring 笔记2:Spring MVC : Did not find handler method for 问题的解决

    spring 笔记2:Spring MVC : Did not find handler method for 问题的解决 参考文章: (1)spring 笔记2:Spring MVC : Did n ...

  9. Spring bean的生命周期init和destory

    Bean生命周期:定义 --- 初始化 --- 使用 --- 销毁 一.初始化: 方法1.实现org.springframework.beans.foctory.InitializingBean接口, ...

  10. react项目在ie11浏览器运行报错Instance method `debug.destroy()` is deprecated and no longer does anything

    react项目运行时,ie11浏览器报错:Instance method `debug.destroy()` is deprecated and no longer does anything. It ...

最新文章

  1. linux中cp:overwrite提示的问题
  2. JAVA并发编程8_线程池的使用
  3. JSON Funcs
  4. js工作笔记001---javascript中(function($){...})(jQuery)写法是什么意思
  5. Mac使用ssh公钥免密登录服务器
  6. android内核源码下载和编译
  7. 系统简单的UIImagePickerController
  8. 空间计量模型_5种经典空间计量模型的回归命令、程序及原始数据:SAR模型、SDM模型、SAC模型、SEM模型及GSPRE模型...
  9. windows cmd 快速编辑模式
  10. web逻辑思维题目_Java Web面试题整理(思维导图)
  11. 使用VM Ware创建虚拟机
  12. python 爬取 强智科技教务系统(湖南)
  13. 测试网页版淘宝购物车
  14. Ultra Recal 一款DIY的个人管理软件
  15. 最后一公里极速配送(一)
  16. 高德地图上线新能源导航 一站式充电服务缓解里程焦虑
  17. PhotoshopCC2019(64位)下载+安装教程
  18. 【7.talker.py listener.py】转载自他人作品,相关专业知识咨询相关人员!
  19. 使用计算机正确坐姿,如何保持正确坐姿?(多图)
  20. 数商云跨境电商平台运营总结:整合渠道+深引流量

热门文章

  1. C语言用warshall算法求传递闭包transitive closure(附完整源码)
  2. C++为什么摒弃auto_ptr
  3. QT的QSGGeometry类的使用
  4. QT的QCameraFocus类的使用
  5. 经典C语言程序100例之二五
  6. 经典C语言程序100例之十二
  7. id文件夹怎样传入服务器,服务器id怎么设置
  8. linux ram 权限,我如何将Linux二进制文件限制为有限的RAM数量?
  9. Win7下Android模拟器中没有3G网络信号的解决办法
  10. Kettle使用_10 存储过程与命名参数