基于SpringAop实现日志管理

1、加入aop依赖

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

2、创建一个接口对象

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Action {
    String name();
}

3、定义切面类

@Aspect
@Component
public class LogAspect {
    @Pointcut("@annotation(com.swpu.todolist.util.Action)")
    public void annotationPointCut() {};
    @After("annotationPointCut()")
    public void after(JoinPoint joinPoint) {
        //获取参数名
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        String[] strings = signature.getParameterNames();
        //获取参数值
        Object[] objects= joinPoint.getArgs();
        //获取切入的方法名
        Method method = signature.getMethod();
        //获取切入的类
        Class aClass = joinPoint.getTarget().getClass();
        System.out.println("Args: "+Arrays.toString(objects));
        System.out.println("parameter: "+ Arrays.toString(strings));
        System.out.println("ClassName: " +aClass.getName());
        System.out.println("method: "+method.getName());
    }
}

4、添加到方法上

@Action(name = "删除已经完成")
@RequestMapping(value = "/deleteTableDone",method = RequestMethod.POST)
@ResponseBody
public String  deleteTableDone(@RequestBody TableDone tabledone ){
    listService.deleteTableDone(tabledone);
    return "succ";
}

5、运行结果

Args: [TableDone(id=46, thing=null, createDate=null, doneDate=null, user_id=0)]
parameter: [tabledone]
ClassName: com.swpu.todolist.controller.ListController
method: deleteTableDone

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值