春のBeanが初期化される(INIT、破壊します)

春のBeanコンテナ初期化の一般的な3つの方法があります。

@PostConstructは、PreDestroy @:ジャワEE5仕様から始まる、サーブレットはPostConstructと@PreDestroy @、2つのノートサーブレットのライフサイクルの影響を増加させます。@PostConstructは、サーブレットの後に初期化するためにコンストラクタの前に実行されます

パッケージcom.edu.bean。


輸入org.springframework.stereotype.Component。

輸入javax.annotation.PostConstruct。
輸入javax.annotation.PreDestroy。

/ * * 
 *のIntelliJ IDEAによって作成されます。
 *ユーザー:chenzhubing 
 *日:2019年6月17日
 * / 
@Component 
パブリック クラス色{ 

    @PostConstruct 
    公共 のinit(){ 
        システム。アウト .println(" INIT ...... " )。

    } 

    @PreDestroy 
    公共 ボイドは(破壊){ 
        システム。アウト(.println .........破壊" ); 

    } 


} 

パッケージcom.edu.bean; 

輸入org.springframework.context.annotation.AnnotationConfigApplicationContext; 

/ * * 
 のIntelliJ IDEAによって作成されます*。
 *ユーザー:chenzhubing 
 *日付:2019 / 6月17日
 * / 
パブリック クラスTest1を{
     公共 静的 ボイドメイン(文字列[]引数){ 

        AnnotationConfigApplicationContextコンテキスト = 新しい AnnotationConfigApplicationContext(" com.edu.bean " ); 
        context.close(); 

    } 
}

出力:

その中に......

破壊する.........

  • @Bean指定にinitMethod、destroyMethodをする方法
パッケージcom.edu.bean。


輸入javax.annotation.PostConstruct。
輸入javax.annotation.PreDestroy。

/ * * 
 *のIntelliJ IDEAによって作成されます。
 *ユーザー:chenzhubing 
 *日:2019年6月17日
 * / 

パブリック クラスの色{ 

    @PostConstruct 
    公共 のinit(){ 
        システム。アウト .println(" INIT ...... " )。

    } 

    公共 ボイドINIT1(){ 
        システム。アウト .println(" INIT1 ...... " )。

    } 

    @PreDestroyの
    公共 破壊(){ 
        システム。アウト .println(" .........破壊" ); 

    } 

    公共 ボイドdestroy1(){ 
        システム。アウト .println(" destroy1 ......... " )。

    } 


} 


パッケージcom.edu.bean。

輸入org.springframework.context.annotation.Bean。
輸入org.springframework.context.annotation.Configuration。

/ * * 
 *のIntelliJ IDEAによって作成されます。
 *ユーザー:chenzhubing 
 *日:2019年6月17日
 * / 
@Configurationの
クラスコンフィグ{ 

    @Bean(名= " "、にinitMethod = " INIT1 "、destroyMethodをは= " destroy1 " パブリック  カラーGETCOLOR(){ 

        戻り 新しい)(カラー; 
    } 

} 


パッケージcom.edu.bean。

輸入org.springframework.context.annotation.AnnotationConfigApplicationContext; 

/ * * 
 *のIntelliJ IDEAによって作成されます。
 *ユーザー:chenzhubingの
 *日:2019年6月17日
 * / 
パブリック クラスTest1を{
     公共 静的 ボイドメイン(文字列[]引数){

        AnnotationConfigApplicationContextコンテキスト = 新しい AnnotationConfigApplicationContext(コンフィグ。クラス); 
        context.close(); 

    } 
}

出力:

熱......
INIT1 ......

破壊.........
destroy1 .........

プルーフ@PostConstruct修飾方法はにinitMethod改良法の前に行われます

 

  • インタフェースInitializingBean、DisposableBean:重写afterPropertiesSet、破壊する方法
パッケージcom.edu.bean。


輸入org.springframework.beans.factory.DisposableBean。
輸入org.springframework.beans.factory.InitializingBean。
輸入org.springframework.stereotype.Component。

輸入javax.annotation.PostConstruct。
輸入javax.annotation.PreDestroy。

/ * * 
 *のIntelliJ IDEAによって作成されます。
 *ユーザー:chenzhubing 
 *日:2019年6月17日
 * / 
@Component 
パブリック クラス色はInitializingBean、DisposableBean {実装

    @PostConstruct 
    公共 のinit(){ 
        システム。アウト .println(" INIT ...... " )。

    }
    @PreDestroy 
    公共 ボイド(){破壊する
        システム。アウト .println(" .........破壊" ); 

    } 
    @Override 
    公共 ボイドafterPropertiesSet()例外{スロー
        システム。アウト .println(" INIT1 ......... " )。
    } 

    @Override 
    公共 ボイドは(破壊)例外{スロー
        システム。アウト .println(" destroy1 ...... " )。
    } 
} 


パッケージcom.edu.bean。

輸入org.springframework.context.annotation.AnnotationConfigApplicationContext; 

/ * * 
 *のIntelliJ IDEAによって作成されます。
 *ユーザー:chenzhubingの
 *日:2019年6月17日
 * / 
パブリック クラスTest1を{
     公共 静的 ボイドメイン(文字列[]引数){ 

        AnnotationConfigApplicationContextコンテキスト = 新しい AnnotationConfigApplicationContext(" com.edu.bean " )。
        context.close(); 

    } 
}

出力:

熱......
INIT1 .........

破壊.........
destroy1 ......

 

おすすめ

転載: www.cnblogs.com/chenzhubing/p/11041360.html