- 项目结构
![项目结构](https://i-blog.csdnimg.cn/blog_migrate/31c24144595c8260a30cec49e5061924.png)
1. 自定义注解
@Target({ ElementType.PARAMETER, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface LogAop {
String url() default "";
String action() default "";
}
2 配置切面
package com.example.demo.config.aspect;
import com.example.demo.config.annotation.LogAop;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
@Aspect
@Component
public class LogAspect {
@Pointcut("@annotation(com.example.demo.config.annotation.LogAop)")
public void log() {
}
@Around("log()")
public Object around(ProceedingJoinPoint point) throws Throwable {
MethodSignature signature = (MethodSignature) point.getSignature();
Method method = signature.getMethod();
LogAop actionLog= method.getAnnotation(LogAop.class);
String action = b2bOrderLog.action();
String url = b2bOrderLog.url()
System.out.println("xxx通过"+url+"路径"+action);
return result;
}
}
3 接口添加日志注解
@RestController
@RequestMapping("/hello")
public class DemoController {
@Autowired
private WuxiaService wuxiaService;
@LogAop(action = "进入主页",url = "/hello/index")
@GetMapping("/index")
public String index(){
return "hello";
}
@LogAop(action = "查询所有",url = "/hello/getAll")
@GetMapping("/getAll")
public List<Wuxia> getAll(){
return wuxiaService.selectAll();
}
}
4 请求接口实现记录操作日志
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/c70ce38ee0680170e15eb31b6c41ace4.png)