最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2019年11月28日 08:39:34   作者:cakincqm  
這篇文章主要介紹了Spring實(shí)戰(zhàn)之Bean銷毀之前的行為操作,結(jié)合實(shí)例形式分析了spring在bean銷毀之前的行為相關(guān)設(shè)置與使用技巧,需要的朋友可以參考下

本文實(shí)例講述了Spring實(shí)戰(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實(shí)例被銷毀之前,Spring會(huì)自動(dòng)執(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實(shí)例化主調(diào)bean:Chinese實(shí)例...");
  }
  public void setAxe(Axe axe)
  {
    System.out.println("Spring執(zhí)行依賴關(guān)系注入...");
    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實(shí)例化依賴bean:SteelAxe實(shí)例...");
   }
   public String chop()
   {
      return "鋼斧砍柴真快";
   }
}

四 測(cè)試類

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實(shí)例
    Person p = ctx.getBean("chinese" , Person.class);
    p.useAxe();
    // 為Spring容器注冊(cè)關(guān)閉鉤子
    ctx.registerShutdownHook();
  }
}

五 測(cè)試結(jié)果

Spring實(shí)例化依賴bean:SteelAxe實(shí)例...
Spring實(shí)例化主調(diào)bean:Chinese實(shí)例...
Spring執(zhí)行依賴關(guān)系注入...
鋼斧砍柴真快
九月 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...

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

希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論

京山县| 花垣县| 溧阳市| 晴隆县| 合川市| 东莞市| 宣威市| 蒙城县| 闵行区| 皮山县| 宽城| 怀远县| 张掖市| 威远县| 和静县| 安达市| 华容县| 东安县| 云阳县| 榆中县| 萨嘎县| 屯门区| 彰化县| 宜宾市| 罗城| 洪雅县| 额济纳旗| 水城县| 茌平县| 舟山市| 景东| 泸西县| 阜阳市| 龙陵县| 常熟市| 玛曲县| 资溪县| 和静县| 宝应县| 津南区| 无棣县|