springboot使用aop记录日志

一、引入aop启动器

		<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>

二、 自定义切点注解

@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface LogAnnotation {

    String value() default "";

}

三、在方法上添加自定义注解(切点)

@LogAnnotation("自定义注解参数")
    @GetMapping
    public String index(){
//        String s = null;
//        s.toString();
        System.out.println("hello world");
        return "hello world";
    }

四、编写切面类

package com.hdu.springsecurity.common;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.Date;

@Component
@Aspect
@Slf4j
public class LogAspect {

    @Pointcut("@annotation(com.hdu.springsecurity.common.LogAnnotation)")
    public void point(){};

    @Before("point()")
    public void beforeAdvice(){
        log.info("前置通知{}",new Date());
    }

    @Around("point()")
    public void aroundAdvice(ProceedingJoinPoint joinPoint) throws Throwable {
        log.info("环绕前通知{}",new Date());
        Object result = joinPoint.proceed();
        MethodSignature signature = (MethodSignature)joinPoint.getSignature();
        Method method = signature.getMethod();
        LogAnnotation annotation = method.getAnnotation(LogAnnotation.class);
        String value = annotation.value();
        log.info("注解信息{}",value);
        log.info("环绕后通知{}",new Date());
    }
    
    @AfterReturning("point()")
    public void afterReturningAdvice(){
        log.info("后置通知{}",new Date());
    }
    
    @After("point()") //finally
    public void afterAdvice(){
        log.info("最终通知{}",new Date());
    }
    
    @AfterThrowing("point()")
    public void afterThrowingAdvice(){
        log.info("异常通知{}",new Date());
    }

}

测试结果(无异常)

15:26:15 [main] INFO  c.h.springsecurity.common.LogAspect - 环绕前通知Mon Apr 17 15:26:15 CST 2023
15:26:15 [main] INFO  c.h.springsecurity.common.LogAspect - 前置通知Mon Apr 17 15:26:15 CST 2023
hello world
15:26:15 [main] INFO  c.h.springsecurity.common.LogAspect - 后置通知Mon Apr 17 15:26:15 CST 2023
15:26:15 [main] INFO  c.h.springsecurity.common.LogAspect - 最终通知Mon Apr 17 15:26:15 CST 2023
15:26:15 [main] INFO  c.h.springsecurity.common.LogAspect - 注解信息自定义注解参数
15:26:15 [main] INFO  c.h.springsecurity.common.LogAspect - 环绕后通知Mon Apr 17 15:26:15 CST 2023

测试有异常

15:28:53 [main] INFO  c.h.springsecurity.common.LogAspect - 环绕前通知Mon Apr 17 15:28:53 CST 2023
15:28:53 [main] INFO  c.h.springsecurity.common.LogAspect - 前置通知Mon Apr 17 15:28:53 CST 2023
15:28:53 [main] INFO  c.h.springsecurity.common.LogAspect - 异常通知Mon Apr 17 15:28:53 CST 2023
15:28:53 [main] INFO  c.h.springsecurity.common.LogAspect - 最终通知Mon Apr 17 15:28:53 CST 2023

结果:当出现异常:最终通知还是会执行

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值