Spring 配置事物处理的几种方法
方法一:
----------------------------------------------------------------------------------
<bean name="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean name="transactionProxyTemplate" abstract="true"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref bean="transactionManager" />
</property>
<property name="target">
<list>
<ref bean="classService" /><!-- 要配置事物的Service或者DAO都配置到这里就OK了 -->
<ref bean="userService" />
</list>
</property>
<property name="transactionAttributes">
<props>
<prop key="find*">PROPAGATION_REQUIRED, readOnly</prop>
<prop key="create*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="modify*">PROPAGATION_REQUIRED</prop>
<prop key="delete*">PROPAGATION_REQUIRED</prop>
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
第二种方法:
----------------------------------------------------------------------------------
<bean name="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean name="transactionProxyTemplate" abstract="true"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref bean="transactionManager" />
</property>
<property name="transactionAttributes">
<props>
<prop key="find*">PROPAGATION_REQUIRED, readOnly</prop>
<prop key="create*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="modify*">PROPAGATION_REQUIRED</prop>
<prop key="delete*">PROPAGATION_REQUIRED</prop>
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<bean name="classService" abstract="false" singleton="true"
lazy-init="default" autowire="default" dependency-check="default"
parent="transactionProxyTemplate"><!-- 哪个Service需要事物就配一个这样的Bean -->
<property name="target">
<bean class="su.service.impl.ClassServiceImpl">
<property name="classDao">
<ref bean="classDao" />
</property>
</bean>
</property>
</bean>
方法三:
----------------------------------------------------------------------------------
为每一个需要事物的DAO配置一个代理Bean
<bean id="deptsortDAOProxy"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref bean="transactionManager" />
</property>
<property name="target">
<ref bean="deptsortDAO" />
</property>
<property name="transactionAttributes">
<props>
<prop key="create*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="delete*">PROPAGATION_REQUIRED</prop>
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
Service配置的时候直接引用配置好的代理DAO
<bean id="deptsortService"
class="com.oa.service.DeptsortServiceImpl">
<property name="deptsortDAO">
<ref bean="deptsortDAOProxy" />
</property>
</bean>