Spring声明式事务基于xml配置

Spring支持编程式事务管理和声明式事务管理。

  1. 编程式事务管理:事务和业务代码耦合度太高。

  2. 声明式事务管理:侵入性小,把事务从业务代码中抽离出来,使用AOP配置到配置文件中,提高维护性。

第一步:项目结构图

导入相关依赖包,并且在applicationContext.xml配置文件引入tx的命名空间

在这里插入图片描述

第二步:编写pojo

package cn.zj.spring.pojo;

public class User {
	private Integer id;
	private String name;
	private String email;

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	@Override
	public String toString() {
		return "User [id=" + id + ", name=" + name + ", email=" + email + "]";
	}

	public User(Integer id, String name, String email) {
		super();
		this.id = id;
		this.name = name;
		this.email = email;
	}

	public User() {
		super();
		// TODO Auto-generated constructor stub
	}

}

第三步:编写持久层(dao)的代码

package cn.zj.spring.dao.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import cn.zj.spring.dao.UserDao;

@Repository
public class UserDaoImpl implements UserDao {

	@Autowired
	private JdbcTemplate jdbcTemplate;

	@Override
	public void transOut(Integer outId, Integer money) {
		this.jdbcTemplate.update("update t_account set balance = balance - ? where id = ?",money,outId);
	}

	@Override
	public void transIn(Integer inId, Integer money) {
		this.jdbcTemplate.update("update t_account set balance = balance + ? where id = ?",money,inId);
		
	}
}


第四步:编写业务层接口和实现类

package cn.zj.spring.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.zj.spring.dao.UserDao;
import cn.zj.spring.service.UserService;

@Service
public class UserServiceImpl implements UserService {
	
	
	@Autowired
	private UserDao userDao;

	@Override
	public void trans(Integer outId, Integer inId, Integer money) {
		
		//扣钱
		userDao.transOut(outId, money);
		System.out.println(1/0);
		//打钱
		userDao.transIn(inId, money);
		
	}

	
}

第五步:编写db.properties配置文件

#批量修改  alt+shift +a 修改完毕以后 ,alt+shirf+a 还原
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/gj1?characterEncoding=utf-8
jdbc.username=root
jdbc.password=123456
jdbc.maxActive=10

第六步:编写applicationContext.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: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/beans http://www.springframework.org/schema/beans/spring-beans.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
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
	
	<context:component-scan base-package="cn.zj.spring"/>
	
	
	<context:property-placeholder location="classpath:db.properties"/>
	
	
	<!-- 创建连接池(数据源) -->
	<bean  id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" 
	init-method="init" destroy-method="close">
		<!-- set/属性注入 -->
		<!-- 
			使用SpringEL表达式可以读取配置文件的内容
			#{}  : #{3600 * 24 * 7}
			${}  : ${xx.properties配置文件中的key}
			
			
			druid 连接池的 配置的的key 不能是username
				因为默认读取的时候当前操作系统登录的账号,不是 配置文件中username的值
			
		 -->
		<property name="driverClassName" value="${jdbc.driverClassName}"/>
		<property name="url" value="${jdbc.url}"/>
		<property name="username" value="${jdbc.username}"/>
		<property name="password" value="${jdbc.password}"/>
		<property name="maxActive" value="${jdbc.maxActive}"/>
	</bean>
	
	
	<bean scope="prototype" id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<constructor-arg name="dataSource" ref="dataSource"/>
	</bean>
	
	
	<!-- ========================================== -->
	
	<!-- 事务配置
		
		1,配置事务管理器 (what?)
		2,配置事务通知/增强 (when ?)
		3,使用AOP将事务切入Service层 (where?)
	
	 -->
	
	
	<!-- 1,配置事务管理器 (what?) -->
	
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		
		<!-- 注入数据源 -->
		<property name="dataSource" ref="dataSource"/>
	
	</bean>
	
	
	<!-- 2,配置事务通知/增强 (when ?)
	
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		id : 唯一标示
		transaction-manager :事务管理器
	 -->
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		
		<!-- 配置事务属性 -->
		<tx:attributes>
			
			<!-- 配置事务要具体处理的方法 
			<tx:method name="" isolation="DEFAULT" propagation="REQUIRED" read-only="false" timeout="-1" />
				name : 要处理事务的方法名 ,(当前案例的场景方法就是  Service层 trans)
					针对DML,DQL 不同的多个方法,可以使用通配符 :  *
					
				
				isolation : 配置事务的隔离级别
				propagation :传播规则
				read-only : 配置是否是只读事务,默认是false
					true :是只读事务,DQL ,性能高一些
					false :非只读事务,一定要处理事务 , DML
				timeout : 事务等待超时管理  5
			-->
<!-- 	<tx:method name="trans" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" timeout="5" /> -->
			
			
			<!-- DQL :查询 -->
			<tx:method name="select*" read-only="true" isolation="REPEATABLE_READ" propagation="REQUIRED"  timeout="5" />
			<tx:method name="get*" read-only="true" isolation="REPEATABLE_READ" propagation="REQUIRED"  timeout="5" />
			<tx:method name="query*" read-only="true" isolation="REPEATABLE_READ" propagation="REQUIRED"  timeout="5" />
			<tx:method name="find*" read-only="true" isolation="REPEATABLE_READ" propagation="REQUIRED"  timeout="5" />
			
			
			<!-- DML :  增删改 -->
			<tx:method name="*" read-only="false" isolation="REPEATABLE_READ" propagation="REQUIRED"  timeout="5" />
		
			
		</tx:attributes>
	
	</tx:advice>
	
	
	<!-- 3,使用AOP将事务切入Service层 (where?) -->
	<aop:config>
		
		<!-- 配置切入点 -->
		<aop:pointcut expression="execution(* cn.zj.spring.service..*.*(..))" id="pt"/>
		
		
		<!-- 切面  = 切入点 +通知
		
		<aop:aspect></aop:aspect> : 配置开发自定义的切面
		
		<aop:advisor advice-ref=""/> : 配置事务管理的切面,默认使用环绕通知
			advice-ref : 事务通知的引用
			pointcut-ref :切入点引用
		-->
		
		<aop:advisor advice-ref="txAdvice" pointcut-ref="pt" />
		<!-- <aop:advisor advice-ref="txAdvice" pointcut="execution(* cn.zj.spring.service..*.*(..))"/> -->
		
	</aop:config>
</beans>

第七步:测试代码

package cn.zj.spring.test;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import cn.zj.spring.service.UserService;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class UserServiceTest {

	@Autowired
	UserService userService;
	
	@Test
	public void testInsert() {
		
		userService.trans(10086, 10010, 1000);
		
	}


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring配置声明式事务可以使用XML配置文件。下面是一个示例: 首先,在Spring配置文件中引入tx命名空间,如下所示: ```xml <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="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.xsd"> <!-- 其他配置 --> </beans> ``` 接下来,为了启用声明式事务,需要配置事务管理器和事务通知器。示例如下: ```xml <!-- 配置数据源 --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <!-- 数据源配置 --> </bean> <!-- 配置事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- 配置事务通知器 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="*" propagation="REQUIRED" /> </tx:attributes> </tx:advice> <!-- 配置切面 --> <aop:config> <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.example.service.*.*(..))" /> </aop:config> ``` 在这个示例中,我们配置了一个数据源、一个事务管理器和一个事务通知器。事务通知器定义了事务的传播行为为REQUIRED,这意味着如果当前没有事务,则创建一个新的事务;如果当前存在事务,则加入到当前事务中。 最后,通过使用aop:config配置切面,指定了需要应用事务的目标方法。在这个示例中,我们将事务应用到了`com.example.service`包下的所有方法。 请注意,这只是一个简单的示例,实际的配置可能会根据具体需求有所不同。你也可以通过注解方式来配置声明式事务,具体可以参考Spring官方文档。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值