続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に渡せる。





