idea-转账案例2-java-Spring-AOP-service层

接口

package cn.csy.account.service;

import java.math.BigDecimal;

public interface AccountService {
    
    
    public void transBalance(Long from, Long to, BigDecimal balance);
}

使用注解的方式

实现类

import cn.csy.account.mapper.AccountMapper;
import cn.csy.account.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;

import java.math.BigDecimal;

@Service
/*给方法添加注解,使配置文件可以读取这个方法*/
    /*注意,如果有多个方法,想让那个方法作为切点,那么就在那个方法上加上此注解*/
    /*可以写在方法上,表示这个方法有事务*/
    /*也可以写在类上,表示这个类中的所有方法都有事务*/
    /*@Transactional(propagation = Propagation.REQUIRED)*/
@Transactional(propagation = Propagation.REQUIRED)
public class AccountServiceImpl implements AccountService {
    
    
    @Autowired
    private AccountMapper accountMapper;
    @Override
    public void transBalance(Long from, Long to, BigDecimal balance) {
    
    
        accountMapper.subBalance(balance,from);
        accountMapper.addBalance(balance,to);
    }
}

service.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:aop="http://www.springframework.org/schema/aop" 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/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
     <context:component-scan base-package="cn.csy.account.service"></context:component-scan>
     <!--解析在实现类中的transational注解,读取所有的切点-->
     <tx:annotation-driven></tx:annotation-driven>
     <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
          <property name="dataSource" ref="dataSource"></property>
     </bean>
</beans>

使用配置文件的方式

实现类

package cn.csy.account.service.impl;

import cn.csy.account.mapper.AccountMapper;
import cn.csy.account.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;

import java.math.BigDecimal;

@Service
public class AccountServiceImpl implements AccountService {
    
    
    @Autowired
    private AccountMapper accountMapper;
    @Override
    public void transBalance(Long from, Long to, BigDecimal balance) {
    
    
        accountMapper.subBalance(balance,from);
        accountMapper.addBalance(balance,to);
    }
}

service.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:aop="http://www.springframework.org/schema/aop" 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/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
     <context:component-scan base-package="cn.csy.account.service"></context:component-scan>
     <!--解析在实现类中的transational注解,读取所有的切点-->
     <tx:annotation-driven></tx:annotation-driven>
     <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
          <property name="dataSource" ref="dataSource"></property>
     </bean>

     <!--
          这里是配置方式
          还可以有注解方式(在实现类中加入注解)
          如上
     -->
     <tx:advice id="myAdvice" transaction-manager="transactionManager">
          <tx:attributes>
               <tx:method name="*"/>
          </tx:attributes>
     </tx:advice>
     <aop:config>
          <aop:pointcut id="myTxMethod" expression="execution(* cn.csy.account.service.impl.*.*(..))"/>
          <aop:advisor advice-ref="myAdvice" pointcut-ref="myTxMethod"/>
     </aop:config>

</beans>

猜你喜欢

转载自blog.csdn.net/weixin_44939526/article/details/109051156