写了一下午的springaop实现日志的记录 ,之所以写这么长时间是因为一开始依赖引的有点问题,调了半天,头秃。哈哈哈,该文章只为记录自己的学习笔记,仅供参考,
1.引入依赖
<!-- springboot-aop 切面 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
2.开发自定义注解 记录日志使用。
package com.me.config.aopconfig.aop;
import java.lang.annotation.*;
/**
* Description: .开发注解 记录日志!
* Date: 2022/6/1 18:52
* author: chuliujian
* version: V1.0
*/
//type代表可用放在类上面,method代表可用放在方法上,此处只放在方法上即可
//@Target({ElementType.ANNOTATION_TYPE,ElementType.METHOD})
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface LogDIY {
//模块名称 默认“”
String module() default "";
//操作名称
String operation() default "";
}
3.配置切点和环绕通知异常通知等
package com.me.config.aopconfig.aspect;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
/**
* Description: .aop切面配置切点和环绕通知; //http://t.csdn.cn/B87VF
* Date: 2022/6/1 19:00
* author: chuliujian
* version: V1.0
*/
@Component //spring可用识别到 组件扫描
@Aspect //切面 定义通知和切面的关系;
@Slf4j //记录日志
public class LogAspect {
ThreadLocal<Long> currentTime = new ThreadLocal<>();
/**
* 配置切入点
* @annotation 路径为自开发的注解,的路径地址
*/
@Pointcut("@annotation(com.me.config.aopconfig.aop.LogDIY)")
public void logPointcut() {
// 该方法无方法体,主要为了让同类中其他方法使用此切入点
}
/**
* 配置环绕通知,使用在方法logPointcut()上注册的切入点
*
* @param joinPoint join point for advice
*/
@Around("logPointcut()") //此处为配置切入点的方法;
public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable {
//...一顿操作
log.info("执行方法前");
System.out.println("执行方法前sout");
Object obj = joinPoint.proceed();
//执行函数后打印日志
log.info("执行方法后");
System.out.println("执行方法后sout");
//调用service层保存日志
return obj;
}
/**
* 前置通知
* @param joinPoint
*/
@Before("logPointcut()")
public void doBefore(JoinPoint joinPoint) {
//输出日志
log.info("前置通知");
}
/**
* 配置异常通知
*/
@AfterThrowing(pointcut = "logPointcut()", throwing = "e") //此处为配置切入点的方法;
public void logAfterThrowing(JoinPoint joinPoint, Throwable e) {
}
// 前置通知(@Before):在目标方法调用之前调用通知
// 后置通知(@After):在目标方法完成之后调用通知
// 环绕通知(@Around):在被通知的方法调用之前和调用之后执行自定义的方法
// 返回通知(@AfterReturning):在目标方法成功执行之后调用通知
// 异常通知(@AfterThrowing):在目标方法抛出异常之后调用通知
// 原文链接:https://blog.csdn.net/print_out/article/details/122926456
}
4.controller层测试
package com.me.modules.aoplogtest;
import com.me.config.aopconfig.aop.LogDIY;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* Description: .测试aop的日志收集功能
* Date: 2022/6/1 18:56
* author: chuliujian
* version: V1.0
*/
@RestController
@RequestMapping("/aop")
@Slf4j
public class aopController {
@LogDIY(module="模块名称",operation = "模块操作")
@RequestMapping("/api")
public String testaop(String username){
log.info("hello");
return "你好";
}
@LogDIY(module="模块名称2",operation = "模块操作2")
@RequestMapping("/api2")
public String testaop2(String username){
log.info("hello2");
return "你好2";
}
}
4.使用postman调用接口 或者直接访问接口