AOP在SpringBoot中的实现

本文展示了如何在SpringBoot应用中使用AOP进行日志记录。通过创建切面、注解和控制器方法,实现在每个控制器方法执行前后记录相关日志,包括请求URL、方法名和参数信息。
摘要由CSDN通过智能技术生成

在SpringBoot应用中实现一个AOP Demo,主要功能就是在控制器方法执行前和执行后记录一些日志。
1、application.yml

logging:
  level:
    root: info
  file:
    #path参数对name不起作用,默认文件名为spring.log
    path:  d:\\aopLogs
    #name:   d:\\aopLogs\mySpring.log

2、注解文件

package com.space.aspect.anno;

import java.lang.annotation.*;
/**
 * @package  com.space.aspect.anno
 * @file  SysUserInfo .java
 * @description
 * @author spring
 * @time  2020/5/19 17:04
 * @version 1.0
 */
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface SysUserInfo {
    String  myName() default "spring";
    String  myAge() default "18";
}

3、切面定义文件

package com.space.aspect.aspect;


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.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;


/**
 * @author spring
 * @version 1.0
 * @package com.space.aspect.aspect
 * @file SysUserInfoAspect.java
 * @description
 * @time 2020/5/19 17:06
 */
@Aspect
@Component
public class SysUserInfoAspect {
    private static Logger logger=LoggerFactory.getLogger(SysUserInfoAspect.class);
    @Pointcut("@annotation(com.space.aspect.anno.SysUserInfo)")
    public void userPointCut() {
    }

    @Around("userPointCut()")
    public Object doAround(ProceedingJoinPoint point) throws Throwable {
        System.out.println("enter SysUserInfoAspect/doAround......");
        logger.info("enter SysUserInfoAspect/doAround......");
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).
                getRequest();
        String requestUrl = request.getRequestURL().toString();
        System.out.println("requestUrl is " + requestUrl);
        logger.info("requestUrl is "+requestUrl);
        //修改request参数;
        MethodSignature signature = (MethodSignature) point.getSignature();
        String methodName = signature.getName();
        String[]  allParams=signature.getParameterNames();
        System.out.println("methodName="+methodName);
        logger.info("methodName is "+methodName);
        for(String oneParameterName:allParams){
            System.out.println(oneParameterName);
            logger.info(oneParameterName);
        }
        Method method = signature.getMethod();
        SysUserInfo sysUserInfo= method.getAnnotation(SysUserInfo.class);
        logger.info(sysUserInfo.myName());
        logger.info(sysUserInfo.myAge());
        Object[] args = point.getArgs();
        for(int i=0;i<args.length;i++){
            System.out.println(args[i]);
            logger.info(args[i].toString());
            args[i]=args[i].toString().concat("+111");
        }
        Object result=point.proceed(args);
        System.out.println("end end end......");
        logger.info("end end end......");
        return result;
    }
}

4、定义控制器方法

package com.space.aspect.controller;


import com.space.aspect.anno.SysUserInfo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @author spring
 * @version 1.0
 * @package com.space.aspect.controller
 * @file TestUserController.java
 * @description
 * @time 2020/5/19 17:29
 */
@RestController
public class TestUserController {
    @SysUserInfo(myName="pig",myAge = "100")
    @GetMapping("/showOneUserInfo")
    public Object showOneUserInfo(@RequestParam("myName") String name1,
                                  @RequestParam("myAge") String age1){
        Map<String,Object> user=new HashMap< >(2);
        user.put("name",name1);
        user.put("age",age1);
        return user;
    }
}

5、运行
浏览器URL
http://localhost:8080/showOneUserInfo?myName=spring&&myAge=10
浏览器运行结果为------
{“name”:“spring+111”,“age”:“10+111”}
日志文件D:\aopLogs\spring.log中AOP产生的内容为------
2020-06-14 17:13:45.470 INFO 7432 — [http-nio-8080-exec-4] c.space.aspect.aspect.SysUserInfoAspect : enter SysUserInfoAspect/doAround…
2020-06-14 17:13:45.471 INFO 7432 — [http-nio-8080-exec-4] c.space.aspect.aspect.SysUserInfoAspect : requestUrl is http://localhost:8080/showOneUserInfo
2020-06-14 17:13:45.472 INFO 7432 — [http-nio-8080-exec-4] c.space.aspect.aspect.SysUserInfoAspect : methodName is showOneUserInfo
2020-06-14 17:13:45.473 INFO 7432 — [http-nio-8080-exec-4] c.space.aspect.aspect.SysUserInfoAspect : name1
2020-06-14 17:13:45.473 INFO 7432 — [http-nio-8080-exec-4] c.space.aspect.aspect.SysUserInfoAspect : age1
2020-06-14 17:13:45.474 INFO 7432 — [http-nio-8080-exec-4] c.space.aspect.aspect.SysUserInfoAspect : 二师兄
2020-06-14 17:13:45.475 INFO 7432 — [http-nio-8080-exec-4] c.space.aspect.aspect.SysUserInfoAspect : 1
2020-06-14 17:13:45.476 INFO 7432 — [http-nio-8080-exec-4] c.space.aspect.aspect.SysUserInfoAspect : spring
2020-06-14 17:13:45.477 INFO 7432 — [http-nio-8080-exec-4] c.space.aspect.aspect.SysUserInfoAspect : 10
2020-06-14 17:13:45.477 INFO 7432 — [http-nio-8080-exec-4] c.space.aspect.aspect.SysUserInfoAspect : end end end…

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

遥望山顶的小孩

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

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

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

打赏作者

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

抵扣说明:

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

余额充值