spring事务管理,xml配置aop事务和注解配置aop事务

xml配置和注解配合共同代码

AccountService.java

public interface AccountService {

    //转账方法
    void transfer(Integer from,Integer to,Double money);

}

AccountServiceImpl.java

xml配置aop事务的AccountServiceImpl.java

public class AccountServiceImpl implements AccountService {

    private AccountDao ad ;

    @Override
    public void transfer(final Integer from,final Integer to,final Double money) {
        //减钱
        ad.decreaseMoney(from, money);
        int i = 1/0;
        //加钱
        ad.increaseMoney(to, money);
    }

    public void setAd(AccountDao ad) {
        this.ad = ad;
    }

}

注解配置aop事务的AccountServiceImpl.java

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

    private AccountDao ad ;

    @Override
    @Transactional(isolation=Isolation.REPEATABLE_READ,propagation=Propagation.REQUIRED,readOnly=false)
    public void transfer(final Integer from,final Integer to,final Double money) {
        //减钱
        ad.decreaseMoney(from, money);
        int i = 1/0;
        //加钱
        ad.increaseMoney(to, money);
    }

    public void setAd(AccountDao ad) {
        this.ad = ad;
    }

}

AccountDao.java

public interface AccountDao {

    //加钱
    void increaseMoney(Integer id,Double money);
    //减钱
    void decreaseMoney(Integer id,Double money);
}

AccountDaoImpl.java

public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao  {

    @Override
    public void increaseMoney(Integer id, Double money) {
        //JdbcTemplate:spring整合jdbc操作数据库的类
        getJdbcTemplate().update("update t_account set money = money+? where id = ? ", money,id);
    }

    @Override
    public void decreaseMoney(Integer id, Double money) {
        getJdbcTemplate().update("update t_account set money = money-? where id = ? ", money,id);
    }

}

xml配置aop事务的applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd ">

    <!-- 指定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>

    <!-- 2.Dao -->
    <bean name="accountDao" class="cn.itcast.dao.AccountDaoImpl">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!-- 3.Service -->
    <bean name="accountService" class="cn.itcast.service.AccountServiceImpl">
        <property name="ad" ref="accountDao"></property>
    </bean>

</beans>

注解配置aop事务的applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd ">

    <!-- 指定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>

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

    <!-- 2.Dao -->
    <bean name="accountDao" class="cn.itcast.dao.AccountDaoImpl">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!-- 3.Service -->
    <bean name="accountService" class="cn.itcast.service.AccountServiceImpl">
        <property name="ad" ref="accountDao"></property>
    </bean>

</beans>

测试类Demo.java

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class Demo {
    @Resource(name="accountService")
    private AccountService as;

    @Test
    public void fun1(){

        as.transfer(1, 2, 100d);

    }
}
时间: 2024-08-04 00:24:38

spring事务管理,xml配置aop事务和注解配置aop事务的相关文章

Spring事务管理的另一种方式--TransactionTemplate编程式事务管理简单入门

1, 一直以来, 在用Spring进行事物管理时, 只知道用声明式的策略, 即根据不同的数据源, 配置一个事物管理器(TransactionManager), 通过配置切面(PointCut)应用到相应的业务方法上或者直接在方法上加@Ttransactional注解. 这种事务管理使用起来比较简单,但个人感觉灵活性欠缺了点. 2, 最近看公司项目代码, 发现有位同事在他的模块了用了另外一种事务管理方式, 查了一下,TransactionTemplate是编程式事务管理.需要自己手动在每个业务方法

Spring学习之旅(七)基于XML配置与基于AspectJ注解配置的AOP编程比较

本篇博文用一个稍复杂点的案例来对比一下基于XML配置与基于AspectJ注解配置的AOP编程的不同. 相关引入包等Spring  AOP编程准备,请参考小编的其他博文,这里不再赘述. 案例要求: 写一个简单的实现四则运算的计算器. 加入AOP功能:日志功能:检测参数中是否有负数的功能. 废话不多说了,直接上代码: (一)基于XML配置: 定义了一个接口类: package com.edu.aop; public interface ArithmeticCalculator { int add(i

Spring初学之spring的事务管理xml

所有的java类都是用的上一篇文章:Spring初学之spring的事务管理 不同的是,这时xml配置事务,所以就要把java类中的那些关于spring的注解都删掉,然后在xml中配置,ApplicationContext.xml如下: 1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans&q

马士兵Spring-声明式事务管理-XML

1.com.cy.model中User.java  和 Log.java 实体 和上一节一样: 2.DAO的实现类com.cy.dao.impl中的UserDAOImpl.LogDAOImpl.java和上一节一样: 3.UserService.java: package com.cy.service; import javax.annotation.Resource; import org.springframework.stereotype.Component; import org.spr

该伙伴事务管理器已经禁止了它对远程/网络事务的支持

原文地址 简介 当启动分布式的事务涉及 SQL Server 数据库引擎时,需要针对MSDTC进行一些配置.否则可能会出现以下错误:合作伙伴事务管理器已经禁用对远程/网络事务的支持. 解决方案 1.       双方启动MSDTC服务,方式为:命令行下service.msc,选择distributed link trackingclient ,右键启动,并且,右键->属性->启动类型改为手动. 2.       对dtc进行手动设置,方式如下:控制面板->管理工具->在[组件服务]

【Spring五】AOP之使用注解配置

AOP使用注解配置流程: 1.当spring容器启动时候,    < context:component- scan base-package= "cn.itheima03.spring.aop.annotation" ></context :component-scan> 2.在上面的包及子包中查询所有的类,按照类扫描注解的机制把类放入到spring容器中 3. 检查是否配置:<aop:aspectj-autoproxy> </aop: as

报错信息:该伙伴事务管理器已经禁止了它对远程/网络事务的支持

解决办法: (1)在windows控制面版-->管理工具-->服务-->Distributed Transaction Coordinator-->属性-->启动 (2)在CMD下运行"net start msdtc"开启服务后正常. 注:如果在第1步Distributed Transaction Coordinator 无法启动,则是因为丢失了日志文件,重新创建日志文件,再启动就行了.重新创建 MSDTC 日志,并重新启动服务的步骤如下: (1) 单击&

该事务管理器已经禁止了它对远程/网络事务的支持

在程序计算机上设置MSDTC: 控制面板->管理工具->组件服务->我的电脑->Distributed Transaction Coo->本地DTC->右键->属性->MSDTC->安全配置->选上网络DTC访问.允许远程客户端和管理.允许入站和出站.不需要验证. 看到网上还有更具体的解决方法,如:http://www.cnblogs.com/nlh774/p/3434050.html

spring aop自动代理注解配置之二

<?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/

Spring第三天——JdbcTemplate和spring事务管理

大致内容: aspectJ的aop操作(基于注解,对比day02配置操作)(会用) *jdbcTemplate操作(实现CRUD) *spring配置连接池 *spring事务管理 一.AspectJ的基于注解的AOP操作 (day02的配置回顾,略显麻烦,配置稍多) 建立项目记得导入day02操作aop的那些包(如果是复制项目一定要修改项目的context name) 再把配置文件拿过来 <?xml version="1.0" encoding="UTF-8"