其他分享
首页 > 其他分享> > 【Spring事务控制】基于注解的声明式事务控制

【Spring事务控制】基于注解的声明式事务控制

作者:互联网

配置文件

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

标签:控制,事务,czy,target,Spring,dao,Account,source,import
来源: https://www.cnblogs.com/czy-algorithm/p/16183102.html