JavaEE spring事务管理——基于注解的声明式服务

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_40788630/article/details/83025342

通过注解实现spring事务管理非常简单,开发者只需要做两件事,

1.在spring容器中注册事务注解驱动,

2.在需要使用到事务的spring bean类或者bean类的方法上添加注解@transaction

下面以Annotation方式实现事务管理,具体步骤如下:

(本节代码套用上一篇文章中的代码,,,上一篇文章点这里

一、在src目录下,创建一个Spring配置文件applicationContext-annotation.xml在该文件中声明是管理器等配置信息

<?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/beans 
    http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-4.3.xsd
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
        
        <!-- 1.配置数据源 -->
        <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <!-- 数据库驱动 -->
            <property name="driverClassName" value="com.mysql.jdbc.Driver" />
            <!-- 连接数据库的url -->
            <property name="url" value="jdbc:mysql://localhost/spring" />
            <!-- 连接数据库的用户名 -->
            <property name="username" value="root" />
            <!-- 连接数据库的用户密码 -->
            <property name="password" value="itcast" /> 
        </bean>
       
        <!-- 2.配置JDBC模板 -->
        <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
            <!-- 默认使用数据源 -->
            <property name="dataSource" ref="dataSource" />
        </bean>
        
        <!-- 3.定义一个名为accountDao的bean -->
        <bean id="accountDao" class="com.itheima.jdbc.AccountDaoImpl">
            <property name="jdbcTemplate" ref="jdbcTemplate" />
        </bean>
        
        <!-- 4.事务管理器,依赖于数据源 -->
        <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		     <property name="dataSource" ref="dataSource" />
        </bean>	
       
        <!-- 5.事务管理器驱动 -->
        <tx:annotation-driven transaction-manager="transactionManager"/>
        
</beans>

二、在AccountDaoImpl类的transfer()方法上添加注解,添加过后的代码如下所示:

@Transactional
	public void transfer(String outUser, String inUser, double money) {
		//收款操作
		this.jdbcTemplate.update("update account set balance = balance + ? where username = ?",money,inUser);
		
		int i=1/0;
		//收款操作
		this.jdbcTemplate.update("update account set balance = balance - ? where username = ?",money,outUser);
	}

三、在TransactionTest测试类中,创建测试方法annotationTest(),编辑后的代码如下所示:

@Test
	public void annptationTest() {
		ApplicationContext cast = new ClassPathXmlApplicationContext("applicationContext-annotation.xml");
		AccountDao accountDao = (AccountDao) cast.getBean("accountDao");
		
		//调用实例中的转账方法
		accountDao.transfer("rose", "joy", 100.0);
		System.out.println("转账成功了!");
	}

四、检查功能实现情况

首先先看一下数据表account中

接着执行以下测试程序,注意此时transaction方法中含有除零错误,所以如果事务管理若是起作用的话应该是双方账户余额不变

执行后发现提示是出现除零错误,再次查看数据表,

发现账户余额确实没有发生变化,接着下面将除零错误去除

再次测试

提示执行正常了,现在再次查询数据表

发现确实发生了账户余额转移,结合刚才发生错误时的情况对比我们就会发现,基于注解的Spring事务管理确实正常工作了

猜你喜欢

转载自blog.csdn.net/qq_40788630/article/details/83025342