2011年3月21日月曜日

Struts2 + Spring + MyBatis (その2)

Struts2 + Spring + MyBatis (その1)の続きです。

WEBアプリケーション開発で使用するフレームワークをStruts2.2 + Spring2.5 + MyBatis2.3 で構成してみます。
(その2)では、各xmlファイルとpropertiesファイルを揃えます。

必要な設定ファイルを記載しておきます。
(省略)と書かれているファイルの内容については、一例を記述する程度に留まるので説明しません。詳細について知りたい場合は、解説している他サイトを参照することをお勧めします。
設定ファイル
web.xmlServletの配備記述子
struts.xmlStruts2の設定ファイル(省略)
struts.propertiesStruts2のパラメータファイル(省略)
applicationContext.xmlSpringの設定ファイル
detabase.propertiesデータベースのパラメータファイル(省略)
sqlMapConfig.xmlMyBatisの設定ファイル(省略)
tiles.xmlTilesの設定ファイル(省略)
web.xml
「WEB-INF」フォルダ内にある配備記述子です。これが無いと始まりません。
どのプロジェクトでも共通した記述なので、ほぼそのままコピーすれば使えます。
Servlet2.5の構文で書いてみます。
  1. <!--xml version="1.0" encoding="utf-8"?-->  
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">  
  3.   
  4.  <context-param>  
  5.   <param-name>contextConfigLocation</param-name>  
  6.   <param-value>classpath:applicationContext*.xml</param-value>  
  7.  </context-param>  
  8.   
  9.  <filter>  
  10.   <filter-name>struts2</filter-name>  
  11.   <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  
  12.   <init-param>  
  13.    <param-name>actionPackages</param-name>  
  14.    <param-value>sample.action</param-value>  
  15.   </init-param>  
  16.  </filter>  
  17.   
  18.  <filter-mapping>  
  19.   <filter-name>struts2</filter-name>  
  20.   <url-pattern>/*</url-pattern>  
  21.  </filter-mapping>  
  22.   
  23.  <servlet>  
  24.   <servlet-name>JspSupportServlet</servlet-name>  
  25.   <servlet-class>org.apache.struts2.views.JspSupportServlet</servlet-class>  
  26.   <load-on-startup>1</load-on-startup>  
  27.  </servlet>  
  28.   
  29.  <servlet-mapping>  
  30.   <servlet-name>JspSupportServlet</servlet-name>  
  31.   <url-pattern>/*</url-pattern>  
  32.  </servlet-mapping>  
  33.   
  34.  <listener>  
  35.   <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  36.  </listener>  
  37.  <listener>  
  38.   <listener-class>org.apache.struts2.tiles.StrutsTilesListener</listener-class>  
  39.  </listener>  
  40.   
  41.  <session-config>  
  42.   <session-timeout>30</session-timeout>  
  43.  </session-config>  
  44.   
  45.  <welcome-file-list>  
  46.   <welcome-file>index.html</welcome-file>  
  47.   <welcome-file>index.jsp</welcome-file>  
  48.  </welcome-file-list>  
  49.   
  50.  <jsp-config>  
  51.   <taglib>  
  52.    <taglib-uri>http://tiles.apache.org/tags-tiles</taglib-uri>  
  53.    <taglib-location>/WEB-INF/tld/tiles-jsp.tld</taglib-location>  
  54.   </taglib>  
  55.  </jsp-config>  
  56.   
  57. </web-app>  
Struts2とSpringを使用するには、上記のように記述します。
画面(View)の実装では、JspではなくFreeMarkerを使用します。 FreeMarker内でJspのtaglib(tiles等)を使用するためには、「JspSupportServlet」を動作させておく必要があります。
applicationContext.xml
applicationContex.xmlはweb.xmlに記述した「classpath:applicationContext*.xml」で参照される場所に配置します。
以下の内容は記述例です。
  1. <!--xml version="1.0" encoding="UTF-8" ?-->  
  2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemalocation="  
  3.    http://www.springframework.org/schema/beans  
  4.    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  5.    http://www.springframework.org/schema/context  
  6.    http://www.springframework.org/schema/context/spring-context-2.5.xsd">  
  7.   
  8.  <context:annotation-config></context:annotation-config>  
  9.   
  10.  <context:component-scan base-package="sample.service.impl,sample.dao.impl">  
  11.  </context:component-scan>  
  12.   
  13.  <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
  14.   <property name="locations">  
  15.    <list>  
  16.     <value>classpath:database.properties</value>  
  17.    </list>  
  18.   </property>  
  19.  </bean>  
  20.   
  21.  <!-- データソース -->  
  22.  <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">  
  23.   <property name="driverClassName" value="${jdbc.driverClassName}"></property>  
  24.   <property name="url" value="${jdbc.url}"></property>  
  25.   <property name="username" value="${jdbc.username}"></property>  
  26.   <property name="password" value="${jdbc.password}"></property>  
  27.   <property name="defaultAutoCommit" value="true"></property>  
  28.   <property name="removeAbandoned" value="true"></property>  
  29.  </bean>  
  30.   
  31.  <!-- トランザクション -->  
  32.  <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
  33.   <property name="dataSource" ref="dataSource"></property>  
  34.  </bean>  
  35.  <bean id="transactionAttributeSource" class="org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource">  
  36.   <property name="properties">  
  37.    <props>  
  38.     <prop key="regist*">PROPAGATION_REQUIRED</prop>  
  39.     <prop key="update*">PROPAGATION_REQUIRED</prop>  
  40.     <prop key="delete*">PROPAGATION_REQUIRED</prop>  
  41.     <prop key="*">PROPAGATION_REQUIRED,readOnly</prop>  
  42.    </props>  
  43.   </property>  
  44.  </bean>  
  45.  <bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">  
  46.   <property name="transactionManager" ref="transactionManager"></property>  
  47.   <property name="transactionAttributeSource" ref="transactionAttributeSource"></property>  
  48.  </bean>  
  49.  <bean id="beanNameAutoProxy" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">  
  50.   <property name="interceptorNames">  
  51.    <list>  
  52.     <value>transactionInterceptor</value>  
  53.    </list>  
  54.   </property>  
  55.   <property name="beanNames">  
  56.    <list>  
  57.     <value>*ServiceImpl</value>  
  58.    </list>  
  59.   </property>  
  60.  </bean>  
  61.   
  62.  <!-- ibatis -->  
  63.  <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">  
  64.   <property name="configLocation" value="classpath:sqlMapConfig.xml"></property>  
  65.   <property name="dataSource" ref="dataSource"></property>  
  66.  </bean>  
  67.   
  68. </beans>  
MyBatis(iBatis)をSpringの管理下に置きます。
Springがトランザクションの管理(Begin、Rollback、Commit等)を、MyBatisがSQLの実行のみを担当するといった役割分担を可能にするためです。
そうすることで、MyBatisをSlim3に置き換えた場合にも同じ設計で実装できます。

大半を省略しましたが、設定ファイルについては以上です。
次回は、Google App Engine上でSlim3と連携させてみます。

0 件のコメント:

コメントを投稿