(1) 使用ProxyFactoryBean 和TransactionInterceptor
<!--定义本地数据源-->
<bean id="dataSource" name="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- !定义单个jdbc数据源的事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!—定义拦截器-->
<bean id="transactionInterceptor"
class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager">
<ref bean="transactionManager"/>
</property>
<property name="transactionAttributes">
<props>
<prop key="insert*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="find*">PROPAGATION_SUPPORTS,readOnly</prop>
<prop key="get*">PROPAGATION_SUPPORTS,readOnly</prop>
<prop key="*">PROPAGATION_SUPPORTS,readOnly</prop>
</props>
</property>
</bean>
<!—定义业务对象-->
<bean id="com.prs.application.ehld.sample.biz.service.sampleService.target"
class="com.prs.application.ehld.sample.biz.service.impl.SampleServiceImpl">
<property name="userInfoDAO"
ref="com.prs.application.ehld.sample.integration.dao.userInfoDAO">
</property>
</bean>
<!—定义业务对象的事务代理对象-->
<bean id="com.prs.application.ehld.sample.biz.service.sampleService" class="org.springframeword.aop.framework.ProxyFacgtoryBean">
<property name="target"
ref="com.prs.application.ehld.sample.biz.service.sampleService.target">
</property>
<property name="interceptorNames">
<value>transactionInterceptor</value>
</property>
</bean>
通过ProxyFacgtoryBean和TransactionInterceptor组合使用,可以对事务进行更多的控制。所有需要事务控制的对象可以共享一个transactionInterceptor的事务属性。
(2) 使用TransactionProxyFactoryBean
<!—定义业务对象-->
<bean id="com.prs.application.ehld.sample.biz.service.sampleService.target"
class="com.prs.application.ehld.sample.biz.service.impl.SampleServiceImpl">
<property name="userInfoDAO"
ref="com.prs.application.ehld.sample.integration.dao.userInfoDAO">
</property>
</bean>
<!—定义业务对象的事务代理对象-->
<bean id="com.prs.application.ehld.sample.biz.service.sampleService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
abstract="true">
<property name="transactionManager">
<ref bean="transactionManager"/>
</property>
<property name="target"
ref="com.prs.application.ehld.sample.biz.service.sampleService.target" />
<property name="transactionAttributes">
<props>
<prop key="insert*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="find*">PROPAGATION_SUPPORTS,readOnly</prop>
<prop key="get*">PROPAGATION_SUPPORTS,readOnly</prop>
<prop key="*">PROPAGATION_SUPPORTS,readOnly</prop>
</props>
</property>
</bean>
使用TransactionProxyFactoryBean需要为每一个代理对象都去定义自己的事务属性。
(3) 使用TransactionProxyFactoryBean及abstract属性来简化配置
这种方工也是目前使用得最多的一种声明式事务配置方法
<!--事务控制代理抽象定义 -->
<bean id="baseTransactionProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
abstract="true">
<property name="transactionManager">
<ref bean="transactionManager"/>
</property>
<property name="transactionAttributes">
<props>
<prop key="insert*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="find*">PROPAGATION_SUPPORTS,readOnly</prop>
<prop key="get*">PROPAGATION_SUPPORTS,readOnly</prop>
<prop key="*">PROPAGATION_SUPPORTS,readOnly</prop>
</props>
</property>
</bean>
<!—定义业务对象-->
<bean id="com.prs.application.ehld.sample.biz.service.sampleService.target"
class="com.prs.application.ehld.sample.biz.service.impl.SampleServiceImpl">
<property name="userInfoDAO"
ref="com.prs.application.ehld.sample.integration.dao.userInfoDAO">
</property>
</bean>
<!—定义业务对象的事务代理对象-->
<bean id="com.prs.application.ehld.sample.biz.service.sampleService" parent="baseTransactionProxy">
<property name="target"
ref="com.prs.application.ehld.sample.biz.service.sampleService.target">
</property>
</bean>
使用abstract属性,可以让代理对象可以共享一个定义好的事务属性,使配置简化。
(4)使用BeanNameAutoProxyCreator
<!—定义拦截器-->
<bean id="transactionInterceptor"
class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager">
<ref bean="transactionManager"/>
</property>
<property name="transactionAttributes">
<props>
<prop key="insert*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="find*">PROPAGATION_SUPPORTS,readOnly</prop>
<prop key="get*">PROPAGATION_SUPPORTS,readOnly</prop>
<prop key="*">PROPAGATION_SUPPORTS,readOnly</prop>
</props>
</property>
</bean>
<!—定义bean别名自动代理创建器-->
<bean id="autoProxyCreator"
class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="interceptorNames">
<value>transactionInterceptor</value>
</property>
<property name="beanNames">
<list>
<idref local="com.prs.application.ehld.sample.biz.service.sampleService"/>
</list>
</property>
</bean>
<!—定义业务对象-->
<bean id="com.prs.application.ehld.sample.biz.service.sampleService"
class="com.prs.application.ehld.sample.biz.service.impl.SampleServiceImpl">
<property name="userInfoDAO"
ref="com.prs.application.ehld.sample.integration.dao.userInfoDAO">
</property>
</bean>