例えばファイル出力先のディレクトリパスなど、アプリケーションの設定をどう書くべきか。propertiesファイルに書いて読み込めばいいと思ったが、そんな必要もなさそう。
コンテナに登録するコンポーネントは、インターフェースを実装したクラスである必要はないのだから、設定値をdiconに書いてコンポーネントとして登録し、Seasar 2.4から追加されたBindingアノテーションを使ってサービスやアクションのコンポーネントにセットしてしまうのが楽そうです。
<component class="java.lang.String" name="hoge"> <arg>"ほげほげ〜"</arg> </component>
というのをどこかのdicon、例えばconfig.diconに書いてapp.diconでincludeするなどしてStringをコンポーネントとして登録。そして、その設定値を利用するサービスコンポーネントのクラスを書く
public class HogeServiceImpl implements HogeService { @Binding("hoge") public String hoge; public void printHoge () { System.out.println(hoge); } }
IntegerやFileでも良い。いずれも不変オブジェクトなので設定値としてはふさわしい型だと思う。
<component class="java.io.File" name="tmpDir"> <arg>"/tmp"</arg> </component> <component class="java.lang.Integer" name="maxValue"> <arg>100</arg> </component>
public class HogeServiceImpl implements HogeService { @Binding("tmpDir") public File tmpDir; @Binding("maxValue") public Integer maxValue; public void print () { System.out.println(tmpDir.getAbsolutePath()); System.out.println(maxValue); } }
diconを書き換えたときはHOT Deployでも反映されないので、APPサーバを再起動!