Spring Frameworkでは、アプリケーションのコンポーネントを管理するための様々なメカニズムが提供されています。その中でも`@Import`アノテーションは、XML設定ファイルを使用せずにJavaベースの設定でSpringコンテナにコンポーネントを登録する強力な手段の一つです。このアノテーションは、複数の異なる方法で利用でき、アプリケーションの構造や要件に応じて柔軟なコンポーネント管理を可能にします。
`@Import`アノテーションの主な利用方法は以下の3つです。
1. 直接クラスを指定してコンポーネントをインポートする
`@Import`アノテーションは、設定クラス(`@Configuration`が付与されたクラス)に直接適用され、指定されたクラスをSpringコンテナの管理対象として登録します。これは、少数の特定のコンポーネントを明示的に含めたい場合に特に便利です。
1.1. `@Import`アノテーションの基本構造
`@Import`アノテーションは、`Class>`型の配列を`value`として受け取ります。これにより、複数のクラスを一度に指定できます。
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Import {
Class<?>[] value(); // 登録するコンポーネントクラスの配列
}
このアノテーションは、クラス、インターフェース、または列挙型の宣言に使用されます。
1.2. 利用例: 複数のサービスをインポート
ここでは、いくつかのシンプルなサービスコンポーネントを定義し、それらを`@Import`アノテーションでSpringコンテナに登録する例を示します。
まず、登録対象となるシンプルなPOJOクラスを定義します。
// com.example.app.service.ReportingService
package com.example.app.service;
public class ReportingService {
public void generateReport() {
System.out.println("レポートを生成しました。");
}
}
// com.example.app.service.DataProcessor
package com.example.app.service;
public class DataProcessor {
public void processData() {
System.out.println("データを処理しました。");
}
}
// com.example.app.service.NotificationSender
package com.example.app.service;
public class NotificationSender {
public void sendNotification() {
System.out.println("通知を送信しました。");
}
}
次に、これらのサービスをインポートするためのSpring設定クラスを定義します。
package com.example.app.config;
import com.example.app.service.ReportingService;
import com.example.app.service.DataProcessor;
import com.example.app.service.NotificationSender;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
@Configuration
@Import({ReportingService.class, DataProcessor.class, NotificationSender.class})
public class AppConfiguration {
}
1.3. コンポーネント登録の確認
`AnnotationConfigApplicationContext`を使用して`AppConfiguration`をロードし、登録されたBeanの名前を確認することで、インポートが成功したことを検証できます。
package com.example.app;
import com.example.app.config.AppConfiguration;
import com.example.app.service.ReportingService;
import com.example.app.service.DataProcessor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.junit.jupiter.api.Test; // JUnit 5を使用
import static org.junit.jupiter.api.Assertions.assertNotNull;
public class ApplicationTest {
@Test
void verifyDirectImports() {
ApplicationContext context = new AnnotationConfigApplicationContext(AppConfiguration.class);
System.out.println("--- Springコンテナに登録されたBeanの名前 ---");
for (String beanName : context.getBeanDefinitionNames()) {
System.out.println(beanName);
}
// サービスが取得できるか確認
ReportingService reportService = context.getBean(ReportingService.class);
assertNotNull(reportService);
reportService.generateReport();
DataProcessor dataProc = context.getBean(DataProcessor.class);
assertNotNull(dataProc);
dataProc.processData();
}
}
上記のテストを実行すると、`ReportingService`、`DataProcessor`、`NotificationSender`がSpringコンテナの管理対象として適切に登録されていることを出力で確認できます。
2. `ImportSelector`インターフェースを利用した動的なコンポーネント選択
特定の条件に基づいて、インポートするコンポーネントを動的に選択したい場合、`ImportSelector`インターフェースを実装する方法が非常に有効です。このインターフェースは、`@Configuration`クラスに`@Import`された際に、どのクラスをインポートすべきかを決定します。
2.1. `ImportSelector`の実装
`ImportSelector`インターフェースを実装するカスタムクラスを定義します。`selectImports`メソッドは、インポートすべきクラスの完全修飾名を文字列配列として返します。
package com.example.app.selector;
import org.springframework.context.annotation.ImportSelector;
import org.springframework.core.type.AnnotationMetadata;
public class FeatureToggleSelector implements ImportSelector {
@Override
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
// 例: 特定の環境変数やプロパティに基づいて動的に選択
boolean enableAuditService = Boolean.parseBoolean(System.getProperty("app.audit.enabled", "false"));
if (enableAuditService) {
return new String[]{
"com.example.app.service.AuditService",
"com.example.app.service.LoggingService"
};
} else {
return new String[]{
"com.example.app.service.BasicService"
};
}
}
}
この例では、`app.audit.enabled`システムプロパティに基づいて、`AuditService`と`LoggingService`をインポートするか、あるいは`BasicService`のみをインポートするかを切り替えています。
登録対象となるサービスを定義します。
// com.example.app.service.AuditService
package com.example.app.service;
public class AuditService {
public void recordEvent() {
System.out.println("監査イベントを記録しました。");
}
}
// com.example.app.service.LoggingService
package com.example.app.service;
public class LoggingService {
public void logMessage() {
System.out.println("メッセージをログに記録しました。");
}
}
// com.example.app.service.BasicService
package com.example.app.service;
public class BasicService {
public void performBasicTask() {
System.out.println("基本的なタスクを実行しました。");
}
}
2.2. 設定クラスでの利用
`@Configuration`クラスで、この`FeatureToggleSelector`を`@Import`します。
package com.example.app.config;
import com.example.app.selector.FeatureToggleSelector;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
@Configuration
@Import(FeatureToggleSelector.class)
public class DynamicServiceConfiguration {
}
2.3. コンポーネント登録の確認
システムプロパティを設定して、異なるコンポーネントがロードされることを確認します。
package com.example.app;
import com.example.app.config.DynamicServiceConfiguration;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
public class DynamicApplicationTest {
@Test
void verifyDynamicImportsEnabled() {
System.setProperty("app.audit.enabled", "true"); // 監査サービスを有効化
ApplicationContext context = new AnnotationConfigApplicationContext(DynamicServiceConfiguration.class);
System.out.println("--- 監査サービス有効時のBean ---");
for (String beanName : context.getBeanDefinitionNames()) {
System.out.println(beanName);
}
assertNotNull(context.getBean("auditService"));
assertNotNull(context.getBean("loggingService"));
assertThrows(org.springframework.beans.factory.NoSuchBeanDefinitionException.class, () -> context.getBean("basicService"));
System.clearProperty("app.audit.enabled");
}
@Test
void verifyDynamicImportsDisabled() {
System.setProperty("app.audit.enabled", "false"); // 監査サービスを無効化
ApplicationContext context = new AnnotationConfigApplicationContext(DynamicServiceConfiguration.class);
System.out.println("--- 監査サービス無効時のBean ---");
for (String beanName : context.getBeanDefinitionNames()) {
System.out.println(beanName);
}
assertThrows(org.springframework.beans.factory.NoSuchBeanDefinitionException.class, () -> context.getBean("auditService"));
assertThrows(org.springframework.beans.factory.NoSuchBeanDefinitionException.class, () -> context.getBean("loggingService"));
assertNotNull(context.getBean("basicService"));
System.clearProperty("app.audit.enabled");
}
}
3. `ImportBeanDefinitionRegistrar`インターフェースによる詳細なBean定義の登録
最も低レベルで強力なコンポーネント登録方法が`ImportBeanDefinitionRegistrar`インターフェースです。このインターフェースを実装することで、開発者は`BeanDefinitionRegistry`に直接アクセスし、プログラム的にBean定義を登録できます。これにより、Beanのスコープ、プロパティ、コンストラクタ引数など、Beanのライフサイクルと構成をきめ細かく制御することが可能になります。
3.1. `ImportBeanDefinitionRegistrar`の実装
`registerBeanDefinitions`メソッド内で、`BeanDefinition`オブジェクトを構築し、`BeanDefinitionRegistry`に登録します。
package com.example.app.registrar;
import com.example.app.service.PaymentGatewayService;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;
public class CustomServiceRegistrar implements ImportBeanDefinitionRegistrar {
@Override
public void registerBeanDefinitions(
AnnotationMetadata importingClassMetadata,
BeanDefinitionRegistry registry) {
// PaymentGatewayServiceのBean定義を作成
RootBeanDefinition paymentGatewayDef = new RootBeanDefinition(PaymentGatewayService.class);
// Beanのスコープをプロトタイプに設定(例)
paymentGatewayDef.setScope(BeanDefinition.SCOPE_PROTOTYPE);
// Beanに特定の初期化メソッドを設定することも可能 (例: paymentGatewayDef.setInitMethodName("initialize");)
// "paymentGateway"という名前でBeanを登録
registry.registerBeanDefinition("paymentGateway", paymentGatewayDef);
// 必要に応じて、別のサービスも登録できます
// RootBeanDefinition anotherServiceDef = new RootBeanDefinition(AnotherService.class);
// registry.registerBeanDefinition("anotherService", anotherServiceDef);
}
}
ここで登録する`PaymentGatewayService`クラスを定義します。
// com.example.app.service.PaymentGatewayService
package com.example.app.service;
public class PaymentGatewayService {
public PaymentGatewayService() {
System.out.println("PaymentGatewayServiceがインスタンス化されました。");
}
public void processPayment() {
System.out.println("支払いを処理しました。");
}
// public void initialize() { System.out.println("PaymentGatewayServiceの初期化..."); } // 初期化メソッドの例
}
3.2. 設定クラスでの利用
`@Configuration`クラスで、この`CustomServiceRegistrar`を`@Import`します。前の例と組み合わせて使用することも可能です。
package com.example.app.config;
import com.example.app.registrar.CustomServiceRegistrar;
import com.example.app.selector.FeatureToggleSelector; // ImportSelectorの例も同時にインポート
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
@Configuration
@Import({FeatureToggleSelector.class, CustomServiceRegistrar.class})
public class AdvancedAppConfiguration {
}
3.3. コンポーネント登録の確認
`CustomServiceRegistrar`によって登録されたBeanが正しく利用できるかを確認します。
package com.example.app;
import com.example.app.config.AdvancedAppConfiguration;
import com.example.app.service.PaymentGatewayService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
public class AdvancedApplicationTest {
@Test
void verifyRegistrarAndSelectorImports() {
System.setProperty("app.audit.enabled", "true"); // ImportSelectorの条件を設定
ApplicationContext context = new AnnotationConfigApplicationContext(AdvancedAppConfiguration.class);
System.out.println("--- RegistrarおよびSelectorによるBean ---");
for (String beanName : context.getBeanDefinitionNames()) {
System.out.println(beanName);
}
// ImportSelectorから登録されたBeanを確認
assertNotNull(context.getBean("auditService"));
assertNotNull(context.getBean("loggingService"));
assertThrows(org.springframework.beans.factory.NoSuchBeanDefinitionException.class, () -> context.getBean("basicService"));
// ImportBeanDefinitionRegistrarから登録されたBeanを確認
PaymentGatewayService gatewayService = context.getBean("paymentGateway", PaymentGatewayService.class);
assertNotNull(gatewayService);
gatewayService.processPayment();
// BeanDefinitionRegistryで設定したスコープの確認 (プロトタイプなのでインスタンスが異なるはず)
PaymentGatewayService anotherGatewayService = context.getBean("paymentGateway", PaymentGatewayService.class);
assertNotSame(gatewayService, anotherGatewayService);
System.clearProperty("app.audit.enabled");
}
}