首页IT科技springboot aop操作日志(使用Spring AOP实现系统操作日志记录)

springboot aop操作日志(使用Spring AOP实现系统操作日志记录)

时间2025-09-09 18:27:01分类IT科技浏览5819
导读:使用Spring AOP实现系统操作日志记录 一、什么是Spring...

使用Spring AOP实现系统操作日志记录

一                、什么是Spring

Spring 是一个广泛应用的J2EE框架                ,是针对bean的生命周期进行管理的轻量级容器                          ,主要由Spring Core                          、Spring AOP         、Spring ORM                、Spring DAO                         、Spring Context         、Spring Web        、Spring Web MVC七大模块组成                。

二                         、什么是AOP

AOP是Aspect Oriented Programming的缩写         ,是面向切面编程                ,针对业务处理过程中的切面进行提取                         ,降低了耦合度         ,提高了可重用性        ,经常用于日志记录                 、性能统计        、安全控制                         、事务处理                 、异常处理等                          。AOP分为静态代理和动态代理                         ,常见的AOP实现有AspectJ                 ,Spring AOP        ,其中Aspect是属于静态代理                         ,Spring AOP是动态代理                 ,Spring AOP实现又是采用的JDK动态代理和CGLib动态代理两种方式         。

三、相关术语

Aspect: 切面,在Spring中使用@Aspect注解标识                         ,该类封装一些具体的操作                          ,例如记录日志                。

Joinpoint: 连接点,是指的是在程序运行过程中的某个阶段                         。

Pointcut: 切入点                ,定义的一个或者一组方法                          ,当程序执行到这些切入点时         ,会产生通知         。

@Before: 前置通知                ,在连接点之前执行的通知                         ,不能阻止连接点前的执行        。

@AfterReturning: 在连接点正常完成后执行的通知         ,不抛出异常的情况                         。

@AfterThrowing: 和上方刚好补充        ,在连接点抛出异常是执行的通知                 。

@After: 在连接点退出时执行的通知        。异常退出和正常退出都会执行                         。

@Around: 环绕通知                         ,可以在方法前后加入自定义的操作                 ,相当于环绕包围        ,并且可以决定方法是否执行                 。

四                         、代码实现

@Aspect @Component @Slf4j public class SysLogAspect { /** * Controller层切点,SysLog是自定义的注解 */ @Pointcut("@annotation(com.xxx.xxx.SysLog)") public void SysLogPointAspect() { } /** * @Description 环绕通知 用于拦截Controller层记录用户的操作 */ @Around("SysLogPointAspect()") public Object around(ProceedingJoinPoint joinPoint) throws Throwable { //*========控制台输出=========*// log.info("==============访问请求==============\n"); HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); String requireType = request.getMethod(); String requireUrl = request.getRequestURI(); // 获取注解信息 MethodSignature signature = (MethodSignature) joinPoint.getSignature(); Method method = signature.getMethod(); SysLog syslog = method.getAnnotation(SysLog.class); //*========数据库日志=========*// // 保存日志到数据库..... // 方法执行开始 long beginTime = System.currentTimeMillis(); //执行方法 Object result = joinPoint.proceed(); //执行时长(毫秒) long execTime = System.currentTimeMillis() - beginTime; return result; } }

原文链接:https://monkey.blog.xpyvip.top/archives/shi-yong-springaop-shi-xian-xi-tong-cao-zuo-ri-zhi-ji-lu

创心域SEO版权声明:以上内容作者已申请原创保护,未经允许不得转载,侵权必究!授权事宜、对本内容有异议或投诉,敬请联系网站管理员,我们将尽快回复您,谢谢合作!

展开全文READ MORE
怎么通过python爬虫赚钱(如何使用Python爬虫赚取收入) OpenGLES 映射缓冲区对象(OpenGL ES EAGLContext 和 EGLContext)