この記事はJavaEE Advent Calendar 2012の13日目の記事です。
参加するっていったものの何を書こうかなと考えていたのですが、とりあえずSpring on Glassfishでいってみようかなと。正直Spring選ぶならTomcat、Glassfish選ぶならJavaEE6じゃないのとか思ったりするのですが、深く考えずにいってみましょう。
とりあえずIDEはEclipseを使います。まずは「Glassfish Java EE Application Server Plugin for Eclipse」を入れましょう。Eclipse MarketplaceでGlassfishで検索すれば出てくると思うのでinstallをクリックすればインストールできます。またこのプラグインを使うとGlassfishのインストールも行うことができます。
インストールしたら今回はJavaDBを使うので、Preferences>Glassfish Preferencesで以下の用に設定しておくと便利です。
今回は実験用にちょっとしたサンプルを作ってみました。
Boyakyという「ぼやき」を登録していくサイトです。
ソースはここにおいておきます。
「ぼやき」を登録して表示するだけなので非常に単純です。
今回はSpringからGlassfishを呼び出すことが目的なので、SpringからJPA経由でGlassfishが管理しているデータソースを呼び出す部分がメインになります。 問題は設定ファイルです。
persistence.xmlは以下のようになっています。
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<!-- JTAを使用します -->
<persistence-unit name="BoyakiPU" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>jdbc/__default</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="eclipselink.ddl-generation" value="drop-and-create-tables" />
<property name="eclipselink.ddl-generation.output-mode" value="database" />
<property name="eclipselink.logging.level.sql" value="FINE" />
</properties>
</persistence-unit>
</persistence>
またSpringの設定ファイルは以下のようになっています。
<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
<!-- ServiceとRepositoryを自動登録する -->
<context:component-scanbase-package="jp.noriand.boyaky.biz"/>
<!-- EntityManagerFactoryとTransactionManagerを定義する -->
<bean id="emf"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<propertyname="persistenceUnitName"value="BoyakiPU"/>
</bean>
<bean id="txManager"
class="org.springframework.transaction.jta.JtaTransactionManager"/>
<!-- PersistenceContextアノテーションにEntityManagerをインジェクトする -->
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor "/>
<!-- JPAの例外をSpringの例外に変換する -->
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
<!-- アノテーションベースでトランザクション制御をする -->
<tx:annotation-driventransaction-manager="txManager"/>
<!-- APサーバのTransationManagerでトランザクション制御をする -->
<tx:jta-transaction-manager/>
<!-- メッセージリソースを設定する -->
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<propertyname="basename"value="classpath:/META-INF/messages"/>
</bean>
<!-- JSR303ベースでのバリデーションをする -->
<bean id="validator"
class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
<propertyname="validationMessageSource"ref="messageSource"/>
</bean>
</beans>
説明はコメントを見れば分かるかなということで省略します。
とりあえずソースを落として動かしてみれば感触はつかめるのではないかと思います。
ということで本日は終了です。