0
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?

More than 5 years have passed since last update.

SpringBootでDI最速体験

Last updated at Posted at 2019-12-10

所要時間 初心者15分。ベテラン5分。

目的

Springフレームワークを扱う上で、重要技術かつ定番技術であるDIに最速で触れる。

概要

0.作業前提

SpringBootでHello Worldするところまで準備する。
参考) SpringBoot体験

1.注入元クラス(Bean)を作成する

シンプルに文字列「Hello World!! from Bean.」を返すメソッドを実装したクラスを用意したらOK。

2.コンフィギュレーションクラスを作成する。

アノテーション @Configuration を付与したクラスとする。
「1」で作成したクラスを返すメソッドを実装し、このメソッドにアノテーション @Bean を付与する。

3.Restコントローラーにフィールドインジェクションして注入クラスのメソッドを呼び出す

型を注入クラスとしたローカル変数を追加し、アノテーション @Autowired を付与する。
リクエストマッピングしたメソッドを、注入クラスの文字列を返すメソッドの呼び出し結果を返すように修正する。

手順詳細

所感

DI設定の類いは多岐にわたるため、業務で実践する際はもっとしっかり考えないと話にならないが、シンプルな構成で捉えることで大まかなアーキテクチャが把握できた。
その昔Strutsを学習したときにも思ったことだが、「設定ファイルでやっていること、設定ファイルに書くこと」に注目すれば、フレームワークが何を自動化、機能提供してくれるのか輪郭が見えてくると思う。

0
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
0
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?