spring aop日志拦截

1.日志拦截类


package com.zebra.carcloud.trip.aspect;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import javassist.ClassClassPath;
import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtMethod;
import javassist.Modifier;
import javassist.NotFoundException;
import javassist.bytecode.CodeAttribute;
import javassist.bytecode.LocalVariableAttribute;
import javassist.bytecode.MethodInfo;

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.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import com.zebra.carcloud.trip.annotation.ParamAnonymous;
import com.zebra.carcloud.trip.utils.ReqIdUtil;
import com.zebra.carcloud.trip.utils.StringUtil;


@Aspect
@Component
@Order(100)
public class LogAspect {
static Logger logger = LoggerFactory.getLogger(LogAspect.class);
@Pointcut("execution(* com.zebra.carcloud.trip.service.*.*.*(..))")
public void executeService(){
}
@Before("executeService()")
public void logBefore(JoinPoint joinPoint) {
try {
if(ReqIdUtil.getReqId() == null) {
ReqIdUtil.setReqId(ReqIdUtil.getReqIdForService());
}
StringBuilder startLog = new StringBuilder(joinPoint.getTarget().getClass().getName())
.append(" ****").append(joinPoint.getSignature().getName()).append(" 方法执行开始...**** REQ_ID:").append(ReqIdUtil.getReqId()).append(" 参数为:");

//获取拦截接口方法
MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
Method method = methodSignature.getMethod();
String[] anonymousParams = null;
if (method.isAnnotationPresent(ParamAnonymous.class)) {
ParamAnonymous data = method.getAnnotation(ParamAnonymous.class);
anonymousParams = data.params();
}
String[] paramName = getFieldsName(this.getClass(),joinPoint.getTarget().getClass().getName(),methodSignature.getMethod().getName());
Map<String,Object> paramMap = new HashMap<String,Object>();
int idx = 0;
Object[] values = joinPoint.getArgs();
for(String name : paramName) {
if(anonymousParams == null || Arrays.binarySearch(anonymousParams, name) < 0) {
paramMap.put(name,values[idx++]);
}
}
logger.info(startLog.append(StringUtil.getJsonByObj(paramMap)).toString());
startLog = null;
} catch(Exception e) {

}

}
@After("executeService()")
public void logAfter(JoinPoint joinPoint) {
logger.info(new StringBuilder(joinPoint.getTarget().getClass().getName())
.append(" ****").append(joinPoint.getSignature().getName()).append(" 方法执行结束...**** REQ_ID:").append(ReqIdUtil.getReqId()).toString());
}
/**
* 得到方法参数的名称
* @throws NotFoundException
*/
private static String[] getFieldsName(Class<?> cls, String clazzName, String methodName) throws NotFoundException{
ClassPool pool = ClassPool.getDefault();
ClassClassPath classPath = new ClassClassPath(cls);
pool.insertClassPath(classPath);

CtClass cc = pool.get(clazzName);
CtMethod cm = cc.getDeclaredMethod(methodName);
MethodInfo methodInfo = cm.getMethodInfo();
CodeAttribute codeAttribute = methodInfo.getCodeAttribute();
LocalVariableAttribute attr = (LocalVariableAttribute) codeAttribute.getAttribute(LocalVariableAttribute.tag);
String[] paramNames = new String[cm.getParameterTypes().length];
int pos = Modifier.isStatic(cm.getModifiers()) ? 0 : 1;
for (int i = 0; i < paramNames.length; i++){
paramNames[i] = attr.variableName(i + pos);
}
return paramNames;
}
}



2.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值