コンポーネントに必要な値をインジェクトする方式は3つあります。
- コンストラクタ・インジェクション
- セッター・インジェクション
- メソッド・インジェクション
コンストラクタ・インジェクション
<components> <component name="example" class="examples.Example"> <arg>"example"</arg> </component> </components>
public class Example { private String param; public Example(String param){ this.param = param; } }
コンストラクタ・インジェクションでは、上記のようにargタグを使えばコンストラクタに値を渡せます。
<components> <component name="param" class="examples.Param"> <arg>"example"</arg> </component> <component name="example" class="examples.Example"> <arg>param</arg> </component> </components>
このようにして値にコンポーネントも指定できます。
セッター・インジェクション
<components> <component name="example" class="examples.Example"> <property name="param">"example"</arg> </component> </components>
public class Example { private String param; public void setParam(String param){ this.param = param; } }
とすると、setParamで"example"を受け取ることができます。
メソッド・インジェクション
<components> <component name="example" class="examples.Example"> <initMethod name="initialize"> <arg>"example"</arg> </initMethod> </component> </components>
public class Example { private String param; public void initialize(String param){ this.param = param; } }
コンストラクタでもセッターでもないメソッドをコールしてインジェクトしたい場合はメソッド・インジェクションが使えます。
今までの開発経験からいってセッター>メソッド>コントラクタの利用頻度です。ほとんど場合はセッターで済ませてます。
とりあえず、以上。