首页 > 其他分享 >SpringBoot自定义注解实现操作日志记录

SpringBoot自定义注解实现操作日志记录

时间:2024-01-16 10:32:38浏览次数:33  
标签:SpringBoot 自定义 private afLog logEntity 日志 public String

1、增加依赖

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-aop</artifactId>
                <version>${spring-version}</version>
            </dependency>

2、自定义注解类

@Target({ElementType.PARAMETER, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface AfLog {
    //模块名
    String module() default "";

    //具体操作
    String operation() default "";
}

3、定义切面类

@Aspect
@Component
public class LogAspect {
    @Resource
    private LogService logService;
    @Pointcut("@annotation(com.gebiafu.log.AfLog)")
    public void logPointcut() {}


    @After("logPointcut() && @annotation(afLog)")
    private void handleLog(JoinPoint joinPoint,AfLog afLog) {
		//日志实体,用来保存到数据库
        LogEntity logEntity = new LogEntity();
		//获取模块信息
        logEntity.setModule(afLog.module());
		//获取操作信息
        logEntity.setOperation(afLog.operation());
		//记录时间
        logEntity.setOperateTime(new Date());
        logService.save(logEntity);

    }
}

4、日志实体

@Data
@TableName("t_af_log")
public class LogEntity implements Serializable {

    @TableId(type = IdType.ASSIGN_ID)
    private String id;

    private String module;

    private String operation;

    @TableField("operate_time")
    private Date operateTime;
}

5、使用方式

image.png

6、日志入库

image.png

7、如有其他业务需求,可自行扩展注解类和切面

标签:SpringBoot,自定义,private,afLog,logEntity,日志,public,String
From: https://blog.51cto.com/u_16479709/9267734

相关文章

  • 自定义echarts绘制直方图,XY轴互调Demo
    1constcolorList=[2'#4f81bd',3'#c0504d',4'#9bbb59',5'#604a7b',6'#948a54',7'#e46c0b'8];9constdata=[10[10,16,3,'A'],11[16,18,15,&#......
  • JSON注解自定义格式解析
    在SpringBoot中,你可以通过自定义注解来格式化或转换属性值。以下是一个示例代码,演示如何实现这个过程:首先,定义一个注解@CustomFormat,用于标注需要格式化或转换的属性。该注解可以包含一个参数,用于指定格式化或转换的方式。importjava.lang.annotation.*;@Target(ElementType......
  • 为什么很多公司 SpringBoot 项目禁止使用 Tomcat
    为什么很多公司SpringBoot项目禁止使用Tomcat学习改变命运,技术铸就辉煌。大家好,我是銘,全栈开发程序员。前言在SpringBoot框架中,我们使用最多的是Tomcat,这是SpringBoot默认的容器技术,而且是内嵌式的Tomcat。同时,SpringBoot也支持Undertow容器,我们可以很方便的用......
  • SparkStreaming 自定义数据采集器
    本文的前提条件:SparkStreaminginJava参考地址:SparkStreamingCustomReceivers1.自定义数据采集器packagecn.coreqi.receiver;importorg.apache.spark.storage.StorageLevel;importorg.apache.spark.streaming.receiver.Receiver;importjava.util.Random;/**......
  • springboot拦截器@resource注解注入为null解决方案 拦截适配配置
    springboot拦截器@resource注解注入为null解决方案 拦截适配配置为什么@resource注入为nullinteceptor在springcontext之前加载,注入必然是null解决方案加入注解@Bean,注意需要使用@Configuration,而不是@Component解决在Spring添加拦截器之前先自己创建一下这个SpringBean,这样......
  • springboot项目配置多数据源
    springboot项目配置多数据源//关键:mybatis文件的目录需要区分开来sqlSessionFactoryBean.setMapperLocations(newPathMatchingResourcePatternResolver().getResources("classpath:mybatis.myProjectOne/*.xml"));#从数据库配置,数据库的配置以spring.datasource.myPr......
  • Idea SpringBoot 子模块 加载不到该子模块根目录config下面的配置文件
    IdeaSpringBoot子模块加载不到该子模块根目录config下面的配置文件importorg.mybatis.spring.annotation.MapperScan;importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;importorg.springframew......
  • springboot + mybatis plus 全局添加查询字段反引号
    配置文件添加: column-format:"`%s`"mybatis-plus:#启动时是否检查MyBatisXML文件是否存在check-config-location:true#MyBatis原生配置configuration:#字段名称下划线转驼峰命名map-underscore-to-camel-case:trueglobal-config:db-co......
  • 12、nginx日志配置
    1.nginx日志指令log_format:用于设置日志格式access_log:用于指定日志文件存放路径、格式、缓存大小可设置在http、server块中2.log_format2.1log_format语法log_formatnameformat[format...]name表示定义的格式名称format表示定义的格式样式。log_forma......
  • SpringBoot中整合ElasticSearch实现增删改查等操作
    场景SpringBoot中整合ElasticSearch快速入门以及踩坑记录:https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/135599698在上面进行集成的基础上,实现对ES数据的增删改查等操作。注:博客:https://blog.csdn.net/badao_liumang_qizhi实现1、ElastciSearch的对象映射h......