面向切面编程:操作日志
在项目中,操作日志至关重要,操作日志记录的是:谁干了什么事;
我们可以利用面向切面编程实现操作日志的打印与记录:
1.首先,添加依赖:
<!--spring aop 依赖--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-aop</artifactId></dependency>
2.自定义注解:
package com.qcby.demo0816.anno;import com.qcby.demo0816.constant.GlobalConstant;import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;/*** TODO 自定义日志注解* @author DB* <br>CreateDate 2021/9/17 10:26*/@Target(ElementType.METHOD) // 该注解可以作用于那些类型元素上:类、方法、字段
@Retention(RetentionPolicy.RUNTIME) // 运行时生效
public @interface SelfLog {/** @Author liu-miss* @Description //TODO 操作类型**/int type() default GlobalConstant.LOG_TYPE_SELECT;/** @Author liu-miss* @Description //TODO 模块**/String module() default "";/** @Author liu-miss* @Description //TODO 名称参数**/String name() default "name";}
操作类型:增删改查
3.切面:
package com.qcby.demo0816.aspect;import com.qcby.demo0816.anno.SelfLog;
import com.qcby.demo0816.common.context.QcbyContext;
import com.qcby.demo0816.entity.OperateLog;
import com.qcby.demo0816.entity.User;
import com.qcby.demo0816.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.time.LocalDateTime;
import java.util.Objects;/*** TODO 操作日志切面** @author DB* <br>CreateDate 2021/9/17 10:48*/
@Aspect
@Component
@Slf4j
public class SelfLogAspect {@AutowiredHttpServletRequest request;/*** @Author liu-miss* @Description //TODO 切入点**/@Pointcut("@annotation(com.qcby.demo0816.anno.SelfLog)")public void pointCut(){}/*** @Author liu-miss* @Description //TODO 前置通知**/@Before("pointCut()")public void testBefore(JoinPoint joinPoint ){log.info("我是前置通知");}@After("pointCut()")public void after(){log.info("我是后置通知");}@AfterReturning("pointCut()")public void afterReturning(){log.info("我是AfterReturning通知");}@AfterThrowing("pointCut()")public void afterThrowing(){log.info("我是AfterThrowing通知");}@Around("pointCut()")public Object around (ProceedingJoinPoint joinPoint) throws Throwable{log.info("进入Around通知....前");//从切面织入点处通过反射机制获取织入点处的方法MethodSignature signature = (MethodSignature) joinPoint.getSignature();//获取切入点所在的方法Method method = signature.getMethod();String token = request.getHeader("token");Long userId = null;if (!StringUtil.isEmpty(token)){User userDb = QcbyContext.getUser(token);userId = userDb.getId();}SelfLog selfLog = method.getAnnotation(SelfLog.class);log.info("注解selfLog:{}", selfLog);OperateLog operateLog = new OperateLog(); //这是操作日志实体类对象operateLog.setCreateTime(LocalDateTime.now());operateLog.setType(selfLog.type());operateLog.setUserId(userId);String nameValue = selfLog.name();log.info("nameValue的值为:{}",nameValue);String name = null;//获取所有参数值Object[] objects = joinPoint.getArgs();// 获取是所有参数名字String[] paramNames = signature.getParameterNames();for (int i =0;i<paramNames.length;i++){if (Objects.equals(nameValue,paramNames[i]) && objects[i] != null){name = objects[i].toString();}}log.info("name的值为:{}",name);/*** 进行业务操作*/// 执行目标方法Object r = joinPoint.proceed();/*** 目标方法执行完毕之后,执行的业务增强操作*/log.info("结束Around通知....后");return r;}
}
4.在我们的controller的方法上加上注解:
5.即可记录对应的操作
总结:
自定义注解加在方法上是要知道该方法是什么;
切面类放在了aspect包下,切面中某些语句的作用:
//切入点,该切面与哪个注解相关联
@Pointcut("@annotation(com.qcby.demo0816.anno.SelfLog)")
//前置通知
@Before("pointCut()")
//后置通知
@Before("pointCut()")
//环绕通知
@Around("pointCut()")
面向切面编程:操作日志相关推荐
- 面向切面编程-日志切面应用
简介: AOP:面向切面编程,即拓展功能不通过修改源代码实现,采用横向抽取机制,取代了传统的纵向继承体系重复性代码.在运行期通过代理方式向目标类织入增强代码. Aspecj:Aspecj 是一个基于j ...
- AOP面向切面编程之全局日志打印/统计接口耗时
目录 一.什么是AOP 二.AOP使用场景 三.使用AOP的好处 四.先举个例子理解AOP面向切面编程 五.Spring5.X的AOP切入点表达式有这些种写法 六.实战基于Spring的AOP快速实现 ...
- SpringBoot面向切面编程-用AOP方式管理日志
面向切面编程 认识AOP AOP(Aspect Oriented Program,面向切面编程)把业务功能分为核心.非核心两部分. 核心业务功能 非核心业务功能 用户登录,增加数据,删除数据 性能统计 ...
- 【spring】初识aop(面向切面编程) 使用jdk动态代理
BankServiceIImple.java 代码实现: package com.zzxtit.aop;import java.math.BigDecimal;public interface Ban ...
- Spring in Action 入门之面向切面编程AOP
注明:这篇文章一是当成学习笔记,二是给大家提供另一个快速理解学习Spring的参考.欢迎留言讨论,持续更新中~ (该部分是Spring的面向切面编程AOP) 第四章 通知Bean 在软件编程中,散布于 ...
- 【AOP 面向切面编程】AOP 简介 ( AspectJ 简介 | AspectJ 下载 )
文章目录 一.AOP 简介 二.AspectJ 简介 三.AspectJ 下载 一.AOP 简介 AOP 是 Aspect Oriented Programming 的缩写 , 面向切面编程 ; 利用 ...
- 【SSM框架系列】Spring 的 AOP(面向切面编程)
什么是 AOP AOP 为 Aspect Oriented Programming 的缩写,意思为面向切面编程,是通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术. AOP 是 OOP ...
- 【ABP杂烩】面向切面编程(AOP)知识总结
目录 1.存在问题 2.AOP的概念 3.AOP应用范围 3.AOP实现方式 4.应用举例 5.结束语 本文在学习[老张的哲学]系列文章AOP相关章节后,自己归纳总结的笔记. 1.存在问题 最近,其它 ...
- 面向切面编程AOP的最佳入门示例
1.AOP简单上手 AOP(Aspect Oriented Programming),意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.它通过对既有程序定义一个切入点 ...
最新文章
- QIIME 2教程. 28社区Community(2020.11)
- 高性能集群软件Keepalived的介绍以及安装与配置
- 基于SSM实现个人健康管理系统
- MongoDB中_id(ObjectId)生成
- Hello World -- Java Web版(Java Web 入门教程)
- docker image aarch64 x86_64_「docker」交叉编译适用于ARM平台的Docker源码
- chrome浏览器中遇到bug【Error in event handler: TypeError: Cannot read property 'name' of undefined】
- 图像局部显著性—点特征(FREAK)
- python输出文本 去掉引号_Python可以在文本文件中读取时从字符串中删除双引号吗?...
- python sqlite3事务_python使用上下文管理器实现sqlite3事务机制
- 不允许指针指向不完整的类类型_8.7 C语言动态内存分配与指向它的指针变量
- python中文教程github_GitHub - Virile-Tao/python_data_structures_and_algorithms: Python 中文数据结构和算法教程...
- CMYK,RGB颜色对照表
- 六度分离 (Floyd算法)
- AWS申请邮件域名和ip绑定
- 樱花何处有?动态樱花飘落图
- 读书笔记——Photoshop设计精讲精练
- 浙大新增机器人工程、人工智能专业:今年招生,导师阵容强大
- linux设备驱动--字符设备模型
- mysql设置id起点_mysql自增ID起始值修改方法
热门文章
- c#通过反射移除所有事件
- 当你打开网页的时候,世界都发生了什么(1)
- 设计模式之Visitor
- 审批流_审批流的优化从何入手
- micropython lcd12864_Esp8266+ssd1306液晶屏+microPython(2020-09-25)
- 计算机系统-电路设计09-计数器的内部电路实现
- php如何查询mysql数据库字符集_修改及查看mysql数据库的字符集_MySQL
- vbreport8.wpf.viewer 个别电脑不显示_手机听歌不过瘾?一招将Win10电脑变成蓝牙音箱...
- r语言 col_R语言: GARCH模型股票交易量的研究道琼斯股票市场指数
- rss spring 接口_spring mvc: rss(xml)输出