spring1的声明式事务配置:
<!-- 事务的处理 -->
<!-- Hibernate使用的事物管理器-->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
<bean id="jdbcTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="jdbcDataSource"></property>
</bean>
<!-- 事务处理代理的抽象类,可被继承使用-->
<bean id="baseTransactionProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true">
<property name="transactionManager">
<ref bean="transactionManager" />
</property>
<property name="transactionAttributes">
<props>
<prop key="create*">PROPAGATION_REQUIRED</prop>
<prop key="remove*">PROPAGATION_REQUIRED</prop>
<prop key="confirm*">PROPAGATION_REQUIRED, -BetException</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="set*">PROPAGATION_REQUIRED</prop>
<prop key="do*">PROPAGATION_REQUIRED, -SettleException</prop>
<prop key="settle">PROPAGATION_REQUIRED, -Exception</prop>
<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
<bean id="jdbcTransactionProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true">
<property name="transactionManager" ref="jdbcTransactionManager"></property>
<property name="transactionAttributes">
<props>
<prop key="refuseGaming">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
因为我们的事务一般都放在service层,ok 看下service是如何与上面的事务配置联系起来的
<bean id="matchService" parent="baseTransactionProxy">
<property name="target">
<bean class="com.airinbox.gamester.service.impl.MatchService">
<property name="matchDao">
<ref bean="matchHibernateDao" />
</property>
<property name="gamblingDao">
<ref bean="gamblingHibernateDao" />
</property>
<property name="teamDao">
<ref bean="teamHibernateDao" />
</property>
<property name="starDao">
<ref bean="starHibernateDao" />
</property>
</bean>
</property>
</bean>
ok,spring1.x事务配置完成,下面看spring2的配置简单了很多
<!--Hibernate TransactionManager-->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- 以AspectJ方式 定义 AOP -->
<aop:config >
<!-- 注意,请把第2个*号换为项目package 这里的写法可以参考spring手册 -->
<aop:advisor pointcut="execution(* *..service.*Manager.*(..))" advice-ref="txAdvice"/>
<aop:advisor pointcut="execution(* org.springside.core.dao.*Dao.*(..))" advice-ref="txAdvice"/>
</aop:config>
<!-- 基本事务定义,使用transactionManager作事务管理,默认get*方法的事务为readonly,其余方法按默认设置.
默认的设置请参考Spring文档事务一章. -->
<tx:advice id="txAdvice">
<tx:attributes>
<tx:method name="get*" read-only="true"/>
<tx:method name="find*" read-only="true"/>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
是不是简单了很多!