打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
java注解日志记录到数据库

1. pom添加依赖包

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

2.配置文件application.yml添加
spring:
    aop:
    auto: true

3 创建实体类

package com.spring4all.entity;import java.io.Serializable;import java.util.Date;/** * @author shafei * @version 1.0 * @date 10:28 2019/9/7 * @fun */public class SysLogDO implements Serializable {    private Long id;    private String username; //用户名    private String operation; //操作    private String method; //方法名    private String params; //参数    private String ip; //ip地址    private Date createDate; //操作时间    public Long getId() {        return id;    }    public void setId(Long 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 getParams() {        return params;    }    public void setParams(String params) {        this.params = params;    }    public String getIp() {        return ip;    }    public void setIp(String ip) {        this.ip = ip;    }    public Date getCreateDate() {        return createDate;    }    public void setCreateDate(Date createDate) {        this.createDate = createDate;    }}

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

package com.spring4all.config.intercepors;import java.lang.annotation.*;/** * @author shafei * @version 1.0 * @date 10:06 2019/9/7 */@Target(ElementType.METHOD) //注解放置的目标位置,METHOD是可注解在方法级别上@Retention(RetentionPolicy.RUNTIME) //注解在哪个阶段执行@Documented //生成文档public @interface MyLog {    String value() default "";}

5.AOP实现:

package com.spring4all.config;import com.alibaba.fastjson.JSON;import com.spring4all.config.intercepors.MyLog;import com.spring4all.entity.SysLogDO;import com.spring4all.service.SysLogService;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;/** * @author shafei * @version 1.0 * @date 10:08 2019/9/7 * fun:  */@Aspect@Componentpublic class SysLogAspect {    @Autowired    private SysLogService sysLogService;    //定义切点 @Pointcut    //在注解的位置切入代码    @Pointcut("@annotation( com.spring4all.config.intercepors.MyLog)")    public void logPoinCut() {    }    //切面 配置通知    @AfterReturning("logPoinCut()")    public void saveSysLog(JoinPoint joinPoint) {        System.out.println("切面。。。。。");        //保存日志        SysLogDO sysLog = new SysLogDO();        //从切面织入点处通过反射机制获取织入点处的方法        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);//保存获取的操作        }        //获取请求的类名        String className = joinPoint.getTarget().getClass().getName();        //获取请求的方法名        String methodName = method.getName();        sysLog.setMethod(className + "." + methodName);        //请求的参数        Object[] args = joinPoint.getArgs();        //将参数所在的数组转换成json        String params = JSON.toJSONString(args);        sysLog.setParams(params);        sysLog.setCreateDate(new Date());        //获取用户名        sysLog.setUsername("shafei");        //获取用户ip地址//        HttpServletRequest request = HttpContextUtils.getHttpServletRequest();        sysLog.setIp("192.168.3.11");        //调用service保存SysLog实体类到数据库//        sysLogService.save(sysLog);        System.out.println("将日志记录到数据库");    }}

6.接下来就可以在需要监控的方法上添加 aop的自定义注解

格式为 @+自定义注解的类名 

@Controller@RequestMapping("/test")public class UserController {     @MyLog(value = "测试一下")    @GetMapping("/test")    @ResponseBody    public String test(){        return "test";    }}

 

说明:

记录日志的服务层接口SysLogService需要另外写,这个应该都会写;

 

 

参考https://www.jianshu.com/p/d0bbdf1974bd

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
[直呼内行] 注解开发才是简洁之道,让同行直呼内行的Java注解技术
SpringBoot使用AOP(环绕通知)完成对用户操作的日志记录
超详细 Spring @RequestMapping 注解使用技巧
Java中如何动态创建接口的实现
张龙 Annotation学习笔记
Spring系列(十三):AOP相关知识笔记
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服