日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring Bean init-method 和 destroy-method实例

發布時間:2024/9/20 javascript 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring Bean init-method 和 destroy-method实例 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在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实例的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。