Spring-BeanPostProcessorインタフェース(ポストプロセッサ)説明
4060 ワード
概要:
BeanPostProcessorインタフェースは、多くのSpringが開発者に提供するbeanライフサイクル内のカスタムロジック拡張インタフェースの1つであり、その他にもInitializingBean、DisposableBean、BeanFactoryAwareなどがある.
BeanPostProcessorインタフェースを実現したBeanはポストプロセッサと呼ばれています.
BeanPostProcessorインタフェースの定義は次のとおりです.
:
プロジェクト(Springコンテナが1つしかないと仮定)では、BeanPostProcessorインタフェースを実装するBean(ポストプロセッサ)を定義すると、このBeanが存在するコンテナ内の他のすべてのBeanが初期化前にポストプロセッサのpostProcessBeforeInitializationメソッドを実行します.初期化後もポストプロセッサのpostProcessAfterInitializationメソッドが実行されます.実行順序は次のとおりです.
1. BeanPostProcessor.postProcessBeforeInitialization
2. InitializingBean.afterPropertiesSet
3.bean構成におけるinit-method
4. BeanPostProcessor.postProcessAfterInitialization
beanライフサイクルについては、ここを参照してください.
1つのコンテナに複数のポストプロセッサを定義することができ、そのコンテナ内の他のBean初期化の前後にこれらのBeanのpostProcessBeforeInitializationメソッドとpostProcessAfterInitializationメソッドが順次実行される.
ポストプロセッサの構成は通常のSpring Beanと同じです.
次にSpringの例を見てみましょう.
クラス定義:
Spring構成:
構成中のmessengerもbeanであり、具体的には公式資料を参照することができます.
Chapter 28, Dynamic language support
あ、ここは詳しくは言いません.
mainメソッドを書いてSpringコンテナを起動します.
実行後、印刷結果は次のとおりです.
構成では、BeanPostProcessorインタフェースを実装したBean(ポストプロセッサ)がSpringコンテナによって優先的にインスタンス化され、他のBeanを後でインスタンス化するときに機能します.
BeanPostProcessorインタフェースは、多くのSpringが開発者に提供するbeanライフサイクル内のカスタムロジック拡張インタフェースの1つであり、その他にもInitializingBean、DisposableBean、BeanFactoryAwareなどがある.
BeanPostProcessorインタフェースを実現したBeanはポストプロセッサと呼ばれています.
BeanPostProcessorインタフェースの定義は次のとおりです.
:
public interface BeanPostProcessor {
Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException;
Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException;
}
プロジェクト(Springコンテナが1つしかないと仮定)では、BeanPostProcessorインタフェースを実装するBean(ポストプロセッサ)を定義すると、このBeanが存在するコンテナ内の他のすべてのBeanが初期化前にポストプロセッサのpostProcessBeforeInitializationメソッドを実行します.初期化後もポストプロセッサのpostProcessAfterInitializationメソッドが実行されます.実行順序は次のとおりです.
1. BeanPostProcessor.postProcessBeforeInitialization
2. InitializingBean.afterPropertiesSet
3.bean構成におけるinit-method
4. BeanPostProcessor.postProcessAfterInitialization
beanライフサイクルについては、ここを参照してください.
1つのコンテナに複数のポストプロセッサを定義することができ、そのコンテナ内の他のBean初期化の前後にこれらのBeanのpostProcessBeforeInitializationメソッドとpostProcessAfterInitializationメソッドが順次実行される.
ポストプロセッサの構成は通常のSpring Beanと同じです.
次にSpringの例を見てみましょう.
クラス定義:
package scripting;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.BeansException;
public class InstantiationTracingBeanPostProcessor implements BeanPostProcessor {
// simply return the instantiated bean as-is
public Object postProcessBeforeInitialization(Object bean, String beanName)
throws BeansException {
return bean; // we could potentially return any object reference here...
}
public Object postProcessAfterInitialization(Object bean, String beanName)
throws BeansException {
System.out.println("Bean '" + beanName + "' created : " + bean.toString());
return bean;
}
}
Spring構成:
構成中のmessengerもbeanであり、具体的には公式資料を参照することができます.
Chapter 28, Dynamic language support
あ、ここは詳しくは言いません.
mainメソッドを書いてSpringコンテナを起動します.
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.scripting.Messenger;
public final class Boot {
public static void main(final String[] args) throws Exception {
ApplicationContext ctx = new ClassPathXmlApplicationContext("scripting/beans.xml");
Messenger messenger = (Messenger) ctx.getBean("messenger");
System.out.println(messenger);
}
}
実行後、印刷結果は次のとおりです.
Bean 'messenger' created : org.springframework.scripting.groovy.GroovyMessenger@272961
org.springframework.scripting.groovy.GroovyMessenger@272961
構成では、BeanPostProcessorインタフェースを実装したBean(ポストプロセッサ)がSpringコンテナによって優先的にインスタンス化され、他のBeanを後でインスタンス化するときに機能します.