SpringBoot系列(五)SpringBoot 之Spring 事务管理 和 切面 AOP

本系列博客将学习开发SpringBoot,快速开发项目


SpringBoot系列 (五):SpringBoot 之Spring 事务管理 和 切面 AOP

文档结构

  1. SpringBoot 之事务管理@Transactional
  2. SpringBoot 之切面 AOP

一、 SpringBoot 之事务管理@Transactional

通过在service方法中加上@Transactional注解实现事务管理。

package com.tofree.service;

/**
 * 账户Service接口
 * @author tofree
 *
 */
public interface AccountService {

/**
 * 从A用户转账b用户钱
 * @param fromUser
 * @param toUser
 * @param account
 */
public void transferAccounts(int fromUser,int toUser,float account);
}

实现方法:

package com.tofree.service.impl;

import javax.annotation.Resource;
import javax.transaction.Transactional;

import org.springframework.stereotype.Service;

import com.tofree.dao.AccountDao;
import com.tofree.entity.Account;
import com.tofree.service.AccountService;

/**
 * 帐号Service实现类
 * @author tofree
 *
 */
@Service("accountService")
public class AccountServiceImpl implements AccountService{

@Resource
private AccountDao accountDao;

@Transactional
public void transferAccounts(int fromUser, int toUser, float account) {
    Account fromAccount=accountDao.getOne(fromUser);
    fromAccount.setBalance(fromAccount.getBalance()-account);
    accountDao.save(fromAccount);

    Account toAccount=accountDao.getOne(toUser);
    toAccount.setBalance(toAccount.getBalance()+account);
    int zero=1/0;
    accountDao.save(toAccount);
}

}

二、 SpringBoot 之切面 AOP

package com.tofree.aspect;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.tofree.entity.Student;

@Aspect
@Component
public class RequestAspect {

private Logger logger=Logger.getLogger(RequestAspect.class);

@Pointcut("execution(public * com.tofree.controller.*.*(..))")
public void log(){
}

@Before("log()")
public void doBefore(JoinPoint joinPoint){
    logger.info("方法执行前...");
    ServletRequestAttributes sra=(ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
    HttpServletRequest request=sra.getRequest();
    logger.info("url:"+request.getRequestURI());
    logger.info("ip:"+request.getRemoteHost());
    logger.info("method:"+request.getMethod());
    logger.info("class_method:"+joinPoint.getSignature().getDeclaringTypeName()+"."+joinPoint.getSignature().getName());
    logger.info("args:"+joinPoint.getArgs());
    Student student=(Student) joinPoint.getArgs()[0];
    System.out.println(student);
}

@After("log()")
public void doAfter(JoinPoint joinPoint){
    logger.info("方法执行后...");
}

@AfterReturning(returning="result",pointcut="log()")
public void doAfterReturning(Object result){
    logger.info("方法返回值:"+result);
}
}

你可能感兴趣的:(SpringBoot系列(五)SpringBoot 之Spring 事务管理 和 切面 AOP)