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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring Bean InitializingBean和DisposableBean实例

發(fā)布時(shí)間:2024/9/20 javascript 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring Bean InitializingBean和DisposableBean实例 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
在Spring中,InitializingBean和DisposableBean是兩個(gè)標(biāo)記接口,為Spring執(zhí)行時(shí)bean的初始化和銷毀某些行為時(shí)的有用方法。
  • 對(duì)于Bean實(shí)現(xiàn) InitializingBean,它將運(yùn)行 afterPropertiesSet()在所有的 bean 屬性被設(shè)置之后。
  • 對(duì)于?Bean?實(shí)現(xiàn)了DisposableBean,它將運(yùn)行?destroy()在?Spring?容器釋放該?bean?之后。
  • 示例

    下面是一個(gè)例子,向您展示如何使用 InitializingBean 和 DisposableBean。一個(gè) CustomerService?bean來實(shí)現(xiàn) InitializingBean和DisposableBean 接口,并有一個(gè)消息(message)屬性。 package com.yiibai.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 : applicationContext.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/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd"><bean id="customerService" class="com.yiibai.customer.services.CustomerService"><property name="message" value="I'm property message" /></bean></beans>

    執(zhí)行它

    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()將關(guān)閉該應(yīng)用程序的上下文,釋放所有資源,并銷毀所有緩存的單例bean。它是只用于?destroy()?方的演示目的。

    輸出結(jié)果

    Init method after properties are set : I'm property message com.yiibai.customer.services.CustomerService@4090c06f Spring Container is destroy! Customer clean up afterPropertiesSet()方法被調(diào)用在 message 屬性設(shè)置后,而 destroy()方法是在調(diào)用 context.close()之后; 建議:
    不建議使用InitializingBean和DisposableBean的接口,因?yàn)樗鼘⒛愕拇a緊耦合到 Spring 代碼中。?一個(gè)更好的做法應(yīng)該是在bean的配置文件屬性指定init-method和destroy-method。 下載代碼 –?http://pan.baidu.com/s/1nu3cEXN

    總結(jié)

    以上是生活随笔為你收集整理的Spring Bean InitializingBean和DisposableBean实例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。