文系プログラマによるTIPSブログ

文系プログラマ脳の私が開発現場で学んだ事やプログラミングのTIPSをまとめています。

sastruts:tomcat起動時に実行するクラスを設定する

sastruts、というかseasarで、tomcat起動時に1回だけクラスを実行したい場合ってありますよね。
例えばほとんど変更の無いマスタをメモリにキャッシュさせるとか。

web.xmlサーブレットを書いても動くんですが、seasarの初期処理が終わってないと、クラスがコンポーネントとして登録されません。
そこで、tomcat起動後でseasarの初期処理完了後に1度だけ実行する方法。

まあ簡単です。app.diconの末尾に↓みたいにクラス名とメソッド名を指定するだけです。

<components>
  <include path="convention.dicon"/>
  <include path="aop.dicon"/>
  <include path="j2ee.dicon"/>
  <include path="customizer.dicon"/>
  <component name="actionMessagesThrowsInterceptor" class="org.seasar.struts.interceptor.ActionMessagesThrowsInterceptor"/>
  
<!-- tomcat起動後の初期処理 -->
  <component name="initService" class="treeshop.s2.init.InitService">
    <initMethod name="init">
    </initMethod>
  </component>
</components>

この例では、InitServiceクラスのinitメソッドを呼びます。つまりinit()の中に色々書けばよいです。
複数のクラスを呼びたいのであれば、を複数書けばよいです。