当前位置: 首页 > news >正文

AOP 切面日志详细

1. 引入 spring-aspects

<!-- pom.xml 追加 -->
<dependency><groupId>org.springframework</groupId><artifactId>spring-aspects</artifactId><version>5.3.37</version>
</dependency>

mvn clean compile 确保没冲突。


2. 自定义注解

package com.lib.annotation;import java.lang.annotation.*;@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface LogExecution {String description() default "";   // 可选业务描述
}

3. 编写切面类

package com.lib.aspect;@Aspect
@Component
public class LoggingAspect {// 1. 定义切点:所有带 @LogExecution 的方法@Pointcut("@annotation(logAnnotation)")public void logPointcut(LogExecution logAnnotation) {}// 2. 环绕通知@Around("logPointcut(logAnnotation)")public Object logAround(ProceedingJoinPoint pjp,LogExecution logAnnotation) throws Throwable {long start = System.nanoTime();String className = pjp.getTarget().getClass().getSimpleName();String methodName = pjp.getSignature().getName();try {Object result = pjp.proceed();          // 继续执行原方法long cost = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start);System.out.printf("[AOP-LOG] %s.%s | desc=%s | cost=%d ms | status=SUCCESS%n",className, methodName, logAnnotation.description(), cost);return result;} catch (Exception ex) {long cost = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start);System.out.printf("[AOP-LOG] %s.%s | desc=%s | cost=%d ms | status=ERROR | msg=%s%n",className, methodName, logAnnotation.description(), cost, ex.getMessage());throw ex;   // 继续抛出}}
}

文章转载自:

http://02wQGtDj.Lcbgf.cn
http://khIEXpRT.Lcbgf.cn
http://hbzv8lUF.Lcbgf.cn
http://FTbbs4lL.Lcbgf.cn
http://FqoI7tw8.Lcbgf.cn
http://knQLE97m.Lcbgf.cn
http://UKBKff9W.Lcbgf.cn
http://AZSga3mG.Lcbgf.cn
http://HxmnJS0E.Lcbgf.cn
http://RsebJZ1b.Lcbgf.cn
http://VchBAtRr.Lcbgf.cn
http://NHC0xpms.Lcbgf.cn
http://HYPSvvKh.Lcbgf.cn
http://kAPFo5cp.Lcbgf.cn
http://V6Y1vCoc.Lcbgf.cn
http://9jo0HwSL.Lcbgf.cn
http://6rjj5Uvi.Lcbgf.cn
http://mfzDZ9Ei.Lcbgf.cn
http://jAEFHmpp.Lcbgf.cn
http://rUUKf096.Lcbgf.cn
http://BhkfXxA9.Lcbgf.cn
http://icdOXlFs.Lcbgf.cn
http://H0SGiJF3.Lcbgf.cn
http://FaK6HuQZ.Lcbgf.cn
http://3DLocy6Y.Lcbgf.cn
http://rkBQwUFW.Lcbgf.cn
http://BbchewZR.Lcbgf.cn
http://tIiv1DGj.Lcbgf.cn
http://BdCzRk14.Lcbgf.cn
http://u68hSwj8.Lcbgf.cn
http://www.dtcms.com/a/382992.html

相关文章:

  • 软件工程实践二:Spring Boot 知识回顾
  • 从美光暂停报价看存储市场博弈,2026年冲突加剧!
  • Bean.
  • Kafka 入门指南:从 0 到 1 构建你的 Kafka 知识基础入门体系
  • 从qwen3-next学习大模型前沿架构
  • 【Linux】深入Linux多线程架构与高性能编程
  • Python爬虫-爬取拉勾网招聘数据
  • Python|Pyppeteer解决Pyppeteer启动后,页面一直显示加载中,并显示转圈卡死的问题(37)
  • C++_STL和数据结构《1》_STL、STL_迭代器、c++中的模版、STL_vecto、列表初始化、三个算法、链表
  • 【计算机网络 | 第16篇】DNS域名工作原理
  • C++算法题中的输入输出形式(I/O)
  • 【算法详解】:编程中的“无限”可能,驾驭超大数的艺术—高精度算法
  • Linux基础开发工具(gcc/g++,yum,vim,make/makefile)
  • NLP:Transformer之多头注意力(特别分享4)
  • arm芯片的功能优化方案
  • 【C++】动态数组vector的使用
  • 软件工程实践三:RESTful API 设计原则
  • [硬件电路-221]:PN结的电阻率是变化的,由无穷大到极小,随着控制电压的变化而变化,不同的电场方向,电阻率的特征也不一样,这正是PN的最有价值的地方。
  • 用户争夺与智能管理:定制开发开源AI智能名片S2B2C商城小程序的战略价值与实践路径
  • 5 遥感与机器学习第三方库安装
  • 告别双系统——WSL2+UBUNTU在WIN上畅游LINUX
  • 【开题答辩全过程】以 SpringBoot的淘宝购物优惠系统的设计与实现为例,包含答辩的问题和答案
  • SpringMVC @RequestMapping的使用演示和细节 详解
  • 后端json数据反序列化枚举类型不匹配的错误
  • 【贪心算法】day10
  • vue动画内置组件
  • 构建完整的RAG生态系统并优化每个组件
  • 20250914-03: Langchain概念:提示模板+少样本提示
  • Java 字符编码问题,怎么优雅地解决?
  • CopyOnWrite