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?

SpringでのjobId・Bean・DIの関係性まとめ

Posted at

Spring開発において、DI(Dependency Injection)とBean管理、さらにジョブ識別用のjobIdを組み合わせる場面があります。
システムが複数ある場合や、同一システム内で設定を変えたい場合、DIの挙動を理解しておくことが重要です。

  1. 基本概念
    | 用語 | 説明 |
    | ------------- | --------------------------------------------------- |
    | Bean | Springコンテナで管理されるオブジェクト。@Component@Serviceなどで定義 |
    | DI(依存性注入) | Bean同士の依存関係を自動で注入する仕組み |
    | jobId | ジョブを識別する動的値。Bean名と対応させることも可能 |

  2. 複数システム・複数コンテキストの場合
    システムA・システムBが存在
    各システムで独自のparmファイルやBean定義を持つ
    jobIdは同じ(例:jobX)でも、DIされるBeanはコンテキスト単位で異なる
    イメージ
    [システムAコンテキスト]
    Bean名: jobX -> JobAService
    parmファイル: parmA.properties
    処理: A向け処理

[システムBコンテキスト]
Bean名: jobX -> JobBService
parmファイル: parmB.properties
処理: B向け処理

  1. 同一システム内でjobIdは同じでもBean内容を変えたい場合
    同じシステムで処理は同じでも、Beanに設定する値や振る舞いを変えたい場合
    シングルトンBeanでは設定値を共有してしまうため
    方法 複数Bean定義 + Bean名で切り分け

  2. 総合ポイントまとめ
    | ケース | DI/Beanの挙動 | 対応策 |
    | ---------------------- | -------------------- | --------------------------------- |
    | 複数システム・jobId同じ | コンテキストごとにDIされるBeanは別 | jobId=Bean名で管理すれば安全 |
    | 同一システム・同じjobIdでも設定変えたい | シングルトンBeanだと設定が共有される | 1. 複数Bean定義 2. Prototypeスコープで動的生成 |

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?