乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      Spring 的 init-method和destory-method

       日月桃子 2015-01-15

      關(guān)于在spring  容器初始化 bean 和銷毀前所做的操作定義方式有三種:

      第一種:通過@PostConstruct 和 @PreDestroy 方法 實(shí)現(xiàn)初始化和銷毀bean之前進(jìn)行的操作

      第二種是:通過 在xml中定義init-method 和  destory-method方法

      第三種是: 通過bean實(shí)現(xiàn)InitializingBean和 DisposableBean接口



      在xml中配置 init-method和 destory-method方法

      只是定義spring 容器在初始化bean 和容器銷毀之前的所做的操作

      基于xml的配置只是一種方式:


      直接上xml中配置文件:

      1. <bean id="personService" class="com.myapp.core.beanscope.PersonService" scope="singleton"  init-method="init"  destroy-method="cleanUp">  
      2.   
      3. </bean>  


      定義PersonService類:

      1. package com.myapp.core.beanscope;  
      2.   
      3.   
      4. public class PersonService  {  
      5.    private String  message;  
      6.   
      7.     public String getMessage() {  
      8.         return message;  
      9.     }  
      10.       
      11.     public void setMessage(String message) {  
      12.         this.message = message;  
      13.     }  
      14.      
      15.   
      16.       
      17.     public void init(){  
      18.         System.out.println("init");  
      19.     }  
      20.     //  how  validate the  destory method is  a question  
      21.     public void  cleanUp(){  
      22.         System.out.println("cleanUp");  
      23.     }  
      24. }  

      相應(yīng)的測試類:

      1. package com.myapp.core.beanscope;  
      2.   
      3. import org.springframework.context.support.AbstractApplicationContext;  
      4. import org.springframework.context.support.ClassPathXmlApplicationContext;  
      5.   
      6. public class MainTest {  
      7.   public static void main(String[] args) {  
      8.         
      9.       AbstractApplicationContext  context =new  ClassPathXmlApplicationContext("SpringBeans.xml");  
      10.       
      11.     PersonService  person = (PersonService)context.getBean("personService");  
      12.       
      13.     person.setMessage("hello  spring");  
      14.       
      15.     PersonService  person_new = (PersonService)context.getBean("personService");  
      16.       
      17.     System.out.println(person.getMessage());  
      18.     System.out.println(person_new.getMessage());  
      19.     context.registerShutdownHook();  
      20.   
      21.       
      22. }  
      23. }   

      測試結(jié)果:

      init
      hello  spring
      hello  spring
      cleanUp

      可以看出 init 方法和 clean up方法都已經(jīng)執(zhí)行了。


      context.registerShutdownHook(); 是一個(gè)鉤子方法,當(dāng)jvm關(guān)閉退出的時(shí)候會調(diào)用這個(gè)鉤子方法,在設(shè)計(jì)模式之 模板模式中 通過在抽象類中定義這樣的鉤子方法由實(shí)現(xiàn)類進(jìn)行實(shí)現(xiàn),這里的實(shí)現(xiàn)類是AbstractApplicationContext,這是spring 容器優(yōu)雅關(guān)閉的方法。



        本站是提供個(gè)人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多