Aop简单实现操作日志记录

  • 项目结构
    项目结构

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;

/**
 * FileName:
 *
 * @author
 * @description
 * @Date 2020/1/6 18:00
 */
@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();
        // 获取注解的url
        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 请求接口实现记录操作日志

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值