1. Maven依存関係の構成
まず、Mavenプロジェクトを作成し、Spring Frameworkの核心機能であるIoC(Inversion of Control)コンテナを利用するために必要なライブラリを設定します。ここでは、spring-contextモジュールを依存関係に追加します。バージョンは5.3.27を使用した構成例を以下に示します。
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.demo</groupId>
<artifactId>spring-ioc-basic</artifactId>
<version>1.0.0</version>
<dependencies>
<!-- Spring Contextモジュール -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.27</version>
</dependency>
<!-- テスト用ライブラリ -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
2. Spring設定ファイル(Bean定義)
Springコンテナが管理するオブジェクト(Bean)は、XMLファイルを通じて定義します。通常、このファイルはクラスパスのルートディレクトリ(src/main/resources)に配置します。設定ファイルのルート要素は<beans>タグであり、その中で管理対象のクラスを<bean>タグを使用して登録します。
以下の設定例では、id属性で一意の識別子を、class属性で完全修飾クラス名を指定しています。Springはこれらの情報を基にリフレクション机制を用いてインスタンス化を行い、内部のマップ構造で管理します。
<?xml version="1.0" encoding="UTF-8"?>
<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">
<!-- ビジネスロジックを持つサービスクラスの定義 -->
<!--
id: コンテナ内で一意となる名前。getBeanメソッドで指定する際に使用。
class: インスタンス化するクラスの完全修飾名。
-->
<bean id="orderService" class="com.example.service.impl.OrderServiceImpl"/>
<!-- 別のIDで同じクラスを登録することも可能 -->
<bean id="userService" class="com.example.service.impl.OrderServiceImpl"/>
<!-- Java標準クラス(Date等)のインスタンス化も可能 -->
<bean id="currentDate" class="java.util.Date"/>
</beans>
3. Javaソースコードの実装
実際に動作させるJavaクラスの構成です。インターフェース、その実装クラス、そしてコンテナを起動するメインクラスの3つで構成されます。
(1) サービスインターフェースの定義
package com.example.service;
public interface OrderService {
void processOrder();
}
(2) 実装クラスの作成
このクラスがSpringコンテナによって管理され、インスタンス化されます。コンストラクタが呼ばれるタイミングを確認するためにシステム出力を含めています。
package com.example.service.impl;
import com.example.service.OrderService;
public class OrderServiceImpl implements OrderService {
public OrderServiceImpl() {
System.out.println("OrderServiceImplのインスタンスが生成されました。");
}
@Override
public void processOrder() {
System.out.println("注文処理ロジックを実行中です。");
}
}
(3) アプリケーションのエントリポイント
ApplicationContextインターフェースの実装クラスであるClassPathXmlApplicationContextを使用して、設定ファイルを読み込み、コンテナを初期化します。その後、getBeanメソッドを用いてオブジェクトを取得し、メソッドを実行します。
import com.example.service.OrderService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ApplicationLauncher {
public static void main(String[] args) {
// 1. 設定ファイルのパスを指定(クラスパスからの相対パス)
String resourcePath = "applicationContext.xml";
// 2. Springコンテナ(IoCコンテナ)の初期化
// この時点で設定ファイルに記載されたBeanのインスタンス化が行われます
ApplicationContext container = new ClassPathXmlApplicationContext(resourcePath);
// 3. コンテナからBeanを取得
// IDを指定して取得する場合
OrderService service = (OrderService) container.getBean("orderService");
// 4. 取得したオブジェクトのメソッドを実行
service.processOrder();
// 別のBean(java.util.Date)の取得例
java.util.Date date = (java.util.Date) container.getBean("currentDate");
System.out.println("現在の時刻: " + date);
}
}