在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

最新文章

  1. ASP.NET Core 中文文档 第二章 指南(4.3)添加 View
  2. vim的使用与配置
  3. CF733C Epidemic in Monstropolis[模拟 构造 贪心]
  4. Autorun.inf文件(2):改变硬盘分区图标
  5. php-建造者模式(Builder)解析
  6. unity安卓和IOS读写目录
  7. putty如何使用
  8. 【Linux】netdata监控组件
  9. 仿写Windows7桌面和任务栏 HTML5+CSS3+Jquery实现
  10. DBSCAN
  11. 浅析android系统设计中的回调思想
  12. Windows下安装MySQL8
  13. Asp.net中web.config配置文件详解(一)
  14. ThinkPHP从零开始(一)安装和配置
  15. 解决:CentOS下的 error while loading shared libraries: libmysqlclient.so.16: cannot open shared object file: No such file or dir
  16. POJ 1944 Fiber Communications (枚举 + 并查集 OR 线段树)
  17. java管道通信
  18. 输入N组父子对,求父子对所组成的二叉树的高度----17年某公司的笔试题
  19. ESP8266/ESP32模块晶振频偏调试
  20. SQL Server 2008中的Hints(提示)的简单整理

热门文章

  1. .pnts点云
  2. Base Class 慎用箭头函数
  3. 取消cp确认
  4. js 正则验证多个邮箱,用;隔开的那种
  5. 字符串处理strcpy strcat函数的用法
  6. centos7 修改时区
  7. VMware虚拟机VMware Authorization Service不能启动问题
  8. .NET基本权限系统框架源代码
  9. asp.net form 验证方式的使用(转载)
  10. 【小思考】Python里面有声明和定义分离这一说么?