在IDEA 、springboot中使用切面aop实现日志信息的记录到数据库
阅读原文时间:2023年07月09日阅读:1

文章目录

1、导入相关的依赖

   <!--spring切面aop依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>

注意:在application.properties文件里加这样一条配置 spring.aop.auto=true

2、创建要保存的数据信息实体类

package com.example.zheng.pojo;

import java.io.Serializable;

public class Syslog implements Serializable {
    private String id;  //我用的全宇宙唯一的子串串、也是直接用的工具类

    private String username; //用户名

    private String operation; //操作

    private String method; //方法名

    private String createDate; //操作时间,这里可以使用Date来实现。我写的有个工具类。用的String接收

    public String getId() {
        return id;
    }

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

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getOperation() {
        return operation;
    }

    public void setOperation(String operation) {
        this.operation = operation;
    }

    public String getMethod() {
        return method;
    }

    public void setMethod(String method) {
        this.method = method;
    }

    public String getCreateDate() {
        return createDate;
    }

    public void setCreateDate(String createDate) {
        this.createDate = createDate;
    }
}

3 、编写对应的sql语句

create table syslog(
id varchar(50) not null comment '主键',
username varchar(20) not null comment '用户名',
operation varchar(100) not null comment '操作',
method varchar(50) not null comment '方法名',
createDate varchar(50) not null comment '时间',
primary key(id)

)comment='日志'

4、使用spring 的 aop 技术切到自定义注解上,所以先创建一个自定义注解类

package com.example.zheng.pojo;

import java.lang.annotation.*;

/**
 * 自定义注解类
 */
@Target(ElementType.METHOD) //注解放置的目标位置,METHOD是可注解在方法级别上
@Retention(RetentionPolicy.RUNTIME) //注解在哪个阶段执行
@Documented //生成文档

public @interface  Mylog {
    String value() default "";
}

5、 创建aop切面实现类

package com.example.zheng.pojo;

import com.alibaba.druid.support.json.JSONUtils;

import com.example.zheng.Utils.CurrentTime;

import com.example.zheng.Utils.UUIDutils;
import com.example.zheng.service.SysLogService;
import org.apache.shiro.SecurityUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.Date;

/**
 * 系统日志:切面处理类
 */
@Aspect
@Component
public class SysLogAspect {

    @Autowired
    private SysLogService sysLogService;//将数据写入数据库的操作

    //定义切点 @Pointcut
    //在注解的位置切入代码
    @Pointcut("@annotation( com.example.zheng.pojo.Mylog)")
    public void logPoinCut() {
    }

    //切面 配置通知
    @AfterReturning("logPoinCut()")
    public void saveSysLog(JoinPoint joinPoint) {
        System.out.println("切面。。。。。");
        //保存日志
        Syslog sysLog = new Syslog();

        //从切面织入点处通过反射机制获取织入点处的方法
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        //获取切入点所在的方法
        Method method = signature.getMethod();

        //获取操作
        Mylog myLog = method.getAnnotation(Mylog.class);
        if (myLog != null) {
            String value = myLog.value();
            sysLog.setOperation(value);//保存获取的操作
        }

        //设置id
        String id = UUIDutils.getUUID();
        sysLog.setId(id);

        //获取请求的类名
        String className = joinPoint.getTarget().getClass().getName();
        //获取请求的方法名
        String methodName = method.getName();
        sysLog.setMethod(className + "." + methodName);

        //获取时间
        String time = CurrentTime.getCurrentTime();
        sysLog.setCreateDate(time);
        //获取用户名
        //拿到当前用户的信息、我这里使用的shiro。直接从shiro中获取当前用户信息
        Customer parent  = (Customer) SecurityUtils.getSubject().getPrincipal();
        sysLog.setUsername(parent.getUsercount());

        //调用service保存SysLog实体类到数据库
        sysLogService.addLog(sysLog);
    }
}

6、在实体类中的具体应用

接下来就可以在需要监控的方法上添加 aop的自定义注解
格式为 @+自定义注解的类名 @MyLog

7、实现的效果

以下是我测试的一些

8、service接口

package com.example.zheng.service;

import com.example.zheng.pojo.Syslog;

public interface SysLogService {

    //写入日志
    int addLog(Syslog syslog);

}

9、接口的实现类

package com.example.zheng.service.impl;

import com.example.zheng.mapper.SysLogMapper;
import com.example.zheng.pojo.Syslog;
import com.example.zheng.service.SysLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SysLogServiceImpl implements SysLogService {

    @Autowired
    SysLogMapper sysLogMapper;

    //写入日志
    @Override
    public int addLog(Syslog syslog) {
        return sysLogMapper.addLog(syslog);
    }
}

10、dao层

package com.example.zheng.mapper;

import com.example.zheng.pojo.Syslog;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;

@Mapper    //这个注解表示这个是mybatis的mapeper
@Repository
public interface SysLogMapper {
    //写入日志
    int addLog(Syslog syslog);
}

11、编写的mapper文件

<?xml version="1.0" encoding="UTF8"?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.zheng.mapper.SysLogMapper">

<insert id="addLog" parameterType="com.example.zheng.pojo.Syslog">
    insert into syslog(id,username,operation,method,createDate)
    values (#{id},#{username},#{operation},#{method},#{createDate})
</insert>

</mapper>