<< 2006/11/11 | Home | 2006/11/13 >>
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に渡せる。

Spring2

リクエスト、セッションスコープbeanが良さげなので、Struts2にSpring2を組み込んでみる。フィルタ登録して、

  <filter> 
    <filter-name>requestContextFilter</filter-name> 
    <filter-class>org.springframework.web.filter.RequestContextFilter</filter-class>
  </filter> 

  <filter-mapping> 
    <filter-name>requestContextFilter</filter-name> 
    <url-pattern>/*</url-pattern>
  </filter-mapping>

applicationContext.xmlに追加。

  <bean id="userDao" class="dao.DummyUserDao"
        init-method="init" destroy-method="terminate" scope="request">
  </bean>

なんかエラーになるなぁ。なんでスレッドバウンドリクエストが見つからないんだろうか。

     [java] 致命的: Context initialization failed
     [java] org.springframework.beans.factory.BeanCreationException: Error creat
ing bean with name 'userAction' defined in ServletContext resource [/WEB-INF/app
licationContext.xml]: Cannot resolve reference to bean 'userDao' while setting b
ean property 'userDao'; nested exception is org.springframework.beans.factory.Be
anCreationException: Error creating bean with name 'userDao': Scope 'request' is
 not active; nested exception is java.lang.IllegalStateException: No thread-boun
d request: use RequestContextFilter
     [java] Caused by: org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'userDao': Scope 'request' is not active; nested e
xception is java.lang.IllegalStateException: No thread-bound request: use Reques
tContextFilter

これがokなら、もうStruts2組み込みのDIはいらないので、Springに統一できそうなんだけど。

猫ページ

猫ページ更新完了。

Operaの言語設定

Accept-Languaeを変えようとして、Operaのツール=>設定=>言語をEnglishに変えたら、ツールバーとかまで英語になってしまった。ま、それはいいんだけど、その後日本語に戻しても、ツールバーは英語のまま。ん~、バグかな。

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