Srping aop事务

来源:互联网 发布:淘宝宝贝回收站在哪里 编辑:程序博客网 时间:2024/06/06 03:46

准备工作

导入aop aspect aopalliance weaver包
导入约束

xml方式配置事务

<!-- 指定spring读取db.properties配置 --><context:property-placeholder location="classpath:db.properties"  /><!-- 1.将连接池 --><bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" >    <property name="jdbcUrl" value="${jdbc.jdbcUrl}" ></property>    <property name="driverClass" value="${jdbc.driverClass}" ></property>    <property name="user" value="${jdbc.user}" ></property>    <property name="password" value="${jdbc.password}" ></property></bean><!-- 事务核心管理器,封装了所有事务操作. 依赖于连接池 --><bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" >    <property name="dataSource" ref="dataSource" ></property></bean><!-- 配置事务通知 --><tx:advice id="txAdvice" transaction-manager="transactionManager" >    <tx:attributes>        <!-- 以方法为单位,指定方法应用什么事务属性            isolation:隔离级别            propagation:传播行为            read-only:是否只读         -->        <tx:method name="save*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />        <tx:method name="persist*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />        <tx:method name="update*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />        <tx:method name="modify*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />        <tx:method name="delete*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />        <tx:method name="remove*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />        <tx:method name="get*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true" />        <tx:method name="find*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true" />        <tx:method name="transfer" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />    </tx:attributes></tx:advice><!-- 配置织入 --><aop:config  >    <!-- 配置切点表达式 -->    <aop:pointcut expression="execution(* cn.itcast.service.*ServiceImpl.*(..))" id="txPc"/>    <!-- 配置切面 : 通知+切点            advice-ref:通知的名称            pointcut-ref:切点的名称     -->    <aop:advisor advice-ref="txAdvice" pointcut-ref="txPc" /></aop:config>

注解配置事务

xml中

<!-- 开启使用注解管理aop事务 --><tx:annotation-driven/>

目标对象的类的上边

@Transactional(isolation=Isolation.REPEATABLE_READ,propagation=Propagation.REQUIRED,readOnly=true)public class AccountServiceImpl implements AccountService {...

这样就开启了 类中所有方法的事务

原创粉丝点击