配置文件
<?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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
https://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.czy"></context:component-scan>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.MysqL.cj.jdbc.Driver"></property>
<property name="url" value="jdbc:MysqL://localhost:3306/eesy?serverTimezone=UTC"></property>
<property name="username" value="root"></property>
<property name="password" value="QWEasdZXC9377616"></property>
</bean>
<bean id="template" class="org.springframework.jdbc.core.JdbcTemplate" scope="prototype">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--
spring中基于注解的声明式事务控制配置步骤
1.配置事务管理器
2.开启spring对于注解事务的支持
3.在需要事务支持的地方使用@Transactional注解
-->
<!--配置事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--开启spring对注解事务的支持-->
<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
<aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy>
</beans>
service
package com.czy.service.impl;
import com.czy.dao.AccountDao;
import com.czy.domain.Account;
import com.czy.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
@Service("accountService")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class AccountServiceImpl implements AccountService {
@Autowired
private AccountDao dao;
public Account findAccountById(Integer accountId) {
return dao.findAccountById(accountId);
}
@Transactional(propagation = Propagation.required, readOnly = false)
public void transfer(String sourceName, String targetName, Float money) {
Account source = dao.findAccountByName(sourceName);
Account target = dao.findAccountByName(targetName);
source.setMoney(source.getMoney() - money);
dao.updateAccount(source);
int i = 1/0;
target.setMoney(target.getMoney() + money);
dao.updateAccount(target);
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。