一般拦截器 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());
}
}