<< Spring2 | Home | Java Persistence with Hibernate >>
PR: 転職    お墓    エコ    通販    結婚相談所    シルバー    質屋    葬式    漫画    エステサロン   

続Spring2 + Struts2

動いた。applicationContext.xmlの書き方が悪かったようだ。aop:scoped-proxyというのを使うらしい。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd">
  <bean id="userAction" class="user.EntryAction"
        destroy-method="terminate" scope="request">
    <aop:scoped-proxy/>
    <property name="userDao">
      <ref local="userDao"/>
    </property>
  </bean>

  <bean id="userDao" class="dao.DummyUserDao"
        init-method="init" destroy-method="terminate" scope="request">
    <aop:scoped-proxy/>
  </bean>
</beans>

みたいにすると、ちゃんとリクエストごとに、init-methodとdestroy-methodを呼んでくれる。これは楽だなぁ。JDBCのConnectionとかHibernateのSessionとかはリクエストスコープBeanにして、スレッドローカルに放り込んでおけば、リクエスト処理の終わりで自動的にクローズしてくれるわけだ。アプリケーションはリソース管理を全くしなくてokだ。ここまでやってくれるとDaoを安心してViewに渡せる。




コメント追加 トラックバック送信
このサイトの掲載内容は私自身の見解であり、必ずしもIBMの立場、戦略、意見を代表するものではありません。
日本アイ・ビー・エム 花井 志生 Since 1997.6.8