一:写一个自定义注解TrailEnabled
package com.agency.admin.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Created by shenguoliang on 2019/2/22 0022.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
public @interface TrailEnabled{String title() default "";
}
二:切面拦截,具体操作
package com.agency.admin.aspect;
import com.agency.admin.annotation.TrailEnabled;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
/**
* Created by shenguoliang on 2019/2/22 0022.
*/
@Aspect
@Component("logAspect")
@Slf4j
public class LogAspect {
// 配置织入点
@Pointcut("@within(com.agency.admin.annotation.TrailEnabled)")
public void logPointCut() {
}/**
* 前置通知 用于拦截操作,在方法返回后执行
* @param joinPoint 切点
*/
@AfterReturning(pointcut = "logPointCut()")
public void doBefore(JoinPoint joinPoint) {
handleLog(joinPoint, null);
}/**
* 拦截异常操作,有异常时执行
*
* @param joinPoint
* @param e
*/
@AfterThrowing(value = "https://www.it610.com/article/logPointCut()", throwing = "e")
public void doAfter(JoinPoint joinPoint, Exception e) {
handleLogE(joinPoint, e);
}private void handleLog(JoinPoint joinPoint, Exception e) {
try {
// 获得注解
// 获得注解
TrailEnabled controllerLog = getAnnotationLog(joinPoint);
if (controllerLog == null) {
return;
}
// 参数名
String[] argNames = ((MethodSignature)joinPoint.getSignature()).getParameterNames();
//传入的参数
Object[] arguments = joinPoint.getArgs();
// 获得方法名称
String className = joinPoint.getTarget().getClass().getName();
String methodName = joinPoint.getSignature().getName();
String title = controllerLog.title();
//打印日志,如有需要还可以存入数据库
log.info("--参数名--:" + JSON.toJSONString(argNames));
log.info("--参数值--:" + JSON.toJSONString(arguments));
log.info(">>>>>>>>>>>>>模块名称:{}",title);
log.info(">>>>>>>>>>>>>类名:{}",className);
log.info(">>>>>>>>>>>>>方法名:{}",methodName);
//这里可以根据需求把日志存入数据库...} catch (Exception exp) {
// 记录本地异常日志
log.error("==前置通知异常==");
log.error("异常信息:{}", exp.getMessage());
exp.printStackTrace();
}
}private void handleLogE(JoinPoint joinPoint, Exception e) {
try {
// 获得注解
TrailEnabled controllerLog = getAnnotationLog(joinPoint);
if (controllerLog == null) {
return;
}
// 获得方法名称
String className = joinPoint.getTarget().getClass().getName();
String methodName = joinPoint.getSignature().getName();
String title = controllerLog.title();
//打印日志,如有需要还可以存入数据库
log.info(">>>>>>>>>>>>>模块名称:{}",title);
log.info(">>>>>>>>>>>>>类名:{}",className);
log.info(">>>>>>>>>>>>>方法名:{}",methodName);
} catch (Exception exp) {
// 记录本地异常日志
log.error("==前置通知异常==");
log.error("异常信息:{}", exp.getMessage());
exp.printStackTrace();
}
}/**
* 是否存在注解,如果存在就获取
*/
private static TrailEnabled getAnnotationLog(JoinPoint joinPoint) throws Exception {
//如果你的自定义注解是在方法上,可以这样判断
/*Signature signature = joinPoint.getSignature();
MethodSignature methodSignature = (MethodSignature) signature;
Method method = methodSignature.getMethod();
if (method != null) {
return method.getAnnotation(TrailEnabled.class);
}*/
//自定义注解在controller层类上
Signature signature = joinPoint.getSignature();
Class cls = signature.getDeclaringType();
boolean isTrailEnabled = cls.isAnnotationPresent(TrailEnabled.class);
if(isTrailEnabled){
//类名前注解
return (TrailEnabled)cls.getAnnotation(TrailEnabled.class);
}else{
return null;
}
}}
三:前面两步都写好了之后,只需要在你想拦截的controller 类上加上你第一步创建的自定义注解@TrailEnabled
【spring boot 使用 Aspect aop切面,自定义注解 拦截请求实现操作日志打印】就好了。
文章图片
编译没问题然后就可以启动项目测试一下效果;
如图:
文章图片