下面是摘取自applicationContext.xml中的一部分代码:
<!--定义transactionManager,为其中的sessionFactory属性注入前面自己配置的sessionFactory -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!--定义AOP切面的通知点.交由上面定义的transactionManager来进行管理 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!--定义的事物传播特性 一般的访问类,如get,find,obtain.load等都是只读属性 -->
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="insert*" propagation="REQUIRED"/>
<tx:method name="drop*" propagation="REQUIRED"/>
<tx:method name="get*" propagation="REQUIRED" read-only="true"/>
<tx:method name="find*" propagation="REQUIRED" read-only="true"/>
<tx:method name="obtain*" propagation="REQUIRED" read-only="true"/>
<tx:method name="load*" propagation="REQUIRED" read-only="true"/>
<tx:method name="list*" propagation="REQUIRED" read-only="true"/>
</tx:attributes>
</tx:advice>
<!--定义Aop配置 -->
<aop:config proxy-target-class="true">
<aop:advisor advice-ref="txAdvice" pointcut="execution(* com.hfms.service..*.*(..))"/>
</aop:config>
spring通过此配置就可以对事物进行自动管理了,出现异常会相应的回滚等.