AOP实现异常记录日志

1、导包

        <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-aop</artifactId></dependency>

2、自定义注解

package com.leo.annotate;import java.lang.annotation.*;/*** @author Leo*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface ExceptionLogAnnotate {String code() default "";String name() default "";String errorName() default "";
}

3、定义切片类,并实现相关逻辑处理

package com.leo.aspect;import com.leo.annotate.ExceptionLogAnnotate;
import com.leo.cache.JvmLruOneCacheComponent;
import com.leo.dto.ErrorInfoResDto;
import com.leo.service.ErrorInfoService;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;import javax.annotation.Resource;
import java.lang.reflect.Method;
import java.util.Arrays;/*** @author Leo*/
@Aspect
@Component
public class ExceptionLogAspect {@Resourceprivate ErrorInfoService errorInfoService;@Resourceprivate JvmLruOneCacheComponent cacheUtil;/*** 获取方法上的注解*/public static ExceptionLogAnnotate getExceptionLogAnnotate(JoinPoint jp) throws Exception {ExceptionLogAnnotate o = null;// 拿到切点的类名,方法名,方法参数String className = jp.getTarget().getClass().getName();String methodName = jp.getSignature().getName();// 获取参数数组Object[] args = jp.getArgs();Class<?> targetClass = Class.forName(className);Method[] methods = targetClass.getMethods();for (Method method : methods) {if (method.getName().equalsIgnoreCase(methodName)) {Class<?>[] clazzs = method.getParameterTypes();if (clazzs.length == args.length) {o = method.getAnnotation(ExceptionLogAnnotate.class);}}}return o;}@Pointcut(value = "@annotation(com.leo.annotate.ExceptionLogAnnotate)")public void pointCut() {}/*** 定义了一个异常通知,这个通知对上面定义的pointCut()切入点中的所有方法有效*/@AfterThrowing(value = "pointCut()",throwing = "e")public void logAfterThrowing(JoinPoint joinPoint,Exception e) throws Exception {ExceptionLogAnnotate ela = getExceptionLogAnnotate(joinPoint);String msgInfo = e.getMessage() + "\r\n" + Arrays.toString(e.getStackTrace());ErrorInfoResDto errorDto = ErrorInfoResDto.buildDto(ela.code(), ela.name(), ela.errorName(), msgInfo);//保存到数据库errorInfoService.asyncCreateErrorInfo(errorDto);}
}

4、使用。在方法上加上注解。(自调方法不生效)

    @Override@ExceptionLogAnnotate(errorName= "test")public void logErrTest() {getDataTest(item);}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/672237.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

DHCP配置

拓扑图 接口地址池 PC5通过接口地址池获取地址&#xff0c;DHCP服务器和VLAN40的网关为SW4 10.0.40.254 SW4 # sysname Huawei # vlan batch 10 20 30 40 # dhcp enable # interface Vlanif40ip address 10.0.40.254 255.255.255.128dhcp select interface # interface Giga…

frp新版toml配置

从frp v0.52.0 版本开始&#xff0c;frp 将TOML作为配置文件格式。INI 格式已被弃用&#xff0c;并将在未来的发布中移除。因此&#xff0c;frp v0.52.0 及更高版本的配置文件默认为TOML格式。 项目地址 GitHub&#xff1a;https://github.com/fatedier/frp/releases 服务端…

做跨境电商为什么需要使用住宅代理IP?

住宅代理IP是近年来跨境电商领域日益受到重视的技术工具&#xff0c;不仅可以保护隐私、优化网络速度&#xff0c;还能助推跨境电商的精细化管理。接下来&#xff0c;我们将深入探讨利用住宅代理IP如何为跨境电商业务带来竞争优势。 一、住宅代理IP与跨境电商 住宅代理IP&…

「深度学习」门控循环单元GRU

一、梯度消失问题 梯度消失&#xff1a; 基础的 RNN 模型不善于处理长期依赖关系&#xff0c;有很多局部影响&#xff0c;很难调整自己前面的计算。y^{<i>} 仅仅受自己附近的值影响。 解决方法&#xff1a;GRU 或 LSTM 梯度爆炸&#xff1a; 反向传播时&#xff0c;随着…

多头注意力和多尺度注意力的区别

我在李沐动手深度学习中第10章学了多头注意力。 最近的论文里有多尺度注意力&#xff0c;其实这俩不一样&#xff0c;我下面综述一下两者的区别。 多头注意力&#xff08;Multi-head Attention&#xff09;和多尺度注意力&#xff08;Multi-scale Attention&#xff09;是两种…

【数据分享】1929-2023年全球站点的逐日降水量数据(Shp\Excel\免费获取)

气象数据是在各项研究中都经常使用的数据&#xff0c;气象指标包括气温、风速、降水、湿度等指标&#xff0c;说到常用的降水数据&#xff0c;最详细的降水数据是具体到气象监测站点的降水数据&#xff01; 有关气象指标的监测站点数据&#xff0c;之前我们分享过1929-2023年全…

JavaScript中闭包的定义、原理及应用场景

JavaScript是一门以函数为核心的编程语言&#xff0c;其独特的闭包特性是众多开发者所喜爱的特点之一。闭包是一种非常强大的概念&#xff0c;可以帮助我们实现许多复杂的功能和逻辑。本篇博客将为大家深入介绍JavaScript中闭包的定义、原理及应用场景&#xff0c;并通过示例代…

C++泛型编程:函数模板

基本语法&#xff1a; template <typename T> void mySwap(T& a, T& b) {//类型参数化T temp a;a b;b temp; } void test01() {int a 10, b 20;//自动类型推导mySwap(a,b);//显示指定类型mySwap<int>(a, b); } 实例&#xff1a;数组排序 template&…

SpringCloud--Gateway解析

一、Gateway简介 Gateway是Spring Cloud官方推出的第二代微服务网关&#xff0c;它旨在提供统一的路由方式以及为微服务应用提供强大的负载均衡能力。与第一代Spring Cloud Netflix Zuul相比&#xff0c;Spring Cloud Gateway在性能、可扩展性、易用性等方面都有了显著的提升。…

(29)数组异或操作

文章目录 每日一言题目解题思路方法一方法二 代码方法一方法二 结语 每日一言 泉涸&#xff0c;鱼相与处于陆&#xff0c;相呴以湿&#xff0c;相濡以沫&#xff0c;不如相忘于江湖。 --庄子内篇大宗师 题目 题目链接&#xff1a;数组异或操作 给你两个整数&#xff0c;n 和…

【VS2022】运行cmake项目

在这里插入代码片https://github.com/kitamstudios/rust-analyzer.vs/blob/master/PREREQUISITES.md Latest rustup (Rust Toolchain Installer). Install from here. Welcome to Rust!This will download and install the official compiler for the Rust programming langua…

Python的属性查找机制的学习笔记

Python中属性查找机制的描述如下&#xff1a; 描述符方法&#xff1a;如果一个类的属性是由描述符定义的&#xff08;即实现了__get__()、__set__()或__delete__()方法&#xff09;&#xff0c;Python会首先调用相应的描述符方法。例如&#xff0c;如果一个属性有__get__()方法…

前端下载文件有哪些方式

前端下载文件有哪些方式 在前端&#xff0c;最常见和最常用的文件下载方式是&#xff1a; 使用 标签的 download 属性&#xff1a; 创建一个 标签&#xff0c;并设置其 href 属性为文件的 URL&#xff0c;然后使用 download 属性指定下载的文件名。 这种方式简单直接&…

作业5.......

封装strcat #include <stdio.h> #include <string.h> int main(int argc, const char *argv[]) { int i0; char arr[30]; char brr[30]; gets(arr); gets(brr); for(i0;i<strlen(brr);i) { arr[istrlen(brr)]brr[i]; printf("%d…

SpringBoot - 不加 @EnableCaching 标签也一样可以在 Redis 中存储缓存?

网上文章都是说需要在 Application 上加 EnableCaching 注解才能让缓存使用 Redis&#xff0c;但是测试发现不用 EnableCaching 也可以使用 Redis&#xff0c;是网上文章有问题吗&#xff1f; 现在 Application 上用了 EnableAsync&#xff0c;SpringBootApplication&#xff0…

go语言每日一练——链表篇(六)

传送门 牛客面试必刷101题—— 判断链表中是否有环 牛客面试必刷101题—— 链表中环的入口结点 题目及解析 题目一 代码 package mainimport . "nc_tools"/** type ListNode struct{* Val int* Next *ListNode* }*//**** param head ListNode类* return bool…

curl命令忽略不受信任的https安全限制

用curl命令没有得到返回&#xff0c;还报了个提示&#xff1a; curl: (60) Issuer certificate is invalid. More details here: http://curl.haxx.se/docs/sslcerts.html curl performs SSL certificate verification by default, using a “bundle” of Certificate Authorit…

vue3-内置组件-TransitionGroup

<TransitionGroup> 是一个内置组件&#xff0c;用于对 v-for 列表中的元素或组件的插入、移除和顺序改变添加动画效果。 与 <Transition> 的区别 <TransitionGroup> 支持和 <Transition> 基本相同的 props、CSS 过渡 class 和 JavaScript 钩子监听器&…

linux centos安装LibreOffice

1.下载安装包 #下载安装包 cd /opt wget https://mirror.cyberbits.eu/tdf/libreoffice/stable/24.2.0/rpm/x86_64/LibreOffice_24.2.0_Linux_x86-64_rpm.tar.gz2.解压安装包 #解压安装包 tar -zxvf LibreOffice_24.2.0_Linux_x86-64_rpm.tar.gz3.安装 #安装 cd /opt/LibreO…

Elasticsearch单个索引数据量过大的优化

当Elasticsearch&#xff08;ES&#xff09;中的单个索引&#xff08;index&#xff09;的数据量变得过大时&#xff0c;可能会遇到性能下降、查询缓慢、管理困难等问题。为了优化和应对大索引的挑战&#xff0c;可以考虑以下策略&#xff1a; 1. 使用分片和副本 分片&#xf…