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();
	}
}

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值