日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

Spring (init-method和destroy-method )

系統(tǒng) 2198 0

轉(zhuǎn)自: http://www.blogjava.net/cmzy/archive/2008/07/29/218059.html

  1. /**
  2. *
  3. */
  4. package ioc.test;
  5. /**
  6. *@authorzhangyong
  7. *
  8. */
  9. public class Animal{
  10. private Stringname;
  11. private int age;
  12. public Stringspeak(){
  13. return "我的名字:" + this .name+ ",我的年齡:" + this .age;
  14. }
  15. public void init_xx() throws Exception{
  16. System.out.println( "初始化方法start()正在運(yùn)行!" );
  17. }
  18. public void end() throws Exception{
  19. System.out.println( "析構(gòu)方法end()正在運(yùn)行!" );
  20. }
  21. //Geter和Seter省略
  22. }

配置文件中配置好bean,并為其指定響應(yīng)的預(yù)處理方法和析構(gòu)方法:

這里的init_xx方法在實(shí)例化與依賴注入之前被調(diào)用,end方法在銷毀之前調(diào)用

  1. <? xml version = "1.0" encoding = "UTF-8" ?>
  2. < beans
  3. xmlns = "http://www.springframework.org/schema/beans"
  4. xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
  5. xsi:schemaLocation ="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd" >
  7. < bean id = "animal" class = "ioc.test.Animal" init-method = "init_xx" destroy-method = "end" >
  8. < property name = "age" value = "5" > </ property >
  9. < property name = "name" value = "豬" > </ property >
  10. </ bean >
  11. </ beans >

創(chuàng)建含有主方法的測試類,代碼如下:

  1. /**
  2. *
  3. */
  4. package ioc.test;
  5. import org.springframework.context.support.AbstractApplicationContext;
  6. import org.springframework.context.support.ClassPathXmlApplicationContext;
  7. /**
  8. *@authorzhangyong
  9. *
  10. */
  11. public class TestMain{
  12. public static void main(String[]args){
  13. AbstractApplicationContextac= new ClassPathXmlApplicationContext( "applicationContext.xml" );
  14. //注冊容器關(guān)閉鉤子,才能關(guān)掉容器,調(diào)用析構(gòu)方法
  15. ac.registerShutdownHook();
  16. Animalanimal=ac.getBean( "animal", Animal.class );
  17. System.out.println(animal.speak());
  18. }
  19. }

運(yùn)行主類,結(jié)果如下:


需要注意的是:要看到析構(gòu)方法的輸出,也必須要注冊關(guān)閉鉤子。

Spring (init-method和destroy-method )


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會(huì)非常 感謝您的哦!!!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 登封市| 普兰县| 青岛市| 禄丰县| 吴堡县| 舞阳县| 庄河市| 宁化县| 惠安县| 固安县| 奈曼旗| 涞源县| 清水河县| 阿尔山市| 南安市| 澄城县| 灵台县| 夏邑县| 塔河县| 萨迦县| 禹州市| 乌审旗| 宾阳县| 尤溪县| 青州市| 萨迦县| 云安县| 鸡西市| 永登县| 利辛县| 绥宁县| 姚安县| 四川省| 夹江县| 沿河| 云龙县| 吉水县| 丹东市| 山东| 永寿县| 罗甸县|