栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

spring AOP实现日志管理

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

spring AOP实现日志管理

前言

spring AOP是使用spring提供的切面来横向拓展项目,使得一些和主业务无关的操作尽可能实现解耦,比如说我们经常使用的日志。

注: 需要用到pom文件


   org.springframework.boot
   spring-boot-starter-aop

实操 1、编写一个自定义的注解@MyLog
@Target({ElementType.PARAMETER, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@documented
public  @interface MyLog {
    
    String type();
    
    String msg();
}
2、entity实体类(日志操作的实体)
package com.example.demo001.entity;

import java.io.Serializable;
import java.util.Date;

public class Mylog implements Serializable {

    private Long id;

    private String type;

    private String msg;

    private Date time;

    private String args;

    public Mylog() {
    }

    public Mylog(Long id, String type, String msg, Date time,String args) {
        this.id = id;
        this.type = type;
        this.msg = msg;
        this.time = time;
        this.args = args;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Date getTime() {
        return time;
    }

    public void setTime(Date time) {
        this.time = time;
    }

    public String getArgs() {
        return args;
    }

    public void setArgs(String args) {
        this.args = args;
    }
}

3、mapper层(插入日志)
@Mapper
public interface MylogMapper {

    @Insert("insert into mylog (type,msg,time,args) values (#{type},#{msg},#{time},#{args})")
    int insert(Mylog mylog);
}
4、切面类
//声明这是一个组件
@Component
//声明这是一个切面Bean
@Aspect
public class TestAspect {


    @Autowired
    private MylogMapper mapper;

    
    @Pointcut("@annotation(com.example.demo001.aop.MyLog)")
    public void aspect(){   }

    //最终通知
    //returning能够将目标方法的返回值传到切面增强方法里
    //声明rvt时指定的类型会限制目标方法必须返回指定类型(String)的值或没有返回值
    //此处将rvt的类型声明为Object,意味着对目标方法的返回值不加限制
    @AfterReturning(pointcut ="aspect()",returning = "rvt")
    public void AfterReturning(String rvt){
    }

    //异常通知
    //声明e时指定的类型会限制目标方法必须抛出指定类型的异常
    //此处将e的类型声明为Throwable,意味着对目标方法抛出的异常不加限制
    @AfterThrowing(pointcut="aspect()",throwing="e")
    public void AfterThrowing(Throwable e){
        System.out.println("--------AfterThrowing方法开始执行:"+e);
    }

    public Map converMap(Map paramMap) {
        Map rtnMap = new HashMap();
        for (String key : paramMap.keySet()) {
            rtnMap.put(key, paramMap.get(key)[0]);
        }
        return rtnMap;
    }


    //@Around注解可以用来在调用一个具体方法前和调用后来完成一些具体的任务。
    //功能很强大,可以深入了解下
    @Around("aspect()")
    public Object Around(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("--------Around方法开始执行");

        Mylog mylog = new Mylog();

        //获取自定义注解里面的值
        Method method = ((MethodSignature)joinPoint.getSignature()).getMethod();
        MyLog myLog = method.getAnnotation(MyLog.class);
        String type = myLog.type();
        String msg = myLog.msg();


        


        Object[] objs = joinPoint.getArgs();
        String[] argNames = ((MethodSignature) joinPoint.getSignature()).getParameterNames(); // 参数名
        Map args = new HashMap();
        for (int i = 0; i < objs.length; i++) {
            args.put(argNames[i], objs[i]);
        }
        System.err.println("入参:"+ args);

       
        mylog.setType(type);
        mylog.setMsg(msg);
        mylog.setArgs(args.toString());
        mylog.setTime(new Date());
        mapper.insert(mylog);

        Object result =joinPoint.proceed();
        return result;

    }

}

5、controller类
package com.example.demo001.controller;

import com.example.demo001.aop.MyLog;
import com.example.demo001.entity.User;
import com.example.demo001.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
public class UserController {

    @Autowired
    private UserMapper userMapper;

    @MyLog(type = "查询", msg = "用户列表查询")  //这里使用的自定义注解
    @RequestMapping("users")
    public List users(Long id, String name ) {
        System.out.println("------"+id+"------"+name+"------");
        return userMapper.select();
    }
}


测试

这里查询的是用户列表,然后经过切面,获取操作参数入库。

数据库中已经获取了切面的信息了,大功告成,这里还能获取一些类信息,方法信息,你也可以把操作人等信息也存储起来,增删改查的操作也都是如此。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/770369.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号