在IDEA 、springboot中使用切面aop实现日志信息的记录到数据库
文章目录
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>
在IDEA 、springboot中使用切面aop实现日志信息的记录到数据库的更多相关文章
- SpringBoot自定义注解、AOP打印日志
前言 在SpringBoot中使用自定义注解.aop切面打印web请求日志.主要是想把controller的每个request请求日志收集起来,调用接口.执行时间.返回值这几个重要的信息存储到数据库里 ...
- Spring 在XML中声明切面/AOP
在Spring的AOP配置命名空间中,我们能够找到声明式切面选择.看以下: <aop:config> <!-- AOP定义開始 --> <aop:pointcut/> ...
- SpringBoot中使用切面的每次传的参数,进行解析,验签,并返回解码后的参数
目的,在每次请求的时候,对每次传的参数,进行解析,验签,并返回解码后的参数, 以json传递: 例子背景: IOT平台提供对外可访问的接口, 需要对所有参数的传递做到 不泄露.认证的目的:所以需要在每 ...
- winform中使用TextBox滚动显示日志信息
代码如下: private void ShowInfo(string msg) { this.BeginInvoke((Action)(() => { textBox1.AppendText(s ...
- Spring AOP 切面实现操作日志
创建接口注解日志类 package com.fh.service.logAop; /** * Created by caozengling on 2018/7/21. */ import java.l ...
- springboot使用@Aspect实现AOP记录日志讲解
AOPAOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.在日常开发当中经常用来记录日志,方法跟踪 ...
- redis基本操作和在springboot中的使用
本文介绍redis的使用 redis启动步骤 说明 redis自增自减相关操作 redis string set操作 get操作 其他操作 redis hash set操作 get操作 其他操作 re ...
- SpringBoot中的日志使用:
SpringBoot中的日志使用(一) 一:日志简介: 常用的日志接口 commons-logging/slf4j 日志框架:log4j/logback/log4j2 日志接口屏蔽了日志框架的底层实现 ...
- 在SpringBoot中使用logback优化异常堆栈的输出
一.背景 在我们在编写程序的过程中,无法保证自己的代码不抛出异常.当我们抛出异常的时候,通常会将整个异常堆栈的信息使用日志记录下来.通常一整个异常堆栈的信息是比较多的,而且存在一些没用的信息.那么我们 ...
随机推荐
- Docker 01 概述
参考源 https://www.bilibili.com/video/BV1og4y1q7M4?spm_id_from=333.999.0.0 https://www.bilibili.com/vid ...
- 通过route , tracert , traceroute 查看本地路由配置及访问ip或域名时经过的路由信息
转载请注明出处: 1.路由器和交换机的区别和过程 在windows 系统或linux 系统访问 外网ip 或域名时,都会通过层层的路由器,然后将请求转发到最终的目标服务器:因为互联网通过路由器实现公网 ...
- MongoDB,入门看这一篇足矣!
一.介绍 在介绍 MongoDB 之前,我先介绍一下业务开发的时候遇到的痛点,以便大家对它有一个更加清晰的认识! 最近在用数据库存储数据的时候发现这么一个坑,例如从消息队列中监听消息的时候,原来的做法 ...
- React报错之Rendered more hooks than during the previous render
正文从这开始~ 总览 当我们有条件地调用一个钩子或在所有钩子运行之前提前返回时,会产生"Rendered more hooks than during the previous render ...
- 如何使用Postman调试HMS Core推送接口?
HMS Core推送服务支持开发者使用HTTPS协议接入Push服务端.Postman是一款接口测试工具,它可以模拟用户发起的各类HTTP请求,将请求数据发送至服务端,获取对应的响应结果.Postma ...
- Kingbase_FDW 使用介绍
与postgresql_fdw功能类似,KINGBASE_FDW 是一种外部访问接口,它可以被用来访问存储在外部的数据.想要使用fdw访问数据需要先确保:网络通,数据库访问配置(pg_hba,con ...
- Linux做bond4
一.编辑bond网络配置 vim /etc/sysconfig/network-scripts/ifcfg-bond4 DEVICE=bond4 NAME=bond4 TYPE=Bond ONBOOT ...
- 使用filebeat接收rsyslog的日志
安装 下载好rpm包后直接安装 curl -L -O https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-7.7.0-x86_ ...
- 引擎之旅 前传:C++代码规范
自己以前写代码时,一个项目一个风格.单人开发的工作使得我并没有注意到代码规范性和可读性的问题.每当项目结束后,看到自己杂乱无章的代码,完全没有二次开发和重构的欲望. 写代码就应该像写诗一样优雅. by ...
- 超详细的格式化输出(format的基本玩法)
一.format的基本玩法 一.什么是format format是字符串内嵌(字符串内嵌:字符串中再嵌套字符串,加入双引号或单引号)的一个方法,用于格式化字符串.以大括号{}来标明被替换的字符串 fo ...