Guiceを使用した依存性注入(DI:Dependency Injection)- 【Java】

Guiceを使用した依存性注入(DI:Dependency Injection)- 【Java】

Google が開発したGuice(グイス)を使用した依存性注入(DI:Dependency Injection)のサンプルです。

アノテーションでDIが出来ます。

pom.xml

pom.xml

<?xml version="1.0" encoding="UTF-8"?>

<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</groupId>
  <artifactId>guice-sample</artifactId>
  <version>1.0-SNAPSHOT</version>
  <name>Guice Sample Project</name>
  <description>A simple project demonstrating Google Guice dependency injection.</description>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>17</maven.compiler.source>
    <maven.compiler.target>17</maven.compiler.target>
  </properties>

  <dependencies>
    <!-- Guice core -->
    <dependency>
      <groupId>com.google.inject</groupId>
      <artifactId>guice</artifactId>
      <version>5.1.0</version>
    </dependency>
    <!-- JSR-330 annotations -->
    <dependency>
      <groupId>jakarta.inject</groupId>
      <artifactId>jakarta.inject-api</artifactId>
      <version>2.0.1</version>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <!-- Compiler plugin -->
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.0</version>
      </plugin>

      <!-- Assembly plugin for dependency-included JAR -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>3.6.0</version>
        <configuration>
          <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
          <archive>
            <manifest>
              <mainClass>com.example.App</mainClass>
            </manifest>
          </archive>
        </configuration>
        <executions>
          <execution>
            <id>make-assembly</id>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
          </execution>
        </executions>
      </plugin>

      <!-- Clean plugin -->
      <plugin>
        <artifactId>maven-clean-plugin</artifactId>
        <version>3.1.0</version>
      </plugin>

      <!-- Surefire plugin (tests) -->
      <plugin>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.22.1</version>
      </plugin>
    </plugins>
  </build>

</project>

依存関係の定義(実装クラスの差し替えなど)

AbstractModuleを継承したクラスで依存関係の定義を記述します。

package com.example;


import com.example.Impl.GreetingServiceImpl;
import com.google.inject.AbstractModule;


public class AppModule extends AbstractModule {
    @Override
    protected void configure() {
        // Bind interface to implementation
        bind(GreetingService.class).to(GreetingServiceImpl.class);
        // If you want eager singleton: bind(GreetingService.class).to(GreetingServiceImpl.class).asEagerSingleton();
    }
}

シングルトンにしたい場合は、以下のどちらかにします。

bind(GreetingService.class)
  .to(GreetingServiceImpl.class)
  .asEagerSingleton();
bind(GreetingService.class)
  .to(MockGreetingService.class)
  .in(com.google.inject.Scopes.SINGLETON);
メソッド インスタンス生成のタイミング 主な用途
.in(Scopes.SINGLETON) 初めて Injector.getInstance() などで必要になったとき(=遅延生成 通常のDI。必要になるまで作らない
.asEagerSingleton() Injector が作られた直後(=即時生成 起動時にすぐ初期化したいクラスに使う

@Injectの種類

DIは以下いずれかの@Injectを使います。

アノテーション パッケージ 標準仕様? 推奨度
javax.inject.Inject javax.inject JSR-330 標準 推奨(標準DI)
jakarta.inject.Inject jakarta.inject Jakarta EE版 JSR-330 今後はこちらが主流
com.google.inject.Inject com.google.inject ⚠️ 非推奨(互換目的)

コンストラクタインジェクション

フィールドインジェクションもできますが、コンストラクタインジェクション例です。

package com.example;


import javax.inject.Inject;


public class Printer {
    private final GreetingService greetingService;

    @Inject
    public Printer(GreetingService greetingService) {
        this.greetingService = greetingService;
    }


    public void run(String name) {
        System.out.println(greetingService.greet(name));
    }
}

DIコンテナ起動

アプリ起動時にDIコンテナ起動します。

package com.example;


import com.google.inject.Guice;
import com.google.inject.Injector;


public class App {
    public static void main(String[] args) {
        // Create Guice injector with our module
        Injector injector = Guice.createInjector(new AppModule());
        // Ask injector to provide a Printer instance (with dependencies injected)
        Printer printer = injector.getInstance(Printer.class);


        // Run the sample
        String name = args.length > 0 ? args[0] : "World";
        printer.run(name);
    }
}

パッケージング&実行

実行可能jarを生成します。

C:\> mvn clean package

実行します。

C:\> java -jar target/guice-sample-1.0-SNAPSHOT-jar-with-dependencies.jar takahashi
Hello, takahashi! (from Guice)

株式会社CONFRAGE ITソリューション事業部をもっと見る

今すぐ購読し、続きを読んで、すべてのアーカイブにアクセスしましょう。

続きを読む

タイトルとURLをコピーしました