設定ファイルを書く(SpringとIBatisとweb.xml)
前回は開発環境構築まで終わったのでいよいよ作っていこうと思う。
でも自動生成できるところはしていかないとめんどくさくてしょうがない。
まずビジネスロジックはJUDEでクラス図を書いたのでjavaスケルトンコードを出力。これでインターフェースと空の実装クラスが完成。パッケージもきちんと分けておけばそのとおりにフォルダが出力されるので、ワークスペースにどーんとブン投げればOK。
次にDAOまわり。今回はIBatisを使うのでAbatorっていうツールが使える。これは前にも使ったことがあって、すごく便利。O/Rマッパーを使用できない案件などで自作DAOを作るときとかも応用できそう。まぁやってることはそんなに難しくなくて、DBにアクセスしてテーブル定義からDAOとモデル、それからIBatis用のSqlMapファイルを生成してくれる。Springと連携する用の設定にすればSqlMapClientDaoSupportを継承したクラスを作ってくれる。
これもパッケージを指定できて、実行すればその階層にファイルが出来上がる。それをワークスペースにどーんとブン投げればOK。
で、設定ファイルを書く。つっても最初に書いてしまえば後はほとんど触らなくていいヤツだからさらっと書いてしまおう。
applicationContext.xml
<?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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd" > <context:component-scan base-package="taka.of.master"/> <!-- DataSource --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="org.postgresql.Driver" /> <property name="url" value="jdbc:postgresql://localhost:5432/appdb" /> <property name="username" value="app" /> <property name="password" value="app" /> </bean> <!-- Transaction --> <tx:annotation-driven /> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- SqlMap setup for iBATIS Database Layer --> <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"> <property name="configLocation"><value>/WEB-INF/SqlMapConfig.xml</value></property> <property name="dataSource"><ref local="dataSource"/></property> </bean> </beans>
まずはBean定義はアノテーションでやるので検索するパッケージを指定する。トランザクションもアノテーション。それからIBatisの設定。今まではここにDAOの設定も書いていたんだけど今回はAbatorで
@Autowired public UsersDao(SqlMapClient sqlmapClient) { super(); this.setSqlMapClient(sqlmapClient); }
こんな感じのコンストラクタができるように生成したので設定はいらなーい。
この設定ファイルは今後さわることはほとんどないだろう。AOPでログとかの設定するときには触るかな?でもbean定義をごりごり書かなくていいのでかなりすっきり。アノテーションってすばらしいね。
SqlMapConfig.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE sqlMapConfig PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-config-2.dtd"> <sqlMapConfig> <settings useStatementNamespaces="true" /> <!-- sqlMap settings --> <sqlMap resource="taka/of/master/dao/users_SqlMap.xml"/> </sqlMapConfig>
IBatis用の設定ファイル。コレはテーブルが追加になったときは触るかな?でもそんなに頻繁にはないね。注意点はパッケージは/で区切る。コレくらい。
web.xml
<?xml version="1.0" encoding="UTF-8"?> <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" id="WebApp_ID" version="2.5"> <display-name>rumania-gorilla</display-name> <filter> <filter-name>wicket</filter-name> <filter-class>org.apache.wicket.protocol.http.WicketFilter</filter-class> <init-param> <param-name>applicationClassName</param-name> <param-value>taka.of.master.web.core.MyApplication</param-value> </init-param> </filter> <filter-mapping> <filter-name>wicket</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> </web-app>
web.xmlはこれだけ。すごくシンプル。これももう触ることはないのかな?
wicketのアプリケーションクラスの設定とspringのリスナーの設定だけ。他はwicket内部でやるのでいらないっす。
ちなみにApplicationクラスのinitメソッド内で
this.addComponentInstantiationListener(new SpringComponentInjector(this));
って書くとWicketとSpringの連携は終了
これで設定も終了。strutsだと設定ファイルが大変なことになるけど今回はコレで終了。
さて。ごりごり作っていくかな。
以上。