Spring-bean-post-processors

提供:Dev Guides
移動先:案内検索

Spring-Beanポストプロセッサ

*BeanPostProcessor* インターフェイスは、独自のインスタンス化ロジック、依存関係解決ロジックなどを提供するために実装できるコールバックメソッドを定義します。 Springコンテナが1つ以上のBeanPostProcessor実装をプラグインして、Beanのインスタンス化、構成、および初期化を完了した後、カスタムロジックを実装することもできます。

BeanPostProcessorが Ordered インターフェースを実装している場合、 order プロパティを設定することにより、複数のBeanPostProcessorインターフェースを設定でき、これらのBeanPostProcessorインターフェースの実行順序を制御できます。

BeanPostProcessorsはBean(またはオブジェクト)インスタンスで動作します。つまり、Spring IoCコンテナーがBeanインスタンスをインスタンス化し、BeanPostProcessorインターフェースが機能します。

*ApplicationContext* は、 *BeanPostProcessor* インターフェースの実装で定義されたBeanを自動的に検出し、これらのBeanをポストプロセッサーとして登録し、Bean作成時にコンテナーによって適切に呼び出されます。

次の例は、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.
*HelloWorld.java* ファイルの内容は次のとおりです-
package com.finddevguides;

public class HelloWorld {
   private String message;

   public void setMessage(String message){
      this.message  = message;
   }
   public void getMessage(){
      System.out.println("Your Message : " + message);
   }
   public void init(){
      System.out.println("Bean is going through init.");
   }
   public void destroy(){
      System.out.println("Bean will destroy now.");
   }
}

これは、Beanの初期化の前後にBean名を出力するBeanPostProcessorを実装する非常に基本的な例です。 両方のポストプロセッサメソッド内のBeanオブジェクトにアクセスできるため、Beanを初期化する前後に、より複雑なロジックを実装できます。

*InitHelloWorld.java* ファイルの内容は次のとおりです-
package com.finddevguides;

import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.BeansException;

public class InitHelloWorld implements BeanPostProcessor {
   public Object postProcessBeforeInitialization(Object bean, String beanName)
      throws BeansException {

      System.out.println("BeforeInitialization : " + beanName);
      return bean; //you can return any other object as well
   }
   public Object postProcessAfterInitialization(Object bean, String beanName)
      throws BeansException {

      System.out.println("AfterInitialization : " + beanName);
      return bean; //you can return any other object as well
   }
}

以下は MainApp.java ファイルの内容です。 ここでは、AbstractApplicationContextクラスで宣言されているシャットダウンフック* registerShutdownHook()*メソッドを登録する必要があります。 これにより、正常なシャットダウンが保証され、関連する破棄メソッドが呼び出されます。

package com.finddevguides;

import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainApp {
   public static void main(String[] args) {
      AbstractApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");

      HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
      obj.getMessage();
      context.registerShutdownHook();
   }
}

以下は、initおよびdestroyメソッドに必要な構成ファイル Beans.xml です-

<?xml version = "1.0" encoding = "UTF-8"?>

<beans xmlns = "http://www.springframework.org/schema/beans"
   xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation = "http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

   <bean id = "helloWorld" class = "com.finddevguides.HelloWorld"
      init-method = "init" destroy-method = "destroy">
      <property name = "message" value = "Hello World!"/>
   </bean>

   <bean class = "com.finddevguides.InitHelloWorld"/>

</beans>

ソースおよびBean構成ファイルの作成が完了したら、アプリケーションを実行します。 すべてがあなたのアプリケーションでうまくいけば、それは次のメッセージを印刷します-

BeforeInitialization : helloWorld
Bean is going through init.
AfterInitialization : helloWorld
Your Message : Hello World!
Bean will destroy now.