2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

導入

今回は、Spring Bootの基本技術であるDI(依存性注入) について、初めて学ぶ方でもわかりやすく解説していきます。
DIを使うと、コードの保守がしやすくなったり、テストが楽になったりと、色々良いことがあるんです。

依存性注入(DI)ってなに?

1. 依存性って...?

まず、「依存性」ってなんだろう?ってところから。
これは、クラス(例えばAクラス)が、他のクラス(例えばBクラス)の機能を利用するために必要なオブジェクトのことを言います。
簡単に言うと、Aが動くためにはBが必要ってことですね。

2. DIってどんなもの?

依存性注入(Dependency Injection, DI) は、上述のAとBのように依存するオブジェクトを、クラスの中で直接作らずに、外から注入する(つまり、渡してあげる)仕組みです。
これにより、オブジェクトは自身で依存オブジェクトを生成するのではなく、外部から提供されるため、柔軟でテストしやすいコードを書くことができます。

3. DIを使うメリット(重要)

DIには、以下の3つのメリットがあります。
このメリットこそがDIを使う意義といっても過言ではありません。

  • 疎結合: クラスが他のクラスに直接依存しないため、変更に強くなります。
  • テストの容易さ: 依存オブジェクトを簡単にモックに置き換えられるため、単体テストが容易になります。
  • 再利用性: クラスの再利用が容易になります。別の依存関係を持つ別のコンテキストで簡単に使用できます。

Spring BootでDIを使ってみよう!

1. Spring Beanって何?

Springでは、オブジェクトを管理するためにBeanという仕組みを使います。
Springがアプリ全体のBeanを管理してくれるので、開発者はBeanの作成や管理に頭を悩ませる必要がありません。

2. Beanをどうやって作るの?

Spring Bootでは、主に2つの方法でBeanを作ります。

2.1. アノテーションを使う方法

一番よく使うのが、@Component@Serviceなどのアノテーションを使う方法です。
これをクラスにつけると、そのクラスがBeanとして登録されます。

SampleService.java
import org.springframework.stereotype.Service;

@Service
public class SampleService {
    public void sayHello() {
        System.out.println("Hello World!");
    }
}

2.2. @Configurationを使う方法

こちらは、Javaの設定ファイル(Configuration)でBeanを定義するやり方です。

AppConfig.java
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AppConfig {
    @Bean
    public SampleService sampleService() {
        return new SampleService();
    }
}

3. 依存性をどうやって注入するの?

依存性を注入する方法も、主に2つあります。

3.1. コンストラクタ注入

コンストラクタの引数に必要なオブジェクトを受け取るようにして、@Autowiredを付けてあげます。

UserService.java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService {
    private final SampleService sampleService;

    @Autowired
    public UserService(SampleService sampleService) {
        this.sampleService = sampleService;
    }
    
    public void executeServiceSay() {
        sampleService.sayHello();
    }
}

3.2. フィールド注入

こちらはフィールドに直接@Autowiredを付けて注入する方法です。
コンストラクタ注入より、一般的ではありますが、テストが難しくなる可能性があります。

UserService.java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService {
    @Autowired
    private SampleService sampleService;
    
    public void executeServiceSay() {
        sampleService.sayHello();
    }
}

最後に

DIを使うと、クラスの依存関係を外から注入できるので、コードがシンプルになって変更にも強くなります。
Spring Bootでは、DIを使ってBeanを簡単に管理できるので、ぜひ活用してみてください!

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?