spring3(JNDI,宣言的トランザクション) + Doma　単体テスト環境構築 覚書

1.依存関係
----

```build.gradle
dependencies{

	["core", "context","test","tx","jdbc","aop"].each {
		compile "org.springframework:spring-${it}:3.2.3.RELEASE"
    }
    //AOP
    testCompile 'aspectj:aspectjrt:1.5.4'
    testCompile 'aspectj:aspectjweaver:1.5.4'
    
    //具象クラスに対するAOP(<aop:config proxy-target-class="true">)
    testCompile 'cglib:cglib-nodep:2.2.2'
    //JNDI on Spring Context (jndi.properties,jndi.xml)
    testCompile 'org.apache.xbean:xbean-spring:3.14'


	//... others
}
```

2.Spring3によるJNDI設定
----

jndi.propertiesに以下のクラスを登録する事により、jndi.xmlが有効になる

```jndi.properties

java.naming.factory.initial=org.apache.xbean.spring.jndi.SpringInitialContextFactory

```

dataSource定義
ポイントは、TransactionAwareDataSourceProxyでDriverManagerDataSourceをラップしているところ
(宣言的トランザクション用）

```jndi.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" 
	xsi:schemaLocation="http://www.springframework.org/schema/beans">

	<bean id="jdbcDataSource"
		class="org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy">
		<constructor-arg>
			<bean class="org.springframework.jdbc.datasource.DriverManagerDataSource">
				<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
				<property name="url" value="jdbc:oracle:thin:@YOUR_HOST:1521:orcl" />
				<property name="username" value="NAME" />
				<property name="password" value="PASS" />
			</bean>
		</constructor-arg>
	</bean>

	<bean id="jndi" class="org.apache.xbean.spring.jndi.DefaultContext">
		<property name="entries">
			<map>
				<entry key="jdbc/dataSource">
					<ref bean="jdbcDataSource" />
				</entry>
			</map>
		</property>
	</bean>

</beans>
```

3.applicationContext設定
----

2.で登録したJNDIよりデータソースを取得

```applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
         http://www.springframework.org/schema/aop
         http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
         http://www.springframework.org/schema/tx
         http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
         http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context-3.0.xsd">

	<context:annotation-config />
	<context:component-scan base-package="jp.co" />

	<!-- @see jndi.properties,jndi.xml -->
	<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
		<property name="jndiName">
			<value>jdbc/dataSource</value>
		</property>
	</bean>

	<bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource" />
	</bean>

	<tx:annotation-driven/>
	<tx:advice id="txDefault" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="*" />
		</tx:attributes>
	</tx:advice>
	<aop:config proxy-target-class="true">
		<aop:advisor
			pointcut="execution(* services.bizlogic.*.*(..))"
			advice-ref="txDefault" />
	</aop:config>
</beans>

```

3. Config(Doma)
----

```java:AppConfig.java

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

import org.seasar.doma.jdbc.DomaAbstractConfig;
import org.seasar.doma.jdbc.JdbcLogger;
import org.seasar.doma.jdbc.dialect.Dialect;
import org.seasar.doma.jdbc.dialect.OracleDialect;
import org.springframework.stereotype.Component;

@Component
public class AppConfig extends DomaAbstractConfig {

    private final Dialect DIARECT = new OracleDialect();

    DataSource dataSource;

    public AppConfig() {
	dataSource = createDataSource();
    }

    @Override
    public DataSource getDataSource() {
	return dataSource;
    }

    @Override
    public Dialect getDialect() {
	return DIARECT;
    }

    @Override
    public JdbcLogger getJdbcLogger() {
	return new Slf4JLogger();
    }

    protected DataSource createDataSource() {
	try {
	    return (DataSource) InitialContext.doLookup("jdbc/dataSource");
	} catch (NamingException e) {
	    throw new RuntimeException(e);
	}
    }
}

```