SpringBoot项目中---AOP实现日志的方法

先导包

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

写AOP配置类

 在方法上配置织入方法的织入点
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.text.SimpleDateFormat;
import java.util.Date;

@Aspect
@Component
public class MyAspect {

    /**
     * 在目标方法执行完成之后执行增强逻辑
     * pointcut切入点表达式指定了要增强的方法
     * returning属性指定了目标方法的返回值,并将其传递给增强方法的参数
     */
    @AfterReturning(pointcut = "execution(* com.atguigu.ssyx..*(..))", returning = "result")
//    joinPoint表示连接点,可以获取到目标方法的信息
    public void afterMethodExecution(JoinPoint joinPoint, Object result)  {

        System.out.println("");
//      切入点获取签名获取方法名称
        String methodName = joinPoint.getSignature().getName();
//      切入点获取传入参数
        Object[] arguments = joinPoint.getArgs();
//      切入点通过反射getClass获取类名
        String className = joinPoint.getTarget().getClass().getName();
//      打印类名.方法名 参数列表
        System.out.println("=========================="+"方法名称:"+ className + "." + methodName +"==============================" );
        System.out.println("参数列表:");
//      如果参数列表不为空并且长度大于0
        if (arguments != null && arguments.length > 0) {
//          切入点获取签名
            Signature signature = joinPoint.getSignature();
            Method method = getMethod(signature);
            if (method != null) {
//              通过method对象获取参数列表
                Parameter[] parameters = method.getParameters();
//              for循环打印参数列表
                for (int i = 0; i < parameters.length; i++) {

                    System.out.print(parameters[i].getName() + ":" + getArgumentValue(arguments[i]) + ",");
                }
            }
        } else {
            System.out.print("无");
        }
        System.out.println(" ");
        System.out.println(" ");

        try {
//          打印返回值,将对象转成json串格式化输出
            ObjectMapper objectMapper = new ObjectMapper();
            objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
            System.out.print("返回值:\n" + objectMapper.writeValueAsString(result));
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        System.out.println("");

        System.out.println("===================================="+printTime()+"=============================");

    }

    private Method getMethod(Signature signature) {
        if (signature instanceof MethodSignature) {
            MethodSignature methodSignature = (MethodSignature) signature;
            return methodSignature.getMethod();
        }
        return null;
    }

    private String getArgumentValue(Object argument) {
        if (argument != null) {
            try {
                ObjectMapper objectMapper = new ObjectMapper();
                objectMapper.enable(SerializationFeature.INDENT_OUTPUT);

                return  objectMapper.writeValueAsString(argument);
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
        }
        return "null";
    }

    private String printTime(){
        // 创建日期对象
        Date currentDate = new Date();
        // 创建日期格式化对象,指定日期格式
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 格式化日期对象为字符串
        String formattedDate = dateFormat.format(currentDate);
        // 打印当前时间
        return("当前方法执行时间: " + formattedDate);
    }



}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值