Aop切面自定义注解的使用

article/2025/9/21 14:06:55

一:功能简介

本文主要记录如何使用aop切面的方式来实现日志记录功能。

主要记录的信息有: 操作人,方法名,参数,运行时间,操作类型(增删改查),详细描述,返回值。

二:项目结构图

在这里插入图片描述

三:代码实现

1.配置文件

spring.aop.auto=true #开启spring的aop配置,简单明了,不需要多配置其他的配置或注解。
server.port=1000  #端口号

2.AOP切点类

这个是最主要的类,可以使用自定义注解或针对包名实现AOP增强。

1)这里实现了对自定义注解的环绕增强切点,对使用了自定义注解的方法进行AOP切面处理;

2)对方法运行时间进行监控;

3)对方法名,参数名,参数值,对日志描述的优化处理;

在方法上增加@Aspect 注解声明切面,使用@Pointcut 注解定义切点,标记方法。

使用切点增强的时机注解:@Before,@Around,@AfterReturning,@AfterThrowing,@After

package com.example.aopdemo.config;import com.alibaba.fastjson.JSON;
import com.example.aopdemo.pojo.OperationLog;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;/***@author lxy*@description 切面*@date 2020/12/16*/
@Aspect
@Component
public class LogAspect {/*** 此处的切点是注解的方式,也可以用包名的方式达到相同的效果* '@Pointcut("execution(* com.wwj.springboot.service.impl.*.*(..))")'*/@Pointcut("@annotation(com.example.aopdemo.config.OperationLogDetail)")public void operationLog(){}/*** 环绕增强,相当于MethodInterceptor*/@Around("operationLog()")public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {Object res = null;long time = System.currentTimeMillis();try {res =  joinPoint.proceed();time = System.currentTimeMillis() - time;return res;} finally {try {//方法执行完成后增加日志addOperationLog(joinPoint,res,time);}catch (Exception e){System.out.println("LogAspect 操作失败:" + e.getMessage());e.printStackTrace();}}}private void addOperationLog(JoinPoint joinPoint, Object res, long time){MethodSignature signature = (MethodSignature)joinPoint.getSignature();OperationLog operationLog = new OperationLog();operationLog.setRunTime(time);operationLog.setReturnValue(JSON.toJSONString(res));operationLog.setId(UUID.randomUUID().toString());operationLog.setArgs(JSON.toJSONString(joinPoint.getArgs()));operationLog.setCreateTime(new Date());operationLog.setMethod(signature.getDeclaringTypeName() + "." + signature.getName());operationLog.setUserId("#{currentUserId}");operationLog.setUserName("#{currentUserName}");OperationLogDetail annotation = signature.getMethod().getAnnotation(OperationLogDetail.class);if(annotation != null){operationLog.setLevel(annotation.level());operationLog.setDescribe(getDetail(((MethodSignature)joinPoint.getSignature()).getParameterNames(),joinPoint.getArgs(),annotation));operationLog.setOperationType(annotation.operationType().getValue());operationLog.setOperationUnit(annotation.operationUnit().getValue());}//TODO 这里保存日志System.out.println("记录日志:" + operationLog.toString());
//        operationLogService.insert(operationLog);}/*** 对当前登录用户和占位符处理* @param argNames 方法参数名称数组* @param args 方法参数数组* @param annotation 注解信息* @return 返回处理后的描述*/private String getDetail(String[] argNames, Object[] args, OperationLogDetail annotation){Map<Object, Object> map = new HashMap<>(4);for(int i = 0;i < argNames.length;i++){map.put(argNames[i],args[i]);}String detail = annotation.detail();try {detail = "'" + "#{currentUserName}" + "'=》" + annotation.detail();for (Map.Entry<Object, Object> entry : map.entrySet()) {Object k = entry.getKey();Object v = entry.getValue();detail = detail.replace("{{" + k + "}}", JSON.toJSONString(v));}}catch (Exception e){e.printStackTrace();}return detail;}/*  @Before("operationLog()")public void doBeforeAdvice(JoinPoint joinPoint){System.out.println("进入方法前执行.....");}*//*** 处理完请求,返回内容* @param ret*//* @AfterReturning(returning = "ret", pointcut = "operationLog()")public void doAfterReturning(Object ret) {System.out.println("方法的返回值 : " + ret);}
*//*** 后置异常通知*//*@AfterThrowing("operationLog()")public void throwss(JoinPoint jp){System.out.println("方法异常时执行.....");}*//*** 后置最终通知,final增强,不管是抛出异常或者正常退出都会执行*//*@After("operationLog()")public void after(JoinPoint jp){System.out.println("方法最后执行.....");}*/}

3.自定义注解

package com.example.aopdemo.config;import com.example.aopdemo.constant.OperationType;
import com.example.aopdemo.constant.OperationUnit;import java.lang.annotation.*;/*** Created by IntelliJ IDEA** @author lxy* @date 2018/9/12*/
//@OperationLogDetail(detail = "通过手机号[{{tel}}]获取用户名",level = 3,operationUnit = OperationUnit.USER,operationType = OperationType.SELECT)
@Documented
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface OperationLogDetail {/*** 方法描述,可使用占位符获取参数:{{tel}}*/String detail() default "";/*** 日志等级:自己定,此处分为1-9*/int level() default 0;/*** 操作类型(enum):主要是select,insert,update,delete*/OperationType operationType() default OperationType.UNKNOWN;/*** 被操作的对象(此处使用enum):可以是任何对象,如表名(user),或者是工具(redis)*/OperationUnit operationUnit() default OperationUnit.UNKNOWN;
}

4.注解用到的枚举类型

package com.example.aopdemo.constant;public enum OperationType {/*** 操作类型*/UNKNOWN("unknown"),DELETE("delete"),SELECT("select"),UPDATE("update"),INSERT("insert");private String value;public String getValue() {return value;}public void setValue(String value) {this.value = value;}OperationType(String s) {this.value = s;}
}
package com.example.aopdemo.constant;public enum OperationUnit {/*** 被操作的单元*/UNKNOWN("unknown"),USER("user"),EMPLOYEE("employee"),Redis("redis");private String value;OperationUnit(String value) {this.value = value;}public String getValue() {return value;}public void setValue(String value) {this.value = value;}
}

5.日志记录对象

package com.example.aopdemo.pojo;import lombok.Data;import java.util.Date;/***@author liuxingying*@description*@date 2020/12/16*/
@Data
public class OperationLog {private String id;private Date createTime;/*** 日志等级*/private Integer level;/*** 被操作的对象*/private String operationUnit;/*** 方法名*/private String method;/*** 参数*/private String args;/*** 操作人id*/private String userId;/*** 操作人*/private String userName;/*** 日志描述*/private String describe;/*** 操作类型*/private String operationType;/*** 方法运行时间*/private Long runTime;/*** 方法返回值*/private String returnValue;
}

6.springboot启动类

package com.example.aopdemo;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class AopdemoApplication {public static void main(String[] args) {SpringApplication.run(AopdemoApplication.class, args);}}

7.controller类

package com.example.aopdemo.controller;import com.example.aopdemo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;/***@author liuxingying*@description*@date 2020/12/16*/
@RestController
@RequestMapping("/user")
public class UserController {@Autowiredprivate UserService userService;/*** 访问路径 http://localhost:11000/user/findUserNameByTel?tel=1234567* @param tel 手机号* @return userName*/@RequestMapping("/findUserNameByTel")public String findUserNameByTel(@RequestParam("tel") String tel){return userService.findUserName(tel);}
}

8.Service接口 和 ServiceImpl实现类

package com.example.aopdemo.service;public interface UserService {/*** 获取用户信息* @return* @param tel*/String findUserName(String tel);
}
package com.example.aopdemo.service;import com.example.aopdemo.config.OperationLogDetail;
import com.example.aopdemo.constant.OperationType;
import com.example.aopdemo.constant.OperationUnit;
import org.springframework.stereotype.Service;/***@author lxy*@description*@date 2020/12/16*/
@Service
public class UserServiceImpl implements UserService {@OperationLogDetail(detail = "通过手机号[{{tel}}]获取用户名",level = 3,operationUnit = OperationUnit.USER,operationType = OperationType.SELECT)@Overridepublic String findUserName(String tel) {System.out.println("tel:" + tel);return "zhangsan";}
}

四:pom依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.4.1</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.example</groupId><artifactId>aopdemo</artifactId><version>0.0.1-SNAPSHOT</version><name>aopdemo</name><description>Demo project for Spring Boot</description><properties><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><!-- 切面 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-aop</artifactId></dependency><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.59</version></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><excludes><exclude><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></exclude></excludes></configuration></plugin></plugins></build></project>

五:运行结果

进入方法前执行.....
tel:1234567
记录日志:OperationLog{id='cd4b5ba7-7580-4989-a75e-51703f0dfbfc', createTime=Fri Sep 14 08:54:55 CST 2018, level=3, operationUnit='user', method='com.wwj.springboot.service.impl.UserServiceImpl.findUserName', args='["1234567"]', userId='#{currentUserId}', userName='#{currentUserName}', describe=''#{currentUserName}'=》通过手机号["1234567"]获取用户名', operationType='select', runTime=4, returnValue='"zhangsan"'}
方法最后执行.....
方法的返回值 : zhangsan

参考自 俊俊的小熊饼干。


http://chatgpt.dhexx.cn/article/UyiF2MwG.shtml

相关文章

AOP切面注解

一.前言 在以前的项目中&#xff0c;很少去关注spring aop的具体实现与理论&#xff0c;只是简单了解了一下什么是aop具体怎么用&#xff0c;看到了一篇博文写得还不错&#xff0c;就转载来学习一下&#xff0c;博文地址&#xff1a;http://www.cnblogs.com/xrq730/p/4919025.h…

AOP切面执行顺序

文章目录 一. 概述二. 讲述1. 单切面中各通知方法的执行顺序2. 多切面中各通知方法的执行顺序3. 多切面的通知方法中抛出异常 参考资料 一. 概述 本文主要讲述以下几点 单AOP切面时&#xff0c;各通知方法的执行顺序。多AOP切面时&#xff0c;多切面的执行顺序和各通知方法的执…

spring aop切面执行顺序

spring aop切面执行顺序 切面执行顺序 现有切面1、切面2对同一个切点按先后顺序执行&#xff08;切面1先于切面2执行&#xff09; 切面1&#xff1a;Before、After、AfterReturnning、AfterThrowing、Around 执行前、Around 正常执行、Around 异常执行、Around 执行后切面2&am…

一文带你搞定AOP切面

摘要&#xff1a;AOP在spring中又叫“面向切面编程”&#xff0c;是对传统我们面向对象编程的一个补充&#xff0c;主要操作对象就是“ 切面”&#xff0c; 可以简单的理解它是贯穿于方法之中&#xff0c;在方法执行前、执行时、执行后、返回值后、异常后要执行的操作。 本文分…

SpringBoot AOP切面实现

文章目录 一、AOP简介二、AOP体系与概念三、AOP实例1、创建SpringBoot工程2、添加依赖3、AOP相关注解3.1、Aspect3.2、Pointcut3.2.1、execution()3.2.2、annotation() 3.3、Around3.4、Before3.5、After3.6、AfterReturning3.7、AfterThrowing 一、AOP简介 AOP&#xff08;As…

AOP切面编程的理解

一、什么是Spring的AOP&#xff1f; AOP在spring中又叫“面向切面编程”&#xff0c;它可以说是对传统我们面向对象编程的一个补充&#xff0c;从字面上顾名思义就可以知道&#xff0c;它的主要操作对象就是“切面”&#xff0c;所以我们就可以简单的理解它是贯穿于方法之中&a…

AOP切面使用

一、主要设计注解&#xff1a; Aspect After before Pointcut Around pom文件引入 <!--用于aop切面编程--> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> </dependency> 二、AOP核心…

AOP面向切面

1.什么是Spring的AOP? AOP又叫"面向切面编程",是对传统的面向对象编程的一个补充,主要的操作对象就是"切面 ",可以简单的理解它是贯穿于方法之中,在方法执行前、执行时、执行后、返回值后、异常后要执行的操作。 相当于将我们原本一条线执行的程序在中间切…

【JavaEE】Spring AOP (面向切面)详解

目录&#xff1a; 1. 什么是 Spring AOP&#xff1f;1.1 AOP1.2 使用 AOP 的场景 2. AOP 组成2.1 切面&#xff08;Aspect&#xff09;2.2 连接点&#xff08;Join Point&#xff09;2.3 切点&#xff08;Pointcut&#xff09;2.4 通知&#xff08;Advice&#xff09; 3. AOP 概…

斜杠,反斜杠说明

/ 斜杠 \反斜杠 在window中都用斜杠 反斜杠是用来转译字符串的 eg: \"a\" 输出"a"

斜杠、反斜杠、双斜杠、反双斜杠的区别和使用方法及范围

背景 这边我就找了两篇大神写的文章&#xff0c;讲得非常清晰明了。文章主要讲了一些历史缘故和我们面对各种斜杠时的疑惑。 斜杠’/’ 和反斜杠’’ 深入探讨正斜杠和反斜杠 概念 1. 斜杠"/"是URL地址中用到的分隔符&#xff0c;并且在linux系统中的文件路径也是…

glob.glob()之返回路径的正反斜杆问题

Windows环境下用一个反斜杠就行 绝对路径&#xff1a;D:\PyCharm_code\pytorch_study\xxx 相对路径&#xff1a;.\cifar10_train\**\*.png 以下是踩过的坑&#xff1a;记录下

正反斜杠的区别_正斜杠( / )和反斜杠( \ )的区别

反斜杠“\”是电脑出现了之后为了表示程序设计里的特殊含义才发明的专用标点。所以除了程序设计领域外&#xff0c;任何地方都不应该使用反斜杠。 如何区分正反斜杠 英语&#xff1a;"/" 英文是forward slash, “\" 是backward slash 形象些比喻的话&#xff0c…

正斜杠 “/” 与反斜杠 “\”辨析

文章目录 1. 正斜杠 / / /2. 反斜杠 \ \backslash \3. 正斜杠与反斜杠的区别4. 注意 注意&#xff0c; / / / 为正斜杠(forward slash)&#xff0c;而 \ \backslash \ 为反斜杠(backward slash)。 1. 正斜杠 / / / 斜线是斜线标点符号 / / /。曾经用于标记句点和逗号的斜…

斜杠'/' 和反斜杠'\'

斜杠’/‘和反斜杠’\’ 2019-1-21 引言&#xff1a;从大一进入信息专业&#xff0c;正式接触计算机、代码也有几年了。一开始迷迷糊糊学Ascii码&#xff0c;很多特殊字符都需要转义&#xff0c;比如换行符\n&#xff0c;自那时起我就拎不清转义符是斜杠还是反斜杠&#xff0c;…

全面了解 Python 中的反斜杆

本文全面介绍了 Python 中反斜杆(\)的用法&#xff0c;包括原始字符串和普通字符串&#xff0c;repr() 和 str() ,\ 作为转义符&#xff0c;\ 作为续行符&#xff0c;\ 在字符串转义和正则表达式转义中的过程及注意事项等。阅读本文预计 6 min. 全面了解 Python 中的反斜杆 1. …

教你认识正斜杠(/)与反斜杠(\)

正斜杠&#xff0c;又称左斜杠&#xff0c;符号是 “/” ; 反斜杠&#xff0c;也称右斜杠&#xff0c;符号是 “” 。 经常很迷惑正斜杠与反斜杠到底有何区别&#xff1f;以下是一些总结: 背景了解 &#xff1a; DOS路径&#xff1a; C:\WINDOWS\SETTING …这是反斜杠的作用后…

微信支付,二维码图片解析

微信支付&#xff1a; 后台返回的是数据流&#xff1b; 开始这样&#xff0c;但是不行&#xff0c; 解决&#xff1a;在请求里面加入 ‘responseType’: ‘blob’ , 转换&#xff1a;附上base64转图片 //base64转换base64ImgtoFile(dataurl, filename file) {let arr data…

Apache里如何将图片解析成PHP

首先&#xff0c;如果没有安装PHP&#xff0c;先安装PHP yum install -y php然后进入网站根目录&#xff0c;如果不记得网站根目录&#xff0c;可以去配置文件里找 我的是/mnt/z 所以进入这个目录下&#xff0c;新建一个i.jpg文件 在浏览器里查看这个文件&#xff0c;存在错误…

图像解析力算法—SFR(Spatial Frequency Response)

Mitre SFR 1.4和sfrmat3是基于ISO 12233标准&#xff0c;但在某些方面彼此不同&#xff1a;Mitre SFR 1.4旨在尽可能接近标准&#xff0c; 而sfrmat3包含一些改进&#xff0c;可以获得精确的结果 即使被测试的图像质量极低。 M O R E MTF50&#xff0c;MTF50P 在表示相机图像…