Spring IoCコンテナの構築と実装

Springコンテナ実装の基本フロー

  1. 設定ファイルの準備
    • XML形式の設定ファイル(通常はapplicationContext.xml)を作成し、Springコンテナの構成を定義します
    • コンテナに管理させたいBeanとそれらの依存関係を宣言します
  2. 依存関係の追加
    • プロジェクトのビルド設定にSpring Framework関連のライブラリを追加します
  3. コンポーネントクラスの作成
    • Spring管理対象となるクラス(Bean)を実装します
    • 通常のPOJOクラスまたはアノテーション付きクラスとして作成可能です
  4. Beanの設定
    • 設定ファイルで各Beanの属性を構成します
    • クラス名、コンストラクタ引数、プロパティ値などを指定します
  5. コンテナの初期化
    • ApplicationContextインターフェースの実装クラスを通じてコンテナを起動します
    • 設定ファイルの場所に応じてClassPathXmlApplicationContextまたはFileSystemXmlApplicationContextを使用します
  6. Beanの取得
    • コンテナのgetBean()メソッドを使用して必要なBeanインスタンスを取得します
    • BeanのIDまたはタイプを指定して取得できます
  7. Beanの活用
    • 取得したBeanインスタンスをビジネスロジックで使用します
    • 他のコンポーネントにインジェクションしたり、直接メソッドを呼び出したりします
  8. コンテナの終了処理
    • アプリケーション終了時にコンテナを破棄してリソースを解放します
    • close()メソッドを手動で呼び出すか、JVMのシャットダウンフックを使用します

設定ファイルの定義

プロジェクトのリソースディレクトリ(例:src/main/resources)にconfig.xmlという名前で設定ファイルを作成します。

<!-- config.xml -->
<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.xsd">
    
    <!-- Beanの定義 -->
    <!-- サンプル:customerService Beanの定義 -->
    <bean id="customerService" class="com.app.CustomerService">
        <!-- プロパティの設定 -->
        <property name="repository" ref="customerRepository"/>
    </bean>

    <!-- 他のBeanを続けて定義可能 -->
</beans>

依存関係の追加

MavenまたはGradleのビルド設定ファイルにSpring Frameworkの依存関係を追加します。

Mavenの例:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>5.3.21</version>
</dependency>

Gradleの例:

implementation 'org.springframework:spring-context:5.3.21'

コンポーネントクラスの作成

Spring管理対象となるクラスを作成します。ここではCustomerServiceとCustomerRepositoryの例を示します。

// CustomerService.java
package com.app;

public class CustomerService {
    private CustomerRepository repository;

    public void setRepository(CustomerRepository repository) {
        this.repository = repository;
    }

    // ビジネスロジックの実装...
}
// CustomerRepository.java
package com.app;

public class CustomerRepository {
    // リポジトリの実装詳細...
}

Beanの設定

設定ファイル(config.xml)で各Beanの属性を設定します。

<!-- config.xml -->
<beans>
    <!-- customerService Beanの定義 -->
    <bean id="customerService" class="com.app.CustomerService">
        <!-- repositoryプロパティの設定 -->
        <property name="repository" ref="customerRepository"/>
    </bean>

    <!-- customerRepository Beanの定義 -->
    <bean id="customerRepository" class="com.app.CustomerRepository"/>
</beans>

コンテナの初期化

ApplicationContextインターフェースの実装クラスを使用してSpringコンテナを起動します。

// ApplicationRunner.java
package com.app;

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

public class ApplicationRunner {
    public static void main(String[] args) {
        // Springコンテナの初期化
        ApplicationContext container = new ClassPathXmlApplicationContext("config.xml");

        // customerService Beanの取得
        CustomerService service = (CustomerService) container.getBean("customerService");

        // serviceの利用
        // ビジネスロジックの実行...
    }
}

Beanの取得

コンテナのgetBean()メソッドを使用して必要なBeanインスタンスを取得します。

// customerService Beanの取得
CustomerService service = (CustomerService) container.getBean("customerService");

Beanの活用

取得したBeanインスタンスをビジネスロジックの実行に使用します。

// serviceの利用
// ビジネスメソッドの呼び出しや処理の実行...

コンテナの終了処理

アプリケーション終了時にコンテナを適切に終了してリソースを解放します。

// Springコンテナの終了
((ClassPathXmlApplicationContext) container).close();

タグ: Spring IoC DI Java ApplicationContext

7月26日 22:22 投稿