1、事务切面:
package com.hibernate.util;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;
public class TxAspect {
private PlatformTransactionManager platformTransactionManager;
public void setPlatformTransactionManager(
PlatformTransactionManager platformTransactionManager) {
this.platformTransactionManager = platformTransactionManager;
}
public Object tx(ProceedingJoinPoint joinPoint) throws Throwable{
Object rev = null;
//TransactionDefinition的实现类
DefaultTransactionDefinition td = new DefaultTransactionDefinition();
//开始事物,传入TransactionDefinition对象,用于指定事物相关属性(隔离、传播、超时、只读 )
TransactionStatus ts = platformTransactionManager.getTransaction(td);
try {
rev = joinPoint.proceed();//直接使用原来的参数调用目标方法;
//提交事务
platformTransactionManager.commit(ts);
} catch (Exception e) {
// 回滚事务
System.out.println(e);
platformTransactionManager.rollback(ts);
}
return rev;
}
}
<?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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- <context:component-scan base-package="com.hibernate"></context:component-scan> -->
<!-- 配置数据源 -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 配置C3p0 -->
<bean id="datasource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="initialPoolSize" value="${jdbc.initPoolSize}"></property>
</bean>
<!-- 配置hibernate 的 session Factory 实例 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="datasource"></property>
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
<!-- <property name="mappingLocations" value="classpath:com/hibernate/entities/*.hbm.xml"></property> -->
</bean>
<bean id="bookDao" class="com.hibernate.dao.BookDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="bookService" class="com.hibernate.service.BookServiceImpl">
<property name="bookDao" ref="bookDao"></property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get*" propagation="REQUIRED" read-only="false"/>
</tx:attributes>
</tx:advice>
<!--1、配置事物管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 编程式事务管理 注解开启 -->
<bean id="txAspect" class="com.hibernate.util.TxAspect">
<property name="platformTransactionManager" ref="transactionManager"></property>
</bean>
<aop:config>
<aop:pointcut expression="execution(* com.hibernate.service.*.*(..))" id="fkpc"/>
<!-- 将txAspect当成aspect使用 -->
<aop:aspect ref="txAspect" >
<aop:around method="tx" pointcut-ref="fkpc"/>
</aop:aspect>
</aop:config>
<!-- 编程式事务管理结束 -->
<!-- 申明式事务管理 注解开启-->
<!--
配置事物属性
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get" read-only="true"/>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
配置事物切点
<aop:config>
<aop:pointcut expression="execution(* com.hibernate.service.*.*(..))" id="txPointcut"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
</aop:config> -->
<!-- 申明式事务管理 结束-->
</beans>