Why recommend everyone to learn bytecode?

1. 背景

本記事では、シンプルなケースを通じて Bean ライフサイクルにおける初期化と破棄の各フェーズの具体的な内容を学びます。

2. 分析

まず推測し、その後検証します。インスタンス化、プロパティの設定、初期化、破棄です。具体的には、インターフェースを実装した初期化メソッドが先に実行されるのか、カスタム初期化メソッドが先に実行されるのかは、コードに依存します。

ブレークデバッグ手法を使用して分析と学習を行い、各メソッドにブレークポイントを設定してデバッグします。
【1】コンストラクタの実行
【2】setValue メソッドの実行
【3】InitializingBean#afterPropertiesSet メソッドの実行
【4】init-method の実行
コアメソッド AbstractApplicationContext#refresh
@Override
public void refresh() throws BeansException, IllegalStateException {
synchronized (this.startupShutdownMonitor) {
// Prepare this context for refreshing.
// 1. 初期化前の準備
prepareRefresh();

// Tell the subclass to refresh the internal bean factory.
// 2. BeanFactory の取得、すべての Bean 定義情報のロード(インスタンス化は未実行)
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

// Prepare the bean factory for use in this context.
// 3. BeanFactory の前処理設定
prepareBeanFactory(beanFactory);

try {
// Allows post-processing of the bean factory in context subclasses.
// 4. BeanFactory 完了後の後処理の準備
postProcessBeanFactory(beanFactory);

// Invoke factory processors registered as beans in the context.
// 5. BeanFactory 作成後のポストプロセッサの実行
invokeBeanFactoryPostProcessors(beanFactory);
【1】インスタンス化、【2】プロパティの設定、【3】afterPropertiesSet、【4】カスタム init メソッドはすべてステップ 11 に含まれます。

コアメソッド:
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#doCreateBean

【5】DisposableBean#destroy 破棄メソッドの実行

3. まとめ
本記事では主に推測と検証の手法、およびコードデバッグの手法を使用して、Bean ライフサイクルの初期化と破棄について学びました。

Related Articles

Explore More Special Offers

  1. Short Message Service(SMS) & Mail Service

    50,000 email package starts as low as USD 1.99, 120 short messages start at only USD 1.00

phone お問い合わせ
Hi, I'm Alibaba Cloud AI Assistant!
I can help with questions and solutions.