一般拦截器 serviceImpl部分

一般拦截器 serviceImpl部分

package com.chinamobile.scm.masterdata.interceptor;

import com.chinamobile.framework.common.context.InvokeTracer;
import com.chinamobile.framework.common.context.RequestContext;
import com.chinamobile.framework.utils.CollectionUtil;
import com.chinamobile.scm.masterdata.util.ThreadsMapUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.ThreadUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;


import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.transaction.support.TransactionSynchronizationAdapter;
import org.springframework.transaction.support.TransactionSynchronizationManager;

import java.lang.reflect.Method;
import java.util.Deque;
import java.util.List;

/**
 * Intercept implementation for service component.<br>
 *
 * @author YangHang
 */
@Component
@Aspect
@Slf4j
public class MybatisServiceInterceptor {
    /**
     * Service切入点<br>
     */
    @Pointcut("execution(* com.chinamobile.scm.*.service.*.*(..)) || execution(* com.chinamobile.scm.service.*.*(..))")
    public void pointCut() {
    }

    /**
     * 后置异常通知
     */

    @AfterThrowing(pointcut = "pointCut()", throwing = "e")
    public void throwss(JoinPoint joinPoint, Throwable e) {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();

        System.out.println("拦截方法异常时执行 本方法内不可事务 异常:"+method.getName());
    }
    @AfterReturning("pointCut()")
    public void afterreturning(JoinPoint joinPoint){
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        if(TransactionSynchronizationManager.isActualTransactionActive()) {
            String tid = String.valueOf(Thread.currentThread().getId());
          List<String> stringList= ThreadsMapUtil.getSqls(tid);
          if(CollectionUtils.isNotEmpty(stringList))
          {
             for(String item:stringList)
             {
                 log.info("SQL:"+item);
             }
          }
            ThreadsMapUtil.removeSQL(tid);
        }
        System.out.println("注解式拦截  本方法内可提交事务 返回"+method.getName());
    }
    @After("pointCut()")
    public void after(JoinPoint joinPoint){
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
//
//            if(TransactionSynchronizationManager.isActualTransactionActive()) {
//                TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {
//                    @Override
//                    public void afterCommit() {
//                        System.out.println("send email after transaction commit...");
//                    }
//
//                });
//            }
        System.out.println("注解式拦截,结束"+method.getName());
    }
    @Before("pointCut()")
    public void before(JoinPoint joinPoint){
        Thread.currentThread().getId();
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        System.out.println("方法规则式拦截,开始:"+method.getName());

    }




}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

杨航 AI

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值