您的位置:首页 > 编程语言 > Java开发

Spring声明式事务配置管理方法(二)

2013-01-29 13:50 399 查看
附四、Spring中的四种声明式事务的配置

让我们言归正传吧。

以下两个bean的配置是下面要用到的。

<bean
id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property
name="sessionFactory">

<ref local="sessionFactory"
/>
</property>
</bean>
<bean
id="fundService"
class="com.jack.fund.service.serviceimpl.FundService">
<property
name="operdao">

<ref bean="operatorDAO"
/>
</property>
<property
name="producedao">

<ref bean="fundProduceDAO"
/>
</property>
<property
name="customerdao">

<ref bean="customerDAO"
/>
</property>
<property
name="accountdao">
<ref bean="accountDAO"
/>
</property>
<property
name="fundaccountdao">

<ref bean="fundAccountDAO"
/>
</property>
<property
name="fundtransdao">

<ref bean="fundTransDAO"
/>
</property>
</bean>

可能还有其他很多模块。可能只是其中的模块。

第一种:配置声明式事务的方法如下。也是我们最常用的方法了,它适用于你的库表比较少的情况下。
<bean
id="fundServiceDAOProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property
name="transactionManager">

<ref bean="transactionManager"
/>
</property>

<property
name="proxyTargetClass">

<value>false</value>
</property>
<property
name="proxyInterfaces">

<value>com.jack.fund.service.IFundService</value>
</property>
<property
name="target">

<ref bean="fundService"
/>
</property>
<property
name="transactionAttributes">

<props>

<prop
key="delete*">PROPAGATION_REQUIRED</prop>

<prop
key="add*">PROPAGATION_REQUIRED</prop>

<prop
key="update*">PROPAGATION_REQUIRED</prop>

<prop
key="save*">PROPAGATION_REQUIRED</prop>

<prop
key="find*">PROPAGATION_REQUIRED,readOnly</prop>

</props>
</property>
</bean>

以下可能还有其他的xxxServiceDAOProxy.大家可以看出针对每一个功能模块配置一个业务代理服务。如果模块多大话,就显得代码有点多了,发现他们只是稍微一点不一样。这时我们就应该想到继承的思想。用第二种方法。

第二种:配置声明式事务的方法如下。这种情况适合相对比较多的模块时使用。
<bean
id="transactionBase"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
lazy-init="true"
abstract="true">
<property
name="transactionManager">

<ref bean="transactionManager"
/>
</property>
<property
name="transactionAttributes">

<props>

<prop
key="delete*">PROPAGATION_REQUIRED</prop>

<prop
key="add*">PROPAGATION_REQUIRED</prop>

<prop
key="update*">PROPAGATION_REQUIRED</prop>

<prop
key="save*">PROPAGATION_REQUIRED</prop>

<prop
key="find*">PROPAGATION_REQUIRED,readOnly</prop>

</props>
</property>
</bean>

而具体的模块可以简单的这样配置。只要指明它的parent(父类)就可以了。父类一般把abstract="true",因为在容器加载的时候不需要初始化,等到用的时候再有它的子类调用的时候,再去初始化。
<bean
id="fundServiceDAOProxy" parent="transactionBase"
>
<property
name="target">
<ref bean="fundService"
/>
</property>
</bean>

这样配置的话,如果有多个像fundService这样模块时,可以少些很多重复的代码。

第三种:配置声明式事务的方法如下。主要利用BeanNameAutoProxyCreator自动创建事务代理
<bean
id="transactionInterceptor"
class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property
name="transactionManager">

<ref bean="transactionManager"
/>
</property>
<property
name="transactionAttributes">

<props>

<prop
key="delete*">PROPAGATION_REQUIRED</prop>

<prop
key="add*">PROPAGATION_REQUIRED</prop>

<prop
key="update*">PROPAGATION_REQUIRED</prop>

<prop
key="save*">PROPAGATION_REQUIRED</prop>

<prop
key="find*">PROPAGATION_REQUIRED,readOnly</prop>

</props>
</property>
</bean>
<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property
name="beanNames">

<list>

<value>fundService</value>

</list>
</property>
<property
name="interceptorNames">

<list>

<value>transactionInterceptor</value>

</list>
</property>
</bean>

这种方法主要利用了拦截器的原理。

前三种方法一般都必需指定具体的模块bean.如果模块过多话,比如一个大型的网站一般有几十个模块。我们就得考虑用第四种的配置方式了。自动创建事务代理的方式了。

第四种:配置声明式事务的方法如下。
<bean
id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property
name="transactionManager">

<ref bean="transactionManager"
/>
</property>
<bean
id="autoproxy"
class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property
name="beanNames">

<list>

<value>*Service</value>

</list>
</property>
<property
name="interceptorNames">

<list>
<value>transactionInterceptor</value>

</list>
</property>
</bean>

自动代理还有一种用法就是结合正规表达式和advice使用。
<bean
id="transactionInterceptor"
class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property
name="transactionManager">

<ref bean="transactionManager"
/>
</property>
<bean
id="autoProxyCreator" class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"
/>
<bean
id="regexpMethodPointcutAdvisor"
class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property
name="advice">
<ref bean="transactionInterceptor"
/>
</property>
<property
name="pattern">
<value>.*</value>
</property>
</bean>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: