Spring InitializingBean and DisposableBean example
In Spring, InitializingBean
and DisposableBean
are two marker interfaces, a useful way for Spring to perform certain actions upon bean initialization and destruction.
- For bean implemented
InitializingBean
, it will runafterPropertiesSet()
after all bean properties have been set. - For bean implemented
DisposableBean
, it will rundestroy()
after Spring container is released the bean.
Example
Here’s an example to show you how to use InitializingBean
and DisposableBean
. A CustomerService
bean to implement both InitializingBean
and DisposableBean
interface, and has a message
property.
package com.mkyong.customer.services;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
public class CustomerService implements InitializingBean, DisposableBean
{
String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public void afterPropertiesSet() throws Exception {
System.out.println("Init method after properties are set : " + message);
}
public void destroy() throws Exception {
System.out.println("Spring Container is destroy! Customer clean up");
}
}
File : Spring-Customer.xml
<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.mkyong.customer.services.CustomerService">
<property name="message" value="i'm property message" />
</bean>
</beans>
Run it
package com.mkyong.common;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.mkyong.customer.services.CustomerService;
public class App
{
public static void main( String[] args )
{
ConfigurableApplicationContext context =
new ClassPathXmlApplicationContext(new String[] {"Spring-Customer.xml"});
CustomerService cust = (CustomerService)context.getBean("customerService");
System.out.println(cust);
context.close();
}
}
The ConfigurableApplicationContext.close()
will close the application context, releasing all resources and destroying all cached singleton beans. It’s use for destroy()
method demo purpose only
Spring InitializingBean and DisposableBean example的更多相关文章
- spring InitializingBean和DisposableBean init-method 和destroy-method @PostConstruct @PreDestroy
对于初始化函数: @PostConstruct 注解的方法 InitializingBean接口定义的回调afterPropertiesSet() Bean配置中自定义的初始化函数 对于析构则与上相同 ...
- Spring中Bean的生命中期与InitializingBean和DisposableBean接口
Spring提供了一些标志接口,用来改变BeanFactory中的bean的行为.它们包括InitializingBean和DisposableBean.实现这些接口将会导致BeanFactory调用 ...
- Spring Bean InitializingBean和DisposableBean实例
在Spring中,InitializingBean和DisposableBean是两个标记接口,为Spring执行时bean的初始化和销毁某些行为时的有用方法. 对于Bean实现 Initializi ...
- 【Spring注解驱动开发】使用InitializingBean和DisposableBean来管理bean的生命周期,你真的了解吗?
写在前面 在<[Spring注解驱动开发]如何使用@Bean注解指定初始化和销毁的方法?看这一篇就够了!!>一文中,我们讲述了如何使用@Bean注解来指定bean初始化和销毁的方法.具体的 ...
- Spring InitializingBean 接口以及Aware接口实现的原理
关于Spring InitializingBean 接口以及Aware接口实现的其实都在 第11步中: finishBeanFactoryInitialization() 方法中完成了3部分的内容: ...
- InitializingBean 和 DisposableBean 指定初始化和销毁方法
通过实现 InitializingBean 和 DisposableBean 接口,也可以指定 bean 的初始化和销毁方法 二.Student 类 public class Student impl ...
- 13、生命周期-InitializingBean和DisposableBean
13.生命周期-InitializingBean和DisposableBean InitializingBean接口 package org.springframework.beans.factory ...
- spring中的DisposableBean和InitializingBean,ApplicationContextAware的用法
在spring容器初始化bean和销毁bean的以前的操作有很多种, 目前我知道的有:在xml中定义的时候用init-method和destory-method,还有一种就是定义bean的时候实现Di ...
- Spring bean 实现InitializingBean和DisposableBean接口实现初始化和销毁前操作
# InitializingBean接口> Spring Bean 实现这个接口,重写afterPropertiesSet方法,这样spring初始化完这个实体类后会调用这个方法```@Over ...
随机推荐
- apk反编译(7)用ProGuard混淆代码,初级防止反编译
eclipse为例 1,project.properties去掉 #proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:pro ...
- javascript算法汇总(持续更新中)
1. 线性查找 <!doctype html> <html lang="en"> <head> <meta charset="U ...
- svn: E180001: Unable to open an ra_local session to URL问题解决方案
在使用Android Studio的SVN导入项目时,出现了: svn: E180001: Unable to open an ra_local session to URLsvn: E180001: ...
- JS中令人发指的valueOf方法介绍
彭老湿近期月报里提到了valueOf方法,兴致来了翻了下ECMA5里关于valueOf方法的介绍,如下: 15.2.4.4 Object.prototype.valueOf ( ) When the ...
- Android远程图片获取和本地缓存
对于客户端——服务器端应用,从远程获取图片算是经常要用的一个功能,而图片资源往往会消耗比较大的流量,对 应用来说,如果处理不好这个问题,那会让用户很崩溃,不知不觉手机流量就用完了,等用户发现是你的应用 ...
- struct TABLE_SHARE
struct TABLE_SHARE { TABLE_SHARE() {} /* Remove gcc warning */ /** Category of this table. */ TABLE_ ...
- Ajax的“dataType”乱用的陷阱
$.doAjax({ url : "areaAction_synchronizeArea.do", data : { 'vrvRangeUrl' : synAreaHTTP ,'v ...
- 一个简单的ORM制作(SQL帮助类)
一个简单的ORM制作大概需要以下几个类: SQL执行类 CURD操作类 其他酱油类 先从SQL执行类说起,可能会涉及数据库的迁移等问题,所以需要定义一个接口以方便迁移到其他数据库, 事务没提供命名,若 ...
- vs2010 js代码折叠
方法一:插件 在Visaul Studio 2010中写js或css代码,缺少像写C#代码时的那种折叠功能,当代码比较多时,就很不方便. 但是已经有VS2010扩展支持这个功能,它就是--JSEn ...
- [转帖]Asp.NET 弹出页面
原文链接:http://www.cnblogs.com/adi-liu/archive/2008/07/18/1246091.html ASP.NET 弹出对话框和页面之间传递值的经验总结 今天碰到一 ...