springaop实现日志记录

写了一下午的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调用接口 或者直接访问接口

在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一路向楠i

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值