溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Spring實戰(zhàn)之Bean銷毀之前的行為操作示例

發(fā)布時間:2020-09-28 17:11:37 來源:腳本之家 閱讀:132 作者:cakincqm 欄目:編程語言

本文實例講述了Spring實戰(zhàn)之Bean銷毀之前的行為操作。分享給大家供大家參考,具體如下:

一 配置

<?xml version="1.0" encoding="GBK"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns="http://www.springframework.org/schema/beans"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
   <bean id="steelAxe" class="org.crazyit.app.service.impl.SteelAxe"/>
   <!-- 配置chinese Bean,使用destroy-method="close"
      指定該Bean實例被銷毀之前,Spring會自動執(zhí)行指定該Bean的close方法 -->
   <bean id="chinese" class="org.crazyit.app.service.impl.Chinese"
      destroy-method="close">
      <property name="axe" ref="steelAxe"/>
   </bean>
</beans>

二 接口

1 Axe

package org.crazyit.app.service;
public interface Axe
{
   public String chop();
}

2 Person

package org.crazyit.app.service;
public interface Person
{
   public void useAxe();
}

三 Bean

1 Chinese

package org.crazyit.app.service.impl;
import org.springframework.beans.factory.DisposableBean;
import org.crazyit.app.service.*;
public class Chinese implements Person,DisposableBean
{
  private Axe axe;
  public Chinese()
  {
    System.out.println("Spring實例化主調(diào)bean:Chinese實例...");
  }
  public void setAxe(Axe axe)
  {
    System.out.println("Spring執(zhí)行依賴關系注入...");
    this.axe = axe;
  }
  public void useAxe()
  {
    System.out.println(axe.chop());
  }
  public void close()
  {
    System.out.println("正在執(zhí)行銷毀之前的方法 close...");
  }
  public void destroy() throws Exception
  {
    System.out.println("正在執(zhí)行銷毀之前的方法 destroy...");
  }
}

2 SteelAxe

package org.crazyit.app.service.impl;
import org.crazyit.app.service.*;
public class SteelAxe implements Axe
{
   public SteelAxe()
   {
      System.out.println("Spring實例化依賴bean:SteelAxe實例...");
   }
   public String chop()
   {
      return "鋼斧砍柴真快";
   }
}

四 測試類

package lee;
import org.springframework.context.*;
import org.springframework.context.support.*;
import org.crazyit.app.service.*;
public class BeanTest
{
  public static void main(String[] args)
  {
    // 以CLASSPATH路徑下的配置文件創(chuàng)建ApplicationContext
    AbstractApplicationContext ctx = new
      ClassPathXmlApplicationContext("beans.xml");
    // 獲取容器中的Bean實例
    Person p = ctx.getBean("chinese" , Person.class);
    p.useAxe();
    // 為Spring容器注冊關閉鉤子
    ctx.registerShutdownHook();
  }
}

五 測試結果

Spring實例化依賴bean:SteelAxe實例...
Spring實例化主調(diào)bean:Chinese實例...
Spring執(zhí)行依賴關系注入...
鋼斧砍柴真快
九月 21, 2019 9:30:18 下午  org.springframework.context.support.ClassPathXmlApplicationContext doClose
信息: Closing  org.springframework.context.support.ClassPathXmlApplicationContext@5a10411: startup date [Sat Sep 21 21:30:18 CST 2019]; root of  context hierarchy
正在執(zhí)行銷毀之前的方法 destroy...
正在執(zhí)行銷毀之前的方法 close...

更多關于java相關內(nèi)容感興趣的讀者可查看本站專題:《Spring框架入門與進階教程》、《Java數(shù)據(jù)結構與算法教程》、《Java操作DOM節(jié)點技巧總結》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》

希望本文所述對大家java程序設計有所幫助。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI