入门)操作日志记录"/>
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(入门)操作日志记录
发布评论