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 run after PropertiesSet() 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 InitializingBeanand 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

<?xml version="1.0"?>
<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 :)

Output

Init method after properties are set : im property message
com.mkyong.customer.services.CustomerService@47393f
...
INFO: Destroying singletons in org.springframework.beans.factory.
support.DefaultListableBeanFactory@77158a: 
defining beans [customerService]; root of factory hierarchy
Spring Container is destroy! Customer clean up

The afterPropertiesSet() method is called, after the message property is set; while the destroy() method is call after the context.close();

Thoughts…
I would not recommend to use InitializingBean and DisposableBean interface, because it will tight coupled your code to Spring. A better approach should be specifying the init-method and destroy-method attributes in your bean configuration file.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末于游,一起剝皮案震驚了整個(gè)濱河市莲祸,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌由蘑,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,820評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件叮趴,死亡現(xiàn)場(chǎng)離奇詭異戒傻,居然都是意外死亡纸泄,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,648評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門幅慌,熙熙樓的掌柜王于貴愁眉苦臉地迎上來宋欺,“玉大人,你說我怎么就攤上這事胰伍〕莸” “怎么了?”我有些...
    開封第一講書人閱讀 168,324評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵骂租,是天一觀的道長(zhǎng)祷杈。 經(jīng)常有香客問我,道長(zhǎng)渗饮,這世上最難降的妖魔是什么但汞? 我笑而不...
    開封第一講書人閱讀 59,714評(píng)論 1 297
  • 正文 為了忘掉前任,我火速辦了婚禮互站,結(jié)果婚禮上私蕾,老公的妹妹穿的比我還像新娘。我一直安慰自己云茸,他們只是感情好是目,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,724評(píng)論 6 397
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著标捺,像睡著了一般懊纳。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上亡容,一...
    開封第一講書人閱讀 52,328評(píng)論 1 310
  • 那天嗤疯,我揣著相機(jī)與錄音,去河邊找鬼闺兢。 笑死茂缚,一個(gè)胖子當(dāng)著我的面吹牛戏罢,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播脚囊,決...
    沈念sama閱讀 40,897評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼龟糕,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了悔耘?” 一聲冷哼從身側(cè)響起讲岁,我...
    開封第一講書人閱讀 39,804評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎衬以,沒想到半個(gè)月后缓艳,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,345評(píng)論 1 318
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡看峻,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,431評(píng)論 3 340
  • 正文 我和宋清朗相戀三年阶淘,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片互妓。...
    茶點(diǎn)故事閱讀 40,561評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡溪窒,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出冯勉,到底是詐尸還是另有隱情霉猛,我是刑警寧澤,帶...
    沈念sama閱讀 36,238評(píng)論 5 350
  • 正文 年R本政府宣布珠闰,位于F島的核電站惜浅,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏伏嗜。R本人自食惡果不足惜坛悉,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,928評(píng)論 3 334
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望承绸。 院中可真熱鬧裸影,春花似錦、人聲如沸军熏。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,417評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)荡澎。三九已至均践,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間摩幔,已是汗流浹背彤委。 一陣腳步聲響...
    開封第一講書人閱讀 33,528評(píng)論 1 272
  • 我被黑心中介騙來泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留或衡,地道東北人焦影。 一個(gè)月前我還...
    沈念sama閱讀 48,983評(píng)論 3 376
  • 正文 我出身青樓车遂,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親斯辰。 傳聞我的和親對(duì)象是個(gè)殘疾皇子舶担,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,573評(píng)論 2 359

推薦閱讀更多精彩內(nèi)容