5
6

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.

Spring Bootの依存性注入(DI)設定方法

Last updated at Posted at 2018-04-22

Spring Frameworkの依存性注入(Dependency Injection)を設定する方法をまとめました。

コンポーネントのスキャン対象指定

デフォルトでは @ComponentScan が付与されたクラスのパッケージと同階層以下のパッケージをスキャンする。その他のクラスをスキャン対象にしたい場合はscanBasePackageにパッケージ名を指定する。

// @SpringBootApplicationなどの複合アノテーションに指定することもできる。
@ComponentScan(scanBasePackages={"com.example"})

依存性注入(DI)設定

Springアノテーションを使う場合

依存するオブジェクトを private final なメンバとして定義して、コンストラクタで依存オブジェクトを引数で受け取るコンストラクタを定義する。Lombokを使う場合は下記のように記載できる。

コンポーネント定義
@Component
class ComponentA { ...
DI設定
@RequiredArgsConstructor
class ClassA {
	// @Autowiredは省略できる。
	private final ComponentA componentA;
	...

以前はfinalでないメンバもしくはそのSetterに@Autowiredを指定するのが主流であった。

class ClassA {
	@Autowired
    private ComponentA componentA;
	...
class ClassA {
	private ComponentA componentA;
	@Autowired
	public void setComponentA(ComponentA componenA) {
		...

コンストラクタを利用することには以下のメリットがあるため、現在はこちらが推奨されている。

  • DIなしでも利用可能なモジュールになる。(利用側でコンストラクタで依存関係を設定すればよい。)
  • final 修飾子を付けられる。(DIオブジェクトを格納する変数を書き換えることは通常ない。)

JSR-330アノテーションを使う場合

Java標準なので他のDIコンテナに乗り換えやすい。

コンポーネント定義
@Named // もしくは@ManagedBean
class ComponentA { ... }
DI設定

コンストラクタ(もしくはSetter)に@Injectを指定する。(Setterでも可。)

@RequiredArgsConstructor(onConstructor=@__(@Inject))
class ClassA {
	private final ComponentA componentA;
	...

もしくは、メンバに@Injectを指定する。

class ClassA {
	@Inject
	private ComponentA componentA;
    ...

参考

5
6
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
5
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?