AOP(入门)操作日志记录

编程入门 行业动态 更新时间:2024-10-17 05:33:25

AOP(<a href=https://www.elefans.com/category/jswz/34/1770026.html style=入门)操作日志记录"/>

AOP(入门)操作日志记录

目录

一、添加依赖

二、自定义切入点注解

三、定义业务类

四、对方法进行描述,获取日志

1).在方法上添加切面注解

2).运行方法显示日志结果


一、添加依赖

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

二、自定义切入点注解

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface RequiredLog {String value() default "" ;
}

三、定义业务类

        在AOP编程设计中,我们会通过切面封装切入点(Pointcut)和扩展业务逻辑(Around,…)的定义

@Slf4j
@Component
@Aspect
public class LogAspect {//1.切入点@Pointcut("@annotation(com.jt.aop.RequiredLog)")public void doLog(){};//2.扩展业务逻辑@Around("doLog()")public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {log.debug("before:{}", System.currentTimeMillis());Object result = joinPoint.proceed();log.debug("after:{}",System.currentTimeMillis());return result;}
}

四、对方法进行描述,获取日志

1).在方法上添加切面注解

@RequiredLog //自定义切面注解
@PostMapping("/upload/") 
public String uploadFile(MultipartFile uploadFile) {...}

2).运行方法显示日志结果

注:spring boot默认打印日志级别是info,如切面业务定义日志级别为debug,需要手动配置日志显示级别

 

 

更多推荐

AOP(入门)操作日志记录

本文发布于:2024-02-06 09:11:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1748178.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:入门   操作   日志   AOP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!