Spring AOP+注解方式实现系统日志记录
一、前言
在上篇文章中,我们使用了AOP思想实现日志记录的功能,代码中采用了指定连接点方式(@Pointcut(“execution(* com.nowcoder.community.controller..(…))”)),指定后不需要在进行任何操作就可以记录日志了,但是如果我们对某些controller不想记录日志,就需要更改指定的切点,灵活性较差。因此采用注解+AOP方式,实现更灵活的日志记录功能。
二、注解实现代码
package com.nowcoder.community.annotation;import java.lang.annotation.*;/*** @author Janson* @Description AOP日志记录注解* @Date 2023/5/12* @Target LogAspect 注解的作用目标* @Retention 指定注解的保留时间*/@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface LogAspect {/*** title 自定义名称* description 自定义描述* @return*/String title() default "";String description() default "";
}
三、AOP日志记录类实现代码
定义切点(织入点)
execution(* com.nowcoder.community.controller..(…))
- 第一个 * 表示 支持任意类型返回值的方法
- com.nowcoder.community.controller 表示这个包下的类
- 第二个 * 表示 controller包下的任意类
- 第三个 * 表示 类中的任意方法
- (…) 表示方法可以拥有任意参数
可以根据自己的需求替换。
package com.nowcoder.community.aspect;import com.nowcoder.community.annotation.LogAspect;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;/*** @author Janson* @Description AOP日志记录,此方案不统一设置连接点 @Pointcut,而是采用注解方式,指定连接的方法* @Date 2023/5/12*/
@Aspect
@Component
public class LogAspectAnnotationTest {/*** 定义切点(织入点)* execution(* com.nowcoder.community.controller.*.*(..))* - 第一个 * 表示 支持任意类型返回值的方法* - com.nowcoder.community.controller 表示这个包下的类* - 第二个 * 表示 controller包下的任意类* - 第三个 * 表示 类中的任意方法* - (..) 表示方法可以拥有任意参数* 可以根据自己的需求替换。**/@Before(value = "@annotation(controllerLog)")public void before(JoinPoint joinPoint, LogAspect controllerLog){String className = joinPoint.getClass().getName();String methodName = joinPoint.getSignature().getName();ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();if (attributes == null){return;}String ip = attributes.getRequest().getRemoteHost();String requestURI = attributes.getRequest().getRequestURI();String requestMethod = attributes.getRequest().getMethod();String title = controllerLog.title();String description = controllerLog.description();System.out.println("title is " + title + ",description is " + description);System.out.println("before excute ······");System.out.println(String.format("用户: [%s] 的请求路径为:[%s], 请求方式为: [%s],请求类名为: [%s], 请求方法名为: [%s]", ip,requestURI,requestMethod,className,methodName));}@After(value = "@annotation(controllerLog)")public void after(JoinPoint joinPoint, LogAspect controllerLog){String className = joinPoint.getClass().getName();String methodName = joinPoint.getSignature().getName();ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();if (attributes == null){return;}String ip = attributes.getRequest().getRemoteHost();String requestURI = attributes.getRequest().getRequestURI();String requestMethod = attributes.getRequest().getMethod();String title = controllerLog.title();String description = controllerLog.description();System.out.println("title is " + title + ",description is " + description);System.out.println("after excute ······");System.out.println(String.format("用户: [%s] 的请求路径为:[%s], 请求方式为: [%s],请求类名为: [%s], 请求方法名为: [%s]", ip,requestURI,requestMethod,className,methodName));}@AfterReturning(value = "@annotation(controllerLog)")public void afterReturning(JoinPoint joinPoint, LogAspect controllerLog){String className = joinPoint.getClass().getName();String methodName = joinPoint.getSignature().getName();ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();if (attributes == null){return;}String ip = attributes.getRequest().getRemoteHost();String requestURI = attributes.getRequest().getRequestURI();String requestMethod = attributes.getRequest().getMethod();String title = controllerLog.title();String description = controllerLog.description();System.out.println("title is " + title + ",description is " + description);System.out.println("afterReturning excute ······");System.out.println(String.format("用户: [%s] 的请求路径为:[%s], 请求方式为: [%s],请求类名为: [%s], 请求方法名为: [%s]", ip,requestURI,requestMethod,className,methodName));}@AfterThrowing(value = "@annotation(controllerLog)",throwing = "e")public void afterThrowing(JoinPoint joinPoint, LogAspect controllerLog,Exception e){String className = joinPoint.getClass().getName();String methodName = joinPoint.getSignature().getName();ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();if (attributes == null){return;}String ip = attributes.getRequest().getRemoteHost();String requestURI = attributes.getRequest().getRequestURI();String requestMethod = attributes.getRequest().getMethod();String title = controllerLog.title();String description = controllerLog.description();System.out.println("title is " + title + ",description is " + description);System.out.println("afterThrowing excute ······" + e.getMessage());System.out.println(String.format("用户: [%s] 的请求路径为:[%s], 请求方式为: [%s],请求类名为: [%s], 请求方法名为: [%s],请求失败原因: [%s]", ip,requestURI, requestMethod,className,methodName,e.getMessage()));}@Around(value = "@annotation(controllerLog)")public void around(ProceedingJoinPoint joinPoint, LogAspect controllerLog) throws Throwable {String className = joinPoint.getTarget().getClass().getName();String methodName = joinPoint.getSignature().getName();System.out.println("className is : " + className + ". methodName is : " + methodName);ServletRequestAttributes attributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();if (attributes == null){return;}// 请求ipString ip = attributes.getRequest().getRemoteHost();// 请求路径String requestURI = attributes.getRequest().getRequestURI();// 请求方法String requestMethod = attributes.getRequest().getMethod();String title = controllerLog.title();String description = controllerLog.description();System.out.println(String.format("用户: [%s] 的请求路径为:[%s], 请求方式为: [%s],请求类名为: [%s], 请求方法名为: [%s]", ip,requestURI,requestMethod,className,methodName));Object obj = null;try {obj = joinPoint.proceed();} catch (Throwable e) {System.out.println("我捕获了异常");throw new RuntimeException("执行失败",e);}System.out.println(String.format("用户: [%s] 的请求路径为:[%s], 请求方式为: [%s],请求类名为: [%s], 请求方法名为: [%s]", ip,requestURI,requestMethod,className,methodName));System.out.println("around excute after ········");}
}相关文章:
Spring AOP+注解方式实现系统日志记录
一、前言 在上篇文章中,我们使用了AOP思想实现日志记录的功能,代码中采用了指定连接点方式(Pointcut(“execution(* com.nowcoder.community.controller..(…))”)),指定后不需要在进行任何操作就可以记录日志了&…...
OpenGL 4.0的Tessellation Shader(细分曲面着色器)
细分曲面着色器(Tessellation Shader)处于顶点着色器阶段的下一个阶段,我们可以看以下链接的OpenGL渲染流水线的图:Rendering Pipeline Overview。它是由ATI在2001年率先设计出来的。 目录 细分曲面着色器细分曲面Patch细分曲面控…...
项目经理如何及时掌控项目进度?
延迟是指超出计划的时间,而无法掌控则意味着管理者对实际情况一无所知。 为了解决这些问题,我们需要建立好的制度和沟通机制。例如使用项目管理软件来跟踪进度、定期开会并避免沟通障碍等。 管理者可以建立相关制度: 1、建立进度记录制度。…...
HTML <applet> 标签
HTML5 中不支持 <applet> 标签在 HTML 4 中用于定义嵌入式小程序(插件)。 实例 一个嵌入的 Java applet: <applet code="Bubbles.class" width="350" height="350"> Java applet that draws animated bubbles. </applet&g…...
加密与解密
加密与解密 加密方式分类 加密方式主要分为两种 一种是对称加密一种是非对称加密 对称加密 对称和非对称两种方式主要说的是加密和解密两个过程。 如果对数据用一个钥匙进行了加密,那么, 你想成功读取到这个加密了的数据的话,就必须对这…...
京东金融Android瘦身探索与实践
作者:京东科技 冯建华 一、背景 随着业务不断迭代更新,App的大小也在快速增加,2019年~2022年期间一度超过了117M,期间我们也做了部分优化如图1红色部分所示,但在做优化的同时面临着新的增量代码,包体积一直…...
open3d-ml 读取SemanticKITTI Dataset
目录 1. 下载dataset 2. 读取并做可视化 3. 源码阅读 3.1 读取点云数据-bin格式 3.2 读取标注数据-.label文件 3.3 读取配置 3.4 test 3.5 train 1. 下载dataset 以SemanticKITTI为例。下载链接:http://semantic-kitti.org/dataset.html#download 把上面三…...
6.其他函数
1.时间日期类 -- current_date() 返回当前日期 -- date_add(date, n) 返回从date开始n天之后的日期 -- date_sub(date, n) 返回从date开始n天之前的日期 -- datediff(date1, date2) 返回date1-date2的日期差 -- year(date) 返回…...
2023年宜昌市中等职业学校技能大赛 “网络搭建与应用”竞赛题-1
2023年宜昌市中等职业学校技能大赛 “网络搭建与应用”竞赛题 一、竞赛内容分布 “网络搭建及应用”竞赛共分二个部分,其中: 第一部分:企业网络搭建部署项目,占总分的比例为50%; 第二部分:企业网络服…...
Linux权限划分的原则
考察的不仅是一个具体的指令,还考察对技术层面的认知。 如果对 Linux 权限有较深的认知和理解,那么完全可以通过查资料去完成具体指令的执行。更重要的是,认知清晰的程序员可以把 Linux 权限管理的知识迁移到其他的系统设计中。 权限抽象 一…...
PhotoScan拼接无人机航拍RGB照片
目录 背景 拼接步骤 1.新建并保存项目 2.添加照片 3.对齐照片 4.添加标记(Markers) 5.添加地面控制点 6.建立批处理任务 7.使用批处理文件进行批处理 8.导出DEM 9.导出DOM 背景 本文介绍使用地面控制点(GCPs)拼接…...
【设计模式】责任链模式的介绍及其应用
责任链的介绍 责任链模式是一种对象的行为模式。在责任链模式里,很多对象由每一个对象对其下家的引用而连接起来形成一条链。请求在这个链上传递,直到链上的某一个对象决定处理此请求。发出这个请求的客户端并不知道链上的哪一个对象最终处理这个请求&a…...
一些思考关于行业,关于方向,关于人生路线
一些碎碎念 选择与视角工程与科研平台与信息敢问路在何方 选择与视角 两年前的秋招时几乎速通了出现在学校招聘会上的几乎出现的每一个offer,那也是我人生第一次收获到如此多的肯定与选择,为此我在b站上上传了一期就业解读,作为一个冷门到几…...
fbx sdk的使用介绍
我们平时需要围绕fbx写一些小工具,虽说使用ascii格式的fbx可以直接进行字符串解析,并且网上也有一些基于ascii解析的开源库,但在制作一些通用的工具时,使用fbx sdk进行编写肯定是最好的。 1.下载fbx sdk和cmake 要用cmake生成vi…...
mvvm模式
mvvm是Model-View-ViewModel的缩写,是前端的一种架构模式 M - Model,模型 对应data数据 V - View,视图 对应用户界面,DOM元素 VM - ViewModel,视图模型 对应vue实例对象,是连接model和view的桥梁 …...
Spring/SpringBoot常用注解总结
为什么要写这篇文章? 最近看到网上有一篇关于 SpringBoot 常用注解的文章被转载的比较多,我看了文章内容之后属实觉得质量有点低,并且有点会误导没有太多实际使用经验的人(这些人又占据了大多数)。所以,自…...
2023 年第八届数维杯大学生数学建模挑战赛 B 题 节能列车运行控制优化策略
在城市交通电气化进程快速推进的同时,与之相应的能耗增长和负面效应也 在迅速增加。城市轨道交通中的快速增长的能耗给城轨交通的可持续性发展带来 负担。2018 年,北京、上海、广州地铁负荷占全市总负荷的 1.5%-2.5%,成为了 城市电网的最大单体负荷[1]。…...
【Swift】 NSButton的用法和示例
NSButton是macOS开发中常用的控件,用于创建按钮。它有许多用法和需要注意的事项,下面介绍其中的一些。 1. 创建按钮:使用init(frame:)或init(title:action:)初始化按钮 let button NSButton(frame: NSRect(x: 0, y: 0, width: 100, height…...
2023什么蓝牙耳机好?经销商盘点新手必入蓝牙耳机品牌
蓝牙耳机是除手机外我们使用频率最高的数码产品,我做蓝牙耳机经销商五年来,对各个品牌都有深入了解。近期看到很多新手们咨询什么蓝牙耳机好,我给大家盘点一下新手必看的五大蓝牙耳机品牌。 1.JEET Air 2蓝牙耳机 推荐理由:专为舒…...
MySQL基础(二十)MySQL的数据目录
1. MySQL8的主要目录结构 find / -name mysql1.1 数据库文件的存放路径 show variables like datadir; # /var/lib/mysql/1.2 相关命令目录 相关命令目录:/usr/bin 和/usr/sbin。 1.3 配置文件目录 配置文件目录:/usr/share/mysql-8.0(命…...
除了排错,你可能不知道OPC Expert v8.1还能做这些:数据归档、计算与冗余实战
解锁OPC Expert v8.1的隐藏潜力:数据归档、实时计算与冗余架构实战指南在工业自动化领域,OPC Expert常被视为故障排查的"急救箱",但它的能力远不止于此。当大多数工程师还在用它解决DCOM配置问题时,少数先行者已经用它重…...
PDF 可视化签名盖章页技术解析
本文是我在设备检测系统项目开发中,无设备检测的技术实现备忘录,记载实现过程。 本文以 PC 端页面 sign-pdf.vue 为主线,说明「无设备报检」在报告审批环节如何通过前后端协作,完成报告/记录 PDF 上的签名、印章、报告编号拖放定位,并在审批通过后由后端合并生成带签章的正…...
解决Claude Code Token不足问题并享受Taotoken活动价
🚀 告别海外账号与网络限制!稳定直连全球优质大模型,限时半价接入中。 👉 点击领取海量免费额度 解决Claude Code Token不足问题并享受Taotoken活动价 应用场景类,聚焦于使用Claude Code时遇到Token配额紧张的开发者&…...
《我看见的世界:李飞飞自传》第1-6章阅读笔记:从移民少女到AI教母的“看见“之旅
前言 当我们谈论人工智能时,我们谈论的是算法、数据、算力,是那些冰冷的代码和复杂的模型。但在《我看见的世界:李飞飞自传》中,李飞飞用她独特的视角告诉我们:AI的本质,是人类对"看见"世界的渴望…...
Windows终极PDF处理工具:3步免费安装Poppler完整指南
Windows终极PDF处理工具:3步免费安装Poppler完整指南 【免费下载链接】poppler-windows Download Poppler binaries packaged for Windows with dependencies 项目地址: https://gitcode.com/gh_mirrors/po/poppler-windows 你是否曾经为在Windows上处理PDF文…...
从零到上机:我的第一个Quest 3空间锚点应用是如何跑起来的(附完整Unity工程)
从零到上机:我的第一个Quest 3空间锚点应用是如何跑起来的(附完整Unity工程)第一次戴上Meta Quest 3时,那种虚拟与现实交织的震撼感至今难忘。但作为开发者,更让我着迷的是如何让虚拟物体在真实空间中"记住"…...
OmenSuperHub:释放惠普游戏本性能的纯净开源控制中心
OmenSuperHub:释放惠普游戏本性能的纯净开源控制中心 【免费下载链接】OmenSuperHub Control Omen laptop performance, fan speeds, and keyboard lighting, and unlock power limits. 项目地址: https://gitcode.com/gh_mirrors/om/OmenSuperHub 还在为官方…...
从零构建FOC轮腿机器人:开源平衡机器人完整指南
从零构建FOC轮腿机器人:开源平衡机器人完整指南 【免费下载链接】foc-wheel-legged-robot Open source materials for a novel structured legged robot, including mechanical design, electronic design, algorithm simulation, and software development. | 一个…...
如何快速上手Redux Dynamic Modules:5分钟完成Redux模块化改造
如何快速上手Redux Dynamic Modules:5分钟完成Redux模块化改造 【免费下载链接】redux-dynamic-modules Modularize Redux by dynamically loading reducers and middlewares. 项目地址: https://gitcode.com/gh_mirrors/re/redux-dynamic-modules Redux Dyn…...
【C++】零基础入门 · 第 4 节:循环结构(while、for、do-while)
上一节我们学习了条件判断,这一节来学习循环结构。循环让程序能够重复执行某段代码,直到满足特定条件为止。C 提供了三种循环语句:while、for 和 do-while。 1. while 循环:先判断后执行 while 循环在每次执行前先检查条件&#x…...
