Spring Beanライフサイクルを制御するためのいくつかの方法


前言:
    Spring中のBeanは作成から廃棄までの段階を経て、これらの段階において、いくつかの方法によってビーンの作成、ビーンの破壊を制御することができます.
    Springの公式文書には、こんな話があります.
As of Spring 2.5, you have three options for controlling bean lifecycle behavior: the InitializingBean and DisposableBean callback interfaces; custom init() and destroy() methods; and the @PostConstruct and @PreDestroy annotations. You can combine these mechanisms to control a given bean
   具体的には、Spring 2.5以降、Beanのライフサイクルをコントロールする3つの方法があります.
    1.InitializingBenとDispposable Beanインターフェースを通じて
    2.Beanのinit()とdestroy()方法
    3.PostConstructと@Predestryコメント
 
    この3つの方法の実現を見に来ました.
 
1.InitializingBenとDispposable Beanインターフェース
    1)まずクラスを作成し、LifeCycleBeanと名づけて、この二つのインターフェースを実現し、その方法を実現します.
public class LifeCycleBean implements Serializable , InitializingBean, DisposableBean {

    private static final long serialVersionUID = 2731651017595722776L;


    public void destroy() throws Exception {
        System.out.println("DisposableBean destroy...");
    }

    public void afterPropertiesSet() throws Exception {
        System.out.println("InitializingBean init...");
    }
}
    2)このBeans.xmlにおいて、このBeanを定義する.
    3)試験類の生命周期試験方法
@Test
public void testXml(){
    ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
    LifeCycleBean lifeCycleBean = (LifeCycleBean) applicationContext.getBean("lifeCycleBean");
    applicationContext.close();
}
// res
InitializingBean init...
DisposableBean destroy...
    まとめ:LifeCycleBenが取得されたとき、afterPropertiesset()方法が呼び出されます.容器が閉まる前にdestroy()メソッドが呼び出されます.
 
2.Beanのinit()とdestroy()方法
    この方法はみんながよく知っているはずです.前の文章ではすでにこの二つの方法を使っています.ここで簡単に説明します.
    1)LifeCycleBean.javaにメソッドxml Init()xml Destroyを追加する()
public void xmlDestroy() {
    System.out.println("xml destroy...");
}

public void xmlInit() {
    System.out.println("xml init...");
}
   2)beans.xmlで定義されているLifeCycleBenにinit-methodとdestroy-methodを追加してxmlInit()とxml Destroy()を指します.
    3)テストビーン
    まとめ:同じ1の2つのインターフェースで同じ結果が得られました.
 
3.PostConstructと@Predestryコメント
    1)この二つはどう使いますか?具体的には以下の通りです
//  LifeCycleBean       
@PostConstruct
public void postConstruct() {
    System.out.println("annotation init ...");
}

@PreDestroy
public void preDestroy() {
    System.out.println("annotation destroy...");
}
    2)beans.xml定義は前の通りで、テストコードは前の通りです.結果は下記の通りです.
//      ,   
    どうして空ですか?なぜメインメソッドでビーンをコンテナで取得する方法は、postConstruct()方法とpreDestroy()方法を実行していないのですか?
    この二つは注解類であることが分かります.これまでのビーンの定義は全部xml定義で実現されました.今のSpring容器はまだ注釈の能力を認識していないと説明します.
    拡張:現在の開発プロジェクトでは、一般的には注釈によってビーンを自動的に注入します.もし@Controller@Service@Componentなどの注釈があれば、この前提条件はSpringを識別する能力を持つ必要があります.beans.xmlファイルに以下の内容を追加する必要があります.
    3)上記の注釈を追加した後、もう一回実行します.結果は以下の通りです.
annotation init ...
InitializingBean init...
xml init...

annotation destroy...
DisposableBean destroy...
xml destroy...
 
4.これらのライフサイクルの行動方法の選択
    ビーンの初期化と廃棄方法にはこのようないくつかの方法がありますが、実際の運用ではどうやって選択すればいいですか?Springに関する提案があります.
// init    
It is recommended that you do not use the InitializingBean interface because it unnecessarily couples the code to Spring. Alternatively, use the @PostConstruct annotation or specify a POJO initialization method. In the case of XML-based configuration metadata, you use the init-method attribute to specify the name of the method that has a void no-argument signature. With Java config, you use the initMethod attribute of @Bean
    
// destroy    
It is recommended that you do not use the DisposableBean callback interface because it unnecessarily couples the code to Spring. Alternatively, use the @PreDestroy annotation or specify a generic method that is supported by bean definitions. With XML-based configuration metadata, you use the destroy-method attribute on the . With Java config, you use the destroyMethod attribute of @Bean
    まとめると、プロジェクトの中で@PostConstructと@Predestroyを使うことを提案します.InitializingBenとDispable Beanはコードと強く結合しています.もし非Webプロジェクトであれば、init-methodとdestroy-methodを使ってもいいです.
 
5.ライフサイクル方法の実行手順
    ビーンというライフサイクルの一つの方法があるなら、実行の順序はどうなりますか?
    Springも同様に答えを出しました.
Multiple lifecycle mechanisms configured for the same bean, with different initialization methods, are called as follows:
//          
1.Methods annotated with @PostConstruct
2.afterPropertiesSet() as defined by the InitializingBean callback interface
3.A custom configured init() method

Destroy methods are called in the same order:
//         
1.Methods annotated with @PreDestroy
2.destroy() as defined by the DisposableBean callback interface
3.A custom configured destroy() method
まとめ:
    以上の三つのBeanライフサイクル管理の方式は自由に選択できます.一般的には注釈とSpringインターフェースから一つを選択します.もしSpringと強く結合したくないなら、注釈方式を選択してもいいです.
 
参考:
https://docs.spring.io/spring/docs/4.3.23.RELEASE/spring-framework-reference/htmlsingle/ 
コードアドレス:https://github.com/kldwz/springstudy