spring源码TX 事务环境准备

21 篇文章 1 订阅
3 篇文章 0 订阅

源码构建

准备事务环境之前,必须要保证aop环境

gradle配置
dependencies {
    compile(project(":spring-beans"))
    compile(project(":spring-core"))
    compile(project(":spring-context"))
    compile(project(":spring-webmvc"))
    compile(project(":spring-jdbc"))
    compile(project(":spring-orm"))
    compile(project(":spring-tx"))
    compile(project(":spring-web"))
    compile(project(":spring-context-indexer"))
    compile(project(":spring-context-support"))
    compile(project(":spring-expression"))
    compile(project(":spring-instrument"))
    compile(project(":spring-jcl"))
    compile(project(":spring-jms"))
    compile(project(":spring-messaging"))
    compile(project(":spring-oxm"))
    compile(project(":spring-test"))
    compile(project(":spring-webflux"))
    compile(project(":spring-websocket"))
    compile(project(":spring-aspects"))
    compile("org.aspectj:aspectjweaver:1.9.6")
    compile(project(":spring-aop"))
//    -----------导入mysql驱动包,数据库连接池-------------
    compile 'mysql:mysql-connector-java:5.1.47'
    compile 'com.alibaba:druid:1.1.14'
    testCompile group: 'junit', name: 'junit', version: '4.12'
}
刷新

在这里插入图片描述

编写类

这里使用的注解,如果使用xml注入可以选择set或构造都可以

@Service
public class UserServiceImpl implements UserService {

	@Autowired
	private JdbcTemplate jdbcTemplate;

	public void get(int id){
		String sql = "select c_phoneno from t_user where n_id=?";
		String phoneno = jdbcTemplate.queryForObject(sql, String.class, id);
		System.out.println("获取用户。。。。" + phoneno);
	}

	public void add(int i){
		System.out.println("增加用户。。。。" + i);
	}

	@Transactional
	public void update(int id){
		String sql = "update t_user set c_phoneno='15966666666' where n_id=?";
		jdbcTemplate.update(sql,id);
//		int i = 1/0;
		System.out.println("更新用户手机号。。。。");
	}

	public void delete(int i){
		System.out.println("删除书籍。。。。");
	}
}

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
       https://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/aop
       https://www.springframework.org/schema/aop/spring-aop.xsd
       http://www.springframework.org/schema/tx
       https://www.springframework.org/schema/tx/spring-tx.xsd
">
	<context:component-scan base-package="com.msgqu.debug.tx"/>
	<context:property-placeholder location="application.properties"/>
	<!-- 配置bean -->
	<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
		<property name="username" value="${jdbc.username}"></property>
		<property name="password" value="${jdbc.password}"></property>
		<property name="url" value="${jdbc.url}"></property>
		<property name="driverClassName" value="${jdbc.driverName}"></property>
	</bean>

	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource" ref="dataSource"></property>
	</bean>

	<!--事务的配置-->
	<!--声明一个事务管理器-->
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"></property>
	</bean>

	<aop:config>
		<aop:pointcut id="txPointcut" expression="execution(* com.msgqu.debug.tx.*.*(..))"/>
		<!--事务建议-->
		<aop:advisor advice-ref="myAdvice" pointcut-ref="txPointcut"></aop:advisor>
	</aop:config>
	<tx:advice id="myAdvice" transaction-manager="transactionManager">
		<!--配置事务的属性-->
		<tx:attributes>
			<!--配置在哪些方法上添加事务-->
<!--			<tx:method name="*" propagation="REQUIRED" read-only="true" isolation="DEFAULT"/>-->
			<tx:method name="add*" propagation="REQUIRED"></tx:method>
			<tx:method name="update*" propagation="REQUIRED"></tx:method>
			<tx:method name="delete*" propagation="REQUIRED"></tx:method>
		</tx:attributes>
	</tx:advice>
</beans>
验证
public class TxTest {
	public static void main(String[] args) {
		MyClassPathXmlApplicationContext ac = new MyClassPathXmlApplicationContext("tx.xml");
		UserService userService = ac.getBean(UserService.class);
		userService.get(1);
		userService.update(1);
		ac.close();
	}
}

事务的环境配置到这里就完成了~~~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Spring 是一款流行的 Java 开发框架,它提供了许多便利的功能,包括对事务管理的支持。Spring事务管理是建立在底层事务管理器之上的,它为开发人员提供了一种简单的方式来处理事务的提交、回滚、保存点等操作。 Spring 支持两种类型的事务管理:编程式事务管理和声明式事务管理。 1. 编程式事务管理 编程式事务管理需要在代码中显式地开启、提交或回滚事务。开发人员需要在代码中使用 Spring 提供的事务模板(TransactionTemplate)类来管理事务事务模板类的主要方法包括: - execute():执行一段代码,并在其中进行事务管理。 - execute(TransactionCallback):执行一个事务回调,该回调负责实际执行数据库操作。 - execute(TransactionCallbackWithoutResult):执行一个没有返回值的事务回调。 使用事务模板类的代码示例: ``` @Autowired private DataSource dataSource; @Transactional public void transferMoney(Account fromAccount, Account toAccount, double amount) { TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager); transactionTemplate.execute(new TransactionCallbackWithoutResult() { protected void doInTransactionWithoutResult(TransactionStatus status) { try { // 在这里执行数据库操作 // ... } catch (Exception e) { status.setRollbackOnly(); } } }); } ``` 2. 声明式事务管理 声明式事务管理是通过在 Spring 的配置文件中声明事务的属性来管理事务的。这种方式将事务管理的代码从业务代码中分离出来,使代码更加清晰。 声明式事务管理的实现方式是使用 Spring 的 AOP(Aspect Oriented Programming)技术,在代码执行前、后或异常时插入事务管理的代码。在 Spring 中,声明式事务管理主要有两种方式:基于注解和基于 XML 配置。 使用基于注解的声明式事务管理,可以在方法上添加 @Transactional 注解来指定事务的属性,示例代码: ``` @Autowired private DataSource dataSource; @Transactional public void transferMoney(Account fromAccount, Account toAccount, double amount) { // 在这里执行数据库操作 // ... } ``` 使用基于 XML 配置的声明式事务管理,需要在配置文件中定义事务管理器、事务通知等元素,示例代码: ``` <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="transferMoney" propagation="REQUIRED" /> </tx:attributes> </tx:advice> <aop:config> <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.example.*.*(..))" /> </aop:config> ``` 以上就是 Spring 事务管理的基本概念和使用方法。在实际应用中,根据具体业务需 ### 回答2: Spring 事务管理是指在Spring框架中对数据库操作进行事务管理的一种机制。在数据库操作中,可能会涉及到多个操作,而事务管理可以确保这些操作要么全部成功,要么全部失败,从而保持数据的一致性。 Spring 事务管理的核心原理是基于AOP(面向切面编程)和IOC(控制反转)两个核心概念,通过在方法或类上添加@Transactional注解来实现事务的控制。 Spring 事务管理的特点如下: 1. 声明式事务管理:可以通过使用@Transactional注解来声明事务,而不需要在代码中显式地编写事务管理代码,简化了操作和维护。 2. 事务的传播特性:可以通过设置事务的传播特性来控制事务的范围和行为,在多个事务操作中灵活进行事务的传递和管理。 3. 事务的隔离级别:可以通过设置事务的隔离级别来控制多个事务之间的隔离程度,从而保证在并发环境下的数据一致性和并发控制。 4. 事务的回滚机制:可以通过设置事务的异常回滚策略,来控制事务在出现异常情况下的回滚和恢复,确保数据的完整性。 5. 多数据事务管理:Spring支持同时管理多个数据事务,可以保证多个数据之间的数据一致性。 总的来说,Spring事务管理提供了一个方便和可靠的机制,用于管理数据库操作的事务,减少了代码的冗余和复杂度,提高了开发效率和数据的一致性。同时,Spring事务管理的可扩展性和灵活性也使其适用于不同的应用场景和需求。 ### 回答3: Spring事务管理是指通过Spring框架提供的事务管理功能来管理数据库事务的机制。Spring事务管理可以确保数据的一致性和完整性,同时也可以提高数据库的性能和并发能力。 Spring事务管理的特点包括: 1. 声明式事务管理:通过在配置文件或注解中声明事务的属性和规则来管理事务,而不需要手动编写事务管理的代码。这样可以有效地降低代码的复杂性和维护成本。 2. 支持多种事务管理策略:Spring事务管理支持多种事务管理策略,包括本地事务管理和分布式事务管理。可以根据具体的应用场景选择不同的事务管理策略。 3. 支持多种事务传播行为:Spring事务管理支持多种事务传播行为,包括REQUIRED、REQUIRES_NEW、NESTED等。可以根据具体的业务需求来选择不同的事务传播行为。 4. 异常回滚和异常捕获:Spring事务管理可以根据配置的属性来决定事务的回滚行为,包括回滚特定的异常和回滚全部的异常。同时,也可以通过异常捕获来处理事务中的异常情况。 5. 提供事务管理的模板:Spring框架提供了用于事务管理的模板类,可以方便地进行事务管理的操作,如开启事务、提交事务、回滚事务等。 总之,Spring事务管理是一种方便、灵活且可扩展的事务管理机制,可以帮助开发人员简化事务管理的操作,提高代码的可读性和可维护性。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值