Bean Lifcycle の確認2

前回の実装に加えて、ライフサイクル内でのカスタムの処理を確認します。

今回の検証内容のリポジトリ

メインメソッドはBean定義を行い、 Bean1 をDIコンテナから取得(ルックアップ)して使用した後に、DIコンテナを消去。 前回の検証と同様のものです。

public static void main(String[] args) {
    var context = new AnnotationConfigApplicationContext(AppConfig.class);

    Bean1 springBean1 = context.getBean(Bean1.class);
    springBean1.sayHello();

    context.close();
}

今回は、 Bean1 に関しては、コンフィギュレーションクラスでBeanを登録します。 その際に、 initMethod, destroyMehod を指定。

@Configuration
@ComponentScan
public class AppConfig {

    @Bean(initMethod = "initialization", destroyMethod = "destruction")
    public Bean1 springBean1() {
        return new Bean1();
    }
}

Bean1 は InitializingBean, DisposableBean を実装。 Bean定義にて指定した、 initialization,destruction メソッドを定義。

public class Bean1 implements InitializingBean, DisposableBean {

    private Bean2 Bean2;

    public Bean1() {
        System.out.println("\nCreating " + getClass().getSimpleName());
    }

    public void sayHello() {
        System.out.println("\nHello\n");
    }

    @Autowired
    public void setBean2(Bean2 bean2) {
        System.out.println("settingProperty on " + getClass().getSimpleName() + " to inject " + bean2.getClass().getSimpleName());
        this.Bean2 = bean2;
    }

    @PostConstruct
    public void postConstruct() {
        System.out.println("@PostConstruct " + getClass().getSimpleName());
    }

    @PreDestroy
    public void preDestroy() {
        System.out.println("@PreDestroy " + getClass().getSimpleName());
    }

    // InitializingBean のメソッド
    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("InitializingBean::afterPropertiesSet " + getClass().getSimpleName());
    }

    // DisposableBean のメソッド
    @Override
    public void destroy() throws Exception {
        System.out.println("DisposableBean::destroy " + getClass().getSimpleName());
    }

    private void initialization() {
        System.out.println("@Bean(initMethod) " + getClass().getSimpleName());
    }

    private void destruction() {
        System.out.println("@Bean(destroyMethod) " + getClass().getSimpleName());
    }
}

メインメソッドを実行すると、Bean1 が依存している、 Bean2 のインスタンスが生成された後に、 Bean1 に関して、

  • コンストラク
  • セッターインジェクション
  • BeanPostProcessor の前処理(postProcessBeforeInitialization)
  • @PostConstruct
  • InitializingBean の afterPropertiesSet
  • Bean定義の initMethod
  • BeanPostProcessor の後処理(postProcessAfterInitialization)

が行われ、 sayHello() を実行した後に、DIコンテナを close するため、

  • @PreDestroy
  • DisposableBean の destroy
  • Bean定義の destroyMethod

が呼ばれます。

Creating Bean2
CustomBeanPostProcessor::postProcessBeforeInitialization Bean2 bean2
CustomBeanPostProcessor::postProcessAfterInitialization Bean2 bean2

Creating Bean1
settingProperty on Bean1 to inject Bean2
CustomBeanPostProcessor::postProcessBeforeInitialization Bean1 springBean1
@PostConstruct Bean1
InitializingBean::afterPropertiesSet Bean1
@Bean(initMethod) Bean1
CustomBeanPostProcessor::postProcessAfterInitialization Bean1 springBean1

Hello

@PreDestroy Bean1
DisposableBean::destroy Bean1
@Bean(destroyMethod) Bean1