springboot使用自定义注解实现aop切面日志
作者:原野灬 时间:2023-11-11 09:14:48
平时我们在开发过程中,代码出现bug时为了更好的在服务器日志中寻找问题根源,会在接口的首尾打印日志,看下参数和返回值是否有问题。但是手动的logger.info() 去编写时工作量较大,这时我们可以使用AOP切面,为所有接口的首尾打印日志。
实现AOP切面日志一般有两种方式:
1、拦截所有接口controller,在首尾打印日志
2、拦截指定注解的接口,为有该注解的接口首尾打印日志
我们尝试用自定义注解来实现AOP日志的打印,这样拥有更高的灵活性。废话不多说,我们开始
1. 导入切面需要的依赖包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
2. 自定义注解 AOPLog , 指定注解使用在方法上, 指定在运行时有效
Target:描述了注解修饰的对象范围
METHOD:用于描述方法
PACKAGE:用于描述包
PARAMETER:用于描述方法变量
TYPE:用于描述类、接口或enum类型
Retention: 表示注解保留时间长短
SOURCE:在源文件中有效,编译过程中会被忽略
CLASS:随源文件一起编译在class文件中,运行时忽略
RUNTIME:在运行时有效
只有定义为 RetentionPolicy.RUNTIME(在运行时有效)时,我们才能通过反射获取到注解,然后根据注解的一系列值,变更不同的操作。
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* @author buer
* @date 2019/12/26
*/
// 指定注解使用在方法上
@Target(ElementType.METHOD)
// 指定生效至运行时
@Retention(RetentionPolicy.RUNTIME)
public @interface AOPLog {
/**
* 指定是否详情显示
* true 显示详情, 默认false
*
* @return
*/
boolean isDetail() default false;
}
3. 设置切面类
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
/**
* @author buer
* @date 2019/12/26
* @description //TODO
*/
// 指定切面类
@Aspect
// 注入容器
@Component
public class AOPLogAspect {
private static Logger log = LoggerFactory.getLogger(AOPLogAspect.class);
/**
* 指定切点, 切点的位置是存在该注解com.xingyun.xybb.demo.annotation.AOPLog
*/
@Pointcut("@annotation(com.xingyun.xybb.demo.annotation.AOPLog)")
public void logPointCut() {
}
/**
* 环绕通知, 该处写具体日志逻辑
*
* @param joinPoint
*/
@Around("logPointCut()")
public void logAround(ProceedingJoinPoint joinPoint) {
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
// 获取方法名称
String methodName = signature.getName();
// 获取入参
Object[] param = joinPoint.getArgs();
StringBuilder sb = new StringBuilder();
for (Object o : param) {
sb.append(o).append("; ");
}
log.info("进入方法[{}], 参数有[{}]", methodName, sb.toString());
String resp = "";
try {
Object proceed = joinPoint.proceed();
resp = JSON.toJSONString(proceed, SerializerFeature.WriteMapNullValue);
} catch (Throwable throwable) {
throwable.printStackTrace();
}
// 获取方法上的注解,判断如果isDetail值为true,则打印结束日志
Method method = signature.getMethod();
AOPLog annotation = method.getAnnotation(AOPLog.class);
boolean isDetail = annotation.isDetail();
if (isDetail) {
log.info("方法[{}]执行结束, 返回值[{}]", methodName, resp);
}
}
}
4. 编写测试接口, 测试切面日志是否生效
import com.xingyun.xybb.common.response.XyResponseEntity;
import com.xingyun.xybb.demo.annotation.AOPLog;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author buer
* @date 2019/12/26
* @description //TODO
*/
@RestController
public class TestAOPLogController {
// 指定注解@AOPLog
@AOPLog
@GetMapping("/testAOP")
public ResponseEntity<?> testAOPLog() {
return XyResponseEntity.ok();
}
// 指定注解@AOPLog, 同时isDetail = true
@AOPLog(isDetail = true)
@GetMapping("/testAOPLogDetail")
public ResponseEntity<?> testAOPLogDetail() {
return XyResponseEntity.ok();
}
}
5. 分别请求两测试接口
http://localhost:8499/demo/testAOP
http://localhost:8499/demo/testAOPLogDetail
控制台打印出
2019-12-26 14:00:56.336 ***.AOPLogAspect : 进入方法[testAOPLog], 参数有[]
2019-12-26 14:01:00.372 ***.AOPLogAspect : 进入方法[testAOPLogDetail], 参数有[]
2019-12-26 14:01:00.373 ***.AOPLogAspect : 方法[testAOPLogDetail]执行结束, 返回值[{"body":{"retCode":200,"retEntity":null,"retMsg":"OK"},"headers":{},"statusCode":"OK","statusCodeValue":200}]
由此可看出,AOP切面拦截成功,打印出了日志,同时设置了 isDetail = true 时,打印出了结束日志。
自定义注解实现AOP切面打印日志完成。
来源:https://blog.csdn.net/lp2388163/article/details/103714079
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
spring+srpingmvc+hibernate实现动态ztree生成树状图效果
基于spring AOP @Around @Before @After的区别说明
![](https://img.aspxhome.com/file/2023/6/81966_0s.jpg)
C#之CLR内存深入分析
![](https://img.aspxhome.com/file/2023/4/108764_0s.png)
spring mvc实现登录账号单浏览器登录
Android仿微信雷达扫描效果的实现方法
![](https://img.aspxhome.com/file/2023/9/95989_0s.gif)
Android中退出确认框的实现代码
![](https://img.aspxhome.com/file/2023/9/100629_0s.jpg)
Java Fluent Mybatis 项目工程化与常规操作详解流程篇 上
![](https://img.aspxhome.com/file/2023/0/100670_0s.png)
Android使用TransitionDrawable渐变切换多张图片
Java动态数组Arraylist存放自定义数据类型方式
![](https://img.aspxhome.com/file/2023/3/66663_0s.png)
java如何给对象按照字符串属性进行排序
Java判断所给年份是平年还是闰年
SpringBoot集成Spring Security用JWT令牌实现登录和鉴权的方法
![](https://img.aspxhome.com/file/2023/7/60347_0s.png)
Android自定义View Flyme6的Viewpager指示器
![](https://img.aspxhome.com/file/2023/6/138206_0s.gif)
SSH原理及两种登录方法图文详解
![](https://img.aspxhome.com/file/2023/8/59038_0s.png)
JAVA中AES加密方法实例分析
WebSocket实现Web聊天室功能
![](https://img.aspxhome.com/file/2023/9/60189_0s.jpg)
Android使用剪切板传递数据
谈一谈Android内存泄漏问题
Android WebView基础应用详解
Java-String类最全汇总(上篇)
![](https://img.aspxhome.com/file/2023/1/83571_0s.jpg)