Spring init-method and destroy-method example

In Spring, you can use init-method and destroy-method as attribute in bean configuration file for bean to perform certain actions upon initialization and destruction. Alternative to InitializingBean and DisposableBean interface.\

Example

Here’s an example to show you how to use init-method and destroy-method.

package com.mkyong.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 : Spring-Customer.xml, define init-method and destroy-method attribute in your bean.

<?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" init-method="initIt" destroy-method="cleanUp">
        <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.

Output

Init method after properties are set : i'm 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 initIt() method is called, after the message property is set, and the cleanUp() method is called after the context.close();

Thoughts…
It’s always recommended to use init-method and destroy-method in bean configuration file, instead of implement the InitializingBean and DisposableBean interface to cause unnecessarily coupled your code to Spring.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市桥狡,隨后出現(xiàn)的幾起案子嘁锯,更是在濱河造成了極大的恐慌,老刑警劉巖臣缀,帶你破解...
    沈念sama閱讀 216,997評論 6 502
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異泻帮,居然都是意外死亡精置,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,603評論 3 392
  • 文/潘曉璐 我一進(jìn)店門锣杂,熙熙樓的掌柜王于貴愁眉苦臉地迎上來脂倦,“玉大人,你說我怎么就攤上這事元莫±底瑁” “怎么了?”我有些...
    開封第一講書人閱讀 163,359評論 0 353
  • 文/不壞的土叔 我叫張陵踱蠢,是天一觀的道長火欧。 經(jīng)常有香客問我棋电,道長,這世上最難降的妖魔是什么苇侵? 我笑而不...
    開封第一講書人閱讀 58,309評論 1 292
  • 正文 為了忘掉前任赶盔,我火速辦了婚禮,結(jié)果婚禮上榆浓,老公的妹妹穿的比我還像新娘于未。我一直安慰自己,他們只是感情好陡鹃,可當(dāng)我...
    茶點故事閱讀 67,346評論 6 390
  • 文/花漫 我一把揭開白布烘浦。 她就那樣靜靜地躺著,像睡著了一般萍鲸。 火紅的嫁衣襯著肌膚如雪谎倔。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,258評論 1 300
  • 那天猿推,我揣著相機與錄音片习,去河邊找鬼。 笑死蹬叭,一個胖子當(dāng)著我的面吹牛藕咏,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播秽五,決...
    沈念sama閱讀 40,122評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼孽查,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了坦喘?” 一聲冷哼從身側(cè)響起盲再,我...
    開封第一講書人閱讀 38,970評論 0 275
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎瓣铣,沒想到半個月后答朋,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,403評論 1 313
  • 正文 獨居荒郊野嶺守林人離奇死亡棠笑,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,596評論 3 334
  • 正文 我和宋清朗相戀三年梦碗,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片蓖救。...
    茶點故事閱讀 39,769評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡洪规,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出循捺,到底是詐尸還是另有隱情斩例,我是刑警寧澤,帶...
    沈念sama閱讀 35,464評論 5 344
  • 正文 年R本政府宣布从橘,位于F島的核電站念赶,受9級特大地震影響础钠,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜晶乔,卻給世界環(huán)境...
    茶點故事閱讀 41,075評論 3 327
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望牺勾。 院中可真熱鬧正罢,春花似錦、人聲如沸驻民。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,705評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽回还。三九已至裆泳,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間柠硕,已是汗流浹背工禾。 一陣腳步聲響...
    開封第一講書人閱讀 32,848評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留蝗柔,地道東北人闻葵。 一個月前我還...
    沈念sama閱讀 47,831評論 2 370
  • 正文 我出身青樓,卻偏偏與公主長得像癣丧,于是被迫代替她去往敵國和親槽畔。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,678評論 2 354

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