Spring-bean-post-processors
Spring-Beanポストプロセッサ
BeanPostProcessorが Ordered インターフェースを実装している場合、 order プロパティを設定することにより、複数のBeanPostProcessorインターフェースを設定でき、これらのBeanPostProcessorインターフェースの実行順序を制御できます。
BeanPostProcessorsはBean(またはオブジェクト)インスタンスで動作します。つまり、Spring IoCコンテナーがBeanインスタンスをインスタンス化し、BeanPostProcessorインターフェースが機能します。
例
次の例は、ApplicationContextのコンテキストでBeanPostProcessorsを作成、登録、および使用する方法を示しています。
動作するEclipse IDEを用意し、次の手順を実行してSpringアプリケーションを作成します。
Steps | Description |
---|---|
1 | Create a project with a name SpringExample and create a package com.finddevguides under the *src *folder in the created project. |
2 | Add required Spring libraries using Add External JARs option as explained in the Spring Hello World Example chapter. |
3 | Create Java classes HelloWorld, InitHelloWorld and MainApp under the com.finddevguides package. |
4 | Create Beans configuration file Beans.xml under the* src* folder. |
5 | The final step is to create the content of all the Java files and Bean Configuration file and run the application as explained below. |
これは、Beanの初期化の前後にBean名を出力するBeanPostProcessorを実装する非常に基本的な例です。 両方のポストプロセッサメソッド内のBeanオブジェクトにアクセスできるため、Beanを初期化する前後に、より複雑なロジックを実装できます。
以下は MainApp.java ファイルの内容です。 ここでは、AbstractApplicationContextクラスで宣言されているシャットダウンフック* registerShutdownHook()*メソッドを登録する必要があります。 これにより、正常なシャットダウンが保証され、関連する破棄メソッドが呼び出されます。
以下は、initおよびdestroyメソッドに必要な構成ファイル Beans.xml です-
ソースおよびBean構成ファイルの作成が完了したら、アプリケーションを実行します。 すべてがあなたのアプリケーションでうまくいけば、それは次のメッセージを印刷します-