liguofeng29’s blog

個人勉強用ブログだっす。

Spring4 - BeanPostProcessor

BeanPostprocessorはBeanの生成後の後処理をサポートする。
BeanPostProcessorはbeanの初期化処理前後に処理を入れることができ、beanに対して共通処理を行える

BeanPostProcessorのIF

  • postProcessBeforeInitialization
  • postProcessAfterInitialization

サンプル

package app.postBeanProcessor;

import org.springframework.beans.factory.InitializingBean;


public class Japanese implements InitializingBean{

    private int age; // 年齢
    private String name; // 名前

    public void setAge(int age) {
        this.age = age;
    }
    public void setName(String name) {
        this.name = name;
    }

    public void info() {
        System.out.println("名前 : " + this.name);
        System.out.println("年齢 : " + this.age);
    }

    public void init() {
        System.out.println("init : 初期化処理");
    }
    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("afterPropertiesSet : 初期化処理");

    }
}
package app.postBeanProcessor;

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

public class MyBeanPostProcessor implements BeanPostProcessor {
    /**
    * コンテナーのBeanに対して後処理を行う
    *
    * @param bean
    *            後処理beanのインスタンス
    * @param beanName
    *            後処理beanのid
    * @return 後処理後のbean
    */
    public Object postProcessBeforeInitialization(Object bean, String beanName) {
        System.out
                .println("postProcessBeforeInitialization : Beanの初期化前に実行されます。");

        return bean;
    }

    public Object postProcessAfterInitialization(Object bean, String beanName) {
        System.out
                .println("postProcessAfterInitialization : Beanの初期化後に実行されます。");

        if (bean instanceof Japanese) {
            System.out.println("後処理でbean.nameを変える");
            Japanese j = (Japanese) bean;
            j.setName("山本");
        }
        return bean;
    }
}
<?xml version="1.0" encoding="GBK"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">

    <!-- Bean定義 -->
    <bean id="japanese" class="app.postBeanProcessor.Japanese"
        init-method="init"  p:name="佐藤" p:age="20">
    </bean>

    <!-- Bean後処理プロセッサー登録 -->
    <bean class="app.postBeanProcessor.MyBeanPostProcessor" />
</beans>
package app.postBeanProcessor;

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

public class TestPostBeanProcessor {

    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("postBeanProcessor.xml");

        // Bean取得
        Japanese p = context.getBean("japanese", Japanese.class);
        p.info();
    }
}
postProcessBeforeInitialization : Beanの初期化前に実行されます。
afterPropertiesSet : 初期化処理
init : 初期化処理
postProcessAfterInitialization : Beanの初期化後に実行されます。
後処理でbean.nameを変える
名前 : 山本
年齢 : 20

name属性は佐藤ではなく山本になる。

※ApplicationContextではなくBeanFactoryを使う際には手動で後処理プロセッサーを登録する必要がある

手動登録サンプル

Resource isr = new ClassPathResource("beans.xml");
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();

new XmlBeanDefinitionReader(beanFactory).loadBeanDefinitions(isr);
BeanPostProcessor bp = (BeanPostProcessor)beanFactory.getBean("bp");
beanFactory.addBeanPostProcessor(bp);