13Spring - Spring转账Demo(了解事务及AOP)

简介: 13Spring - Spring转账Demo(了解事务及AOP)

Spring的转账案例

1)代码准备

1)创建业务层

public interface AccountService {
    public void transfer(String from, String to, Double money);
}
-----------------------------------------------------------------------------------
public class AccountServiceImpl implements AccountService {
    // 业务层注入 DAO:
    private AccountDao accountDao;
    public void setAccountDao(AccountDao accountDao) {
        this.accountDao = accountDao;
    }
    @Override
    /**
     * from:转出的账号
     * to:转入的账号
     * money:转账金额
     */
    public void transfer(String from, String to, Double money) {
        accountDao.outMoney(from, money);
        accountDao.inMoney(to, money);
    }
}

2)创建DAO类

public interface AccountDao {
    public void outMoney(String from, Double money);
    public void inMoney(String to, Double money);
}
-------------------------------------------------------------------------------
public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao {
    @Override
    public void outMoney(String from, Double money) {
        this.getJdbcTemplate().update("update account set money = money - ? where
                name = ? ", money,from);
    }
    @Override
    public void inMoney(String to, Double money) {
        this.getJdbcTemplate().update("update account set money = money + ? where
                name = ? ", money,to);
    }
}

3)配置业务层和DAO类

<!-- 配置业务层的类 -->
<bean  id="accountService" class="cn.itcast.transaction.demo1.AccountServiceImpl">
    <property name="accountDao" ref="accountDao"/>
</bean>
<!-- 配置 DAO 的类 -->
<bean id="accountDao" class="cn.itcast.transaction.demo1.AccountDaoImpl">
    <property name="dataSource" ref="dataSource"/>
</bean>

3)测试

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext2.xml")
public class SpringDemo4 {
    @Resource(name = "accountService")
    private AccountService accountService;
    @Test
    // 转账的测试:
    public void demo1() {
        accountService.transfer("会希", "凤姐", 1000d);
    }
}

第一种 - Spring的编程式事务

1)配置事务管理器

<bean  id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"/>
</bean>

2)配置事务管理模板

<bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
    <property name="transactionManager" ref="transactionManager"/>
</bean>

3)业务层注入事务管理模板

<!-- 配置业务层的类 -->
<bean id="accountService" class="cn.itcast.transaction.demo1.AccountServiceImpl">
  <property name="accountDao" ref="accountDao"/>
  <!-- 注入事务管理模板 -->
  <property name="transactionTemplate" ref="transactionTemplate"/>
</bean>

4)代码实现

public void transfer(final String from, final String to, final Double money) {
        transactionTemplate.execute(new TransactionCallbackWithoutResult() {
            @Override
            protected void doInTransactionWithoutResult(TransactionStatus status) {
                accountDao.outMoney(from, money);
                int d = 1 / 0;
                accountDao.inMoney(to, money);
            }
        });
    }

第二种 - Spring的声明式事务AOP(XML的方式)

1)配置事务管理器

<bean  id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"/>
</bean>

2)配置事务通知

<!-- 配置事务的增强 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
    <!-- isolation="DEFAULT" 隔离级别
         propagation="REQUIRED" 传播行为
         read-only="false"  只读
         timeout="-1"  过期时间
         rollback-for="" -Exception
         no-rollback-for="" +Exception
    -->
    <tx:method name="transfer" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>

3)配置AOP事务

<aop:config>
  <aop:pointcut  expression="execution(*cn.itcast.transaction.demo2.AccountServiceImpl.transfer(..))" id="pointcut1"/>
  <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut1"/>
</aop:config>

第二种 - Spring的声明式事务AOP(注解的方式)

1)配置事务管理器

<bean  id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"/>
</bean>

2)开启事务管理的注解

<tx:annotation-driven transaction-manager="transactionManager"/>

3)在使用事务的类上添加一个注解:@Transactional

20190403102149386_.png


目录
相关文章
|
8天前
|
Java 数据库连接 数据库
spring复习05,spring整合mybatis,声明式事务
这篇文章详细介绍了如何在Spring框架中整合MyBatis以及如何配置声明式事务。主要内容包括:在Maven项目中添加依赖、创建实体类和Mapper接口、配置MyBatis核心配置文件和映射文件、配置数据源、创建sqlSessionFactory和sqlSessionTemplate、实现Mapper接口、配置声明式事务以及测试使用。此外,还解释了声明式事务的传播行为、隔离级别、只读提示和事务超时期间等概念。
spring复习05,spring整合mybatis,声明式事务
|
8天前
|
设计模式 Java 测试技术
spring复习04,静态代理动态代理,AOP
这篇文章讲解了Java代理模式的相关知识,包括静态代理和动态代理(JDK动态代理和CGLIB),以及AOP(面向切面编程)的概念和在Spring框架中的应用。文章还提供了详细的示例代码,演示了如何使用Spring AOP进行方法增强和代理对象的创建。
spring复习04,静态代理动态代理,AOP
|
11天前
|
Java 测试技术 数据库
Spring事务传播机制(最全示例)
在使用Spring框架进行开发时,`service`层的方法通常带有事务。本文详细探讨了Spring事务在多个方法间的传播机制,主要包括7种传播类型:`REQUIRED`、`SUPPORTS`、`MANDATORY`、`REQUIRES_NEW`、`NOT_SUPPORTED`、`NEVER` 和 `NESTED`。通过示例代码和数据库插入测试,逐一展示了每种类型的运作方式。例如,`REQUIRED`表示如果当前存在事务则加入该事务,否则创建新事务;`SUPPORTS`表示如果当前存在事务则加入,否则以非事务方式执行;`MANDATORY`表示必须在现有事务中运行,否则抛出异常;
46 4
Spring事务传播机制(最全示例)
|
6天前
|
Java Spring
Spring 事务传播机制是什么?
Spring 事务传播机制是什么?
14 4
|
22天前
|
Java 数据库连接 数据库
Spring基础3——AOP,事务管理
AOP简介、入门案例、工作流程、切入点表达式、环绕通知、通知获取参数或返回值或异常、事务管理
Spring基础3——AOP,事务管理
|
2月前
|
XML Java 数据格式
Spring5入门到实战------11、使用XML方式实现AOP切面编程。具体代码+讲解
这篇文章是Spring5框架的AOP切面编程教程,通过XML配置方式,详细讲解了如何创建被增强类和增强类,如何在Spring配置文件中定义切入点和切面,以及如何将增强逻辑应用到具体方法上。文章通过具体的代码示例和测试结果,展示了使用XML配置实现AOP的过程,并强调了虽然注解开发更为便捷,但掌握XML配置也是非常重要的。
Spring5入门到实战------11、使用XML方式实现AOP切面编程。具体代码+讲解
|
2月前
|
缓存 Java 开发者
Spring高手之路22——AOP切面类的封装与解析
本篇文章深入解析了Spring AOP的工作机制,包括Advisor和TargetSource的构建与作用。通过详尽的源码分析和实际案例,帮助开发者全面理解AOP的核心技术,提升在实际项目中的应用能力。
23 0
Spring高手之路22——AOP切面类的封装与解析
|
2月前
|
XML Java 数据库
Spring5入门到实战------15、事务操作---概念--场景---声明式事务管理---事务参数--注解方式---xml方式
这篇文章是Spring5框架的实战教程,详细介绍了事务的概念、ACID特性、事务操作的场景,并通过实际的银行转账示例,演示了Spring框架中声明式事务管理的实现,包括使用注解和XML配置两种方式,以及如何配置事务参数来控制事务的行为。
Spring5入门到实战------15、事务操作---概念--场景---声明式事务管理---事务参数--注解方式---xml方式
|
2月前
|
Java Spring XML
掌握面向切面编程的秘密武器:Spring AOP 让你的代码优雅转身,横切关注点再也不是难题!
【8月更文挑战第31天】面向切面编程(AOP)通过切面封装横切关注点,如日志记录、事务管理等,使业务逻辑更清晰。Spring AOP提供强大工具,无需在业务代码中硬编码这些功能。本文将深入探讨Spring AOP的概念、工作原理及实际应用,展示如何通过基于注解的配置创建切面,优化代码结构并提高可维护性。通过示例说明如何定义切面类、通知方法及其应用时机,实现方法调用前后的日志记录,展示AOP在分离关注点和添加新功能方面的优势。
38 0
|
2月前
|
Java Spring
【Azure 应用服务】记一次Azure Spring Cloud 的部署错误 (az spring-cloud app deploy -g dev -s testdemo -n demo -p ./hellospring-0.0.1-SNAPSHOT.jar --->>> Failed to wait for deployment instances to be ready)
【Azure 应用服务】记一次Azure Spring Cloud 的部署错误 (az spring-cloud app deploy -g dev -s testdemo -n demo -p ./hellospring-0.0.1-SNAPSHOT.jar --->>> Failed to wait for deployment instances to be ready)
下一篇
无影云桌面