一、Spring注解事务配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsd">
<!-- 配置sessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
</bean>
<!-- 配置事管理 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 用注解来实现事务管理 将所有具有@Transactional 注解的文件自动配置为声明式事务支持-->
<tx:annotation-driven transaction-manager="transactionManager" />
<!-- 处理注解 -->
<bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor"/>
</beans>
说明:
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="false"/>
中的proxy-target-class默认是false 表示不使用Java CGLIB 代理而使用Spring自己的Aop代理,这样可以在接口中注解事务
二、Spring声明事务配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsd">
<!-- 配置sessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
</bean>
<!-- 配置事管理 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 用注解实现事务,就不要下面的了 -->
<!-- 配置事务的传播性 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="Add*" propagation="REQUIRED"/>
<tx:method name="Delete*" propagation="REQUIRED"/>
<tx:method name="Modify*" propagation="REQUIRED"/>
<tx:method name="*" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- 配置有那些类中的方法拥有事务的传播性 -->
<aop:config>
<aop:pointcut id="pointcut" expression="execution(* com.svse.dao.impl.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"/>
</aop:config>
<!-- 处理注解 -->
<bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor"/>
</beans>
本文深入探讨了Spring框架中事务管理的两种主要方式:注解式和声明式配置。通过XML配置文件,展示了如何设置sessionFactory、transactionManager,并使用@Transaction注解来自动管理事务。同时,解释了如何通过配置事务属性,实现不同方法的传播行为,确保数据的一致性和完整性。

2240

被折叠的 条评论
为什么被折叠?



