【SpringBoot应用篇】SpringBoot使用Aspect AOP注解实现日志管理(增强版)

news2024/10/6 10:37:44

【SpringBoot应用篇】SpringBoot使用Aspect AOP注解实现日志管理(增强版)

  • pom
  • Log
  • 实体类
    • OperateLog
    • Order
    • Good
  • LogAspect
  • 转换器
    • Convert
    • GoodConvert
    • OrderConvert
  • AopController
  • 启动类
  • @EnableAutoOperateLog

需求: 需要保存的日志内容在方法的参数中,并且方法参数的类型对象不一样,且对象的属性名称不一样。

解决思路:
1、添加类型转换器Convert接口,需要转换的类型继承Convert接口
2、@Log注解中添加Convert接口类型的Class属性
3、在切面环绕通知中进行处理

pom

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.2.RELEASE</version>
</parent>

<dependencies>
	<dependency>
	  <groupId>org.springframework.boot</groupId>
	  <artifactId>spring-boot-starter-aop</artifactId>
	</dependency>
	
	<dependency>
	  <groupId>org.springframework.boot</groupId>
	  <artifactId>spring-boot-starter-web</artifactId>
	</dependency>
	
	<dependency>
	  <groupId>org.projectlombok</groupId>
	  <artifactId>lombok</artifactId>
	</dependency>
</dependencies>

Log

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Log {
    String desc() default "";
    Class<? extends Convert> convert();
}

实体类

OperateLog

@Data
public class OperateLog {
    private String id;
    private String desc;
    private String result;
}

Order

@Data
public class Order {
    private String orderId;
}

Good

@Data
public class Good {
    private String goodId;
}

LogAspect

@Component
@Aspect
public class LogAspect {
    static final Logger logger = LoggerFactory.getLogger(LogAspect.class);

    static final ExecutorService executorService = new ThreadPoolExecutor(
            1,
            1,
            10 ,
            TimeUnit.SECONDS,
            new LinkedBlockingQueue<>(100),
            Executors.defaultThreadFactory(),
            new ThreadPoolExecutor.AbortPolicy());


    @Pointcut("@annotation(cn.zysheep.anno.Log)")
    public void pointcut(){};


    @Around("pointcut()")
    public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        Object result = proceedingJoinPoint.proceed();
        executorService.execute(()->{
            try {
                MethodSignature signature = (MethodSignature)proceedingJoinPoint.getSignature();
                Method method = signature.getMethod();
                Log log = method.getAnnotation(Log.class);
                if (log != null) {
                    Class<? extends Convert> convert = log.convert();
                    Convert instance = convert.newInstance();
                    OperateLog operateLog = instance.convert(proceedingJoinPoint.getArgs()[0]);
                    operateLog.setDesc(log.desc());
                    operateLog.setResult(result.toString());
                    logger.info("save operateLog: {}", operateLog);
                }
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        });
        return result;
    }
}

转换器

Convert

public interface Convert<T> {
    OperateLog convert(T t);
}

GoodConvert

public class GoodConvert implements Convert<Good> {
    @Override
    public OperateLog convert(Good updateOrder) {
        OperateLog operateLog = new OperateLog();
        operateLog.setId(updateOrder.getGoodId());
        return operateLog;
    }
}

OrderConvert

public class OrderConvert implements Convert<Order> {
    @Override
    public OperateLog convert(Order saveOrder) {
        OperateLog operateLog = new OperateLog();
        operateLog.setId(saveOrder.getOrderId());
        return operateLog;
    }
}

AopController

@RestController
public class AopController {

    @GetMapping("/saveOrder")
    @Log(desc = "保存订单", convert = OrderConvert.class)
    public String saveOrder(Order order) {
        System.out.println(order);
        return "ok";
    }

    @GetMapping("/saveGood")
    @Log(desc = "保存商品", convert = GoodConvert.class)
    public String saveGood(Good good) {
        System.out.println(good);
        return "ok";
    }
}

启动类

@SpringBootApplication
public class AopApplication {
    public static void main(String[] args) {
        SpringApplication.run(AopApplication.class, args);
    }
}

1、页面访问: http://localhost:8080/saveOrder?orderId=2
2、页面访问: http://localhost:8080/saveGood?goodId=1231
在这里插入图片描述
不同类型不同属性名称的值保存到了操作日志对象的id中

@EnableAutoOperateLog

使用SpringBoot自动配置的原理,启用热拔插效果

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Import({LogAspect.class})
public @interface EnableAutoOperateLog {
}

1、LogAspect 切面类去除@Component注解,保留@Aspect,否则切面不生效
2、启动类添加@EnableAutoOperateLog

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

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

相关文章

Elasticsearch 谷歌插件 Elasticsearch-head 使用

目录 什么是Elasticsearch-head 安装 ​编辑界面 ​编辑集群健康值的几种状态如下 解决跨域问题 基本使用 创建索引 点击概览 点击数据浏览 什么是Elasticsearch-head ElasticSearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎&#x…

js的Date对象

Date 对象用于处理日期与时间。可以通过 new 关键词来定义 Date 对象。 有四种方式初始化日期: new Date(); new Date(value); new Date(dateString); new Date(year, monthIndex [, day [, hours [, minutes [, seconds [, milliseconds]]]]]);实例化例子&#xff1a; var …

gitlab-ci.yml关键字(二)(全局)default、stages

default 这是一个全局关键字&#xff0c;可以在全局设置某些关键字的的默认值&#xff0c;如果在job中没有定义该关键字的值&#xff0c;那么就会使用全局设置的默认值 示例 default:image: ruby:3.0rspec:script: bundle exec rspecrspec 2.7:image: ruby:2.7script: bundl…

LeetCode 598 范围求和Ⅱ

LeetCode刷题记录 文章目录&#x1f4dc;题目描述&#x1f4a1;解题思路⌨C代码&#x1f4dc;题目描述 给你一个 m x n 的矩阵 M &#xff0c;初始化时所有的 0 和一个操作数组 op &#xff0c;其中 ops[i] [ai, bi] 意味着当所有的0 < x < ai和 0 < y < bi 时&am…

力扣——环形链表

142. 环形链表 II - 力扣&#xff08;LeetCode&#xff09; 这个题是要求我们判断链表是否存在环&#xff0c;有则返回环开始的结点&#xff0c;没有则返回一个NULL&#xff1b; 废话就不说了&#xff0c;直入主题&#xff1a; /*** Definition for singly-linked list.* str…

代码快一点~生活慢一点~【python性能调试工具分享】

背景1 天下武功&#xff0c;为快不破&#xff01; 上学的时候&#xff0c;有些人考试半小时就已经把卷子写完了。有些人还没写完。 工作的时候&#xff0c;有些人一天就能把活干完了&#xff0c;有些人还没开始。 写论文的时候&#xff0c;有些人代码都写完了&#xff0c;数…

ospf不规则区域实验

r11&#xff0c;r3为运行商&#xff0c;要求全网可达 首先现在r13上配置ospf 然后配置一条缺省路由并且下发路由 抓取流量 [r13]acl 2000 [r13-acl-basic-2000]rule permit source any [r13]int g0/0/1 [r13-GigabitEthernet0/0/1]nat outbound 2000 下一步实现ospf100 里面…

Ant Design入门

目录 一&#xff1a;什么是Ant Design&#xff1f; 二&#xff1a;开始使用 三&#xff1a;布局 四&#xff1a;表格 一&#xff1a;什么是Ant Design&#xff1f; Ant Design是阿里蚂蚁金服团队基于React开发的ui组件&#xff0c;主要用于中后台系统的使用。 官网&#x…

【技术】5G技术的应用场景及发展趋势

5G是具有高速率、低时延和大连接特点的新一代宽带移动通信技术。5G系统基于大带宽和大规模天线方案&#xff0c;能实现亚米级高精度定位。 据《5G经济社会影响白皮书》可知&#xff0c;按照2020年5G才大规模商用算起&#xff0c;预计2020年至2025年期间&#xff0c;中国5G发展将…

抓包工具wiresharke及抓包流程

背景&#xff1a;公司的系统在生产环境运行一段时间之后&#xff0c;通过skywalking监控工具发现时不时会有接口调用耗时很长的情况出现。且监控到的数据和华为云ELB的监控日志不匹配&#xff0c;为了验证是否是由华为云ELB转发延迟导致&#xff0c;决定在生产上抓包验证&#…

【应用】博图SCL语言之抢答器应用

使用博图的SCL语言来完成多人抢答器的应用案例。 文章目录 目录 文章目录 前言 一、控制要求和I/O分配 1.控制要求 2.I/O分配 3.具体场景 二、编写 1.建立变量 2.编写 1.四路抢答器互锁 2.抢答提示指示灯 3.提前抢答和超时不抢答 4.完善 三、效果 1.仿真效果 2.虚拟工厂效果 …

IPO OC 系列模拟信号隔离转换模块0-1mA /0-10mA/0-20mA/ 4-20mA/0-75mV/0-2.5V/0-5V/0-10V

概述 IPO OC 系列模拟信号隔离放大器是一种将输入信号隔离放大、转换成按比例输出的直流信号混合集成厚模电路。产品广泛应用在电力、远程监控、仪器仪表、医疗设备、工业自控等需要电量隔离测控的行业。该模块内部嵌入了一个高效微功率的电源&#xff0c;可以向输入端和输出端…

缅怀致敬 继往开来 | 萨师煊教授诞辰100周年纪念主题活动在京举行

2022年12月27日是我国数据库学科奠基人——萨师煊教授诞辰100周年纪念日。为缅怀萨师煊先生对我国数据库领域做出的突出贡献&#xff0c;弘扬萨师煊先生敢为人先、严谨治学的高尚品格&#xff0c;“萨师煊教授诞辰100周年纪念活动”27日在北京举行。本次纪念活动由中国人民大学…

云原生丨手把手教你使用zabbix监控postgresql数据库(超详细讲解)

文章目录一、前言二、什么是zabbix三、zabbix安装步骤四、监控postgresql实现步骤一、前言 对于运维人员来说&#xff0c;监控是非常重要的&#xff0c;因为如果想要保证线上业务整体能够稳定运行&#xff0c;那么我们则需要实时关注与其相关的各项指标是否正常。 而一个业务…

数据结构-图

1、图的基本概念 &#xff08;1&#xff09;定义 图是一种较为复杂的非线性结构。 图就是由顶点的有穷非空集合和顶点之间的边组成的集合。通常表示为G(V,E)&#xff0c;其中&#xff0c;G表示一个图&#xff0c;V表示顶点的集合&#xff0c;E表示边的集合。 &#xff08;2&…

当年谷歌为什么退出中国?

《时代》周刊中&#xff0c;百度公司创始人兼CEO李彦宏成为封面人物&#xff0c;成为中国互联网登《时代》的第一人。 而专访中的一段话&#xff0c;却让一段往事再次成为了舆论热议的焦点。专访中&#xff0c;李彦宏表示&#xff0c;谷歌当年退出中国是因为迫于百度给予的市场…

高可用软件什么意思?哪些高可用软件好用?

你知道高可用软件什么意思吗&#xff1f;哪些高可用软件好用&#xff1f;这里我们小编就给大家简单回答一下这两个问题。希望能对大家有用。 高可用软件什么意思&#xff1f; 所谓高可用是指系统无中断地执行其功能的能力&#xff1b;因此高可用软件是指具备处理能力&#xff…

SpringBoot 之自动装配简单使用

什么是自动装配&#xff1f; Spring Boot 自动装配是指 Spring Boot 应用程序在启动时&#xff0c;框架会自动根据应用程序的配置来创建和连接各种对象之间的依赖关系。这意味着&#xff0c;在应用程序中使用的对象可以通过声明它们的依赖关系来自动创建&#xff0c;而不需要人…

Linux中wait()函数

编程过程中&#xff0c;有时需要让一个进程等待另一个进程&#xff0c;最常见的是父进程等待自己的子进程&#xff0c;或者父进程回收自己的子进程资源包括僵尸进程。这里简单介绍一下系统调用函数&#xff1a;wait() 函数原型是#include <sys/types.h>#include <wai…

GPT学习路线分享

初代GPT-3展示了三个重要能力&#xff1a; 语言生成&#xff1a;遵循提示词&#xff08;prompt&#xff09;&#xff0c;然后生成补全提示词的句子。这也是今天人类与语言模型最普遍的交互方式。 上下文学习 (in-context learning): 遵循给定任务的几个示例&#xff0c;然后为…