1.自定义注释

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface Log {LogType type() default LogType.SPACE;
}

2.type值的枚举

public enum LogType {SPACE(""),INSERT("增加"),DELETE("删除"),UPDATE("修改"),QUERY("查询");private String description;private LogType( String string) {description=string;}public String GetDescription(){return description;}
}

3.注释的使用

public abstract class AbstractService<T, M> implements IService<T, M>
{@Override@Log(type = LogType.INSERT)public int add(T t){return getMapper().insert(t);}}

4.自定义切面(注意@Aspect 和@Component注解)

@Aspect
@Component
public class OperateLogAspect {@Autowiredprivate IOperationLogService operationLogService;/*** 后置增强*/@After("@annotation(cn.com.reformer.annotation.Log) && @annotation(log)")private void after(JoinPoint joinPoint, Log log) {//读取中的用户HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();Admin user = (Admin) request.getAttribute(Constants.CURRENT_ADMIN);//未登录或登录操作if (null == user) {return;}//没有参数if (joinPoint.getArgs() == null) {return;}OperationLog operationLog = new OperationLog();operationLog.setUserId(user.getId());operationLog.setIpAddr(getIP(request));operationLog.setOpearteTime(new Date());Object[] os = joinPoint.getArgs();//获取类名String className = joinPoint.getTarget().getClass().getSimpleName();//获取方法名String methodName = joinPoint.getSignature().getName();String param = className + "." + methodName + ":";for (int i = 0; i < os.length; i++) {param += "参数[" + i + "]:" + os[i].toString();}operationLog.setOperation(log.type().GetDescription());operationLog.setOperateContent(param);//写入数据库operationLogService.insertSelective(operationLog);return;}//获取请求IPpublic String getIP(HttpServletRequest request) {String ip = request.getHeader("x-forwarded-for");if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {ip = request.getHeader("Proxy-Client-IP");}if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {ip = request.getHeader("WL-Proxy-Client-IP");}if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {ip = request.getRemoteAddr();}return ip;}
}
</pre><p></p><pre>

5.配置Spring-mvc.xml

proxy-target-class="true"为开启CGLIB动态代理

    <!--自定义切面所在包--><context:component-scan base-package="cn.com.reformer.service"/><!--识别切面,开启CGLIB动态代理--><aop:aspectj-autoproxy proxy-target-class="true"/>

6.此过程中遇到的麻烦

在数据库写操作日志时报read-only的错误,原因事务配置了read-only,如下:

    <!-- 事务管理 通知 --><tx:advice id="txAdvice" transaction-manager="transactionManager"><tx:attributes><!-- 对insert,update,delete 开头的方法进行事务管理,只要有异常就回滚 --><tx:method name="add*" propagation="REQUIRED" rollback-for="java.lang.Throwable"/><tx:method name="create*" propagation="REQUIRED" rollback-for="java.lang.Throwable"/><tx:method name="insert*" propagation="REQUIRED" rollback-for="java.lang.Throwable"/><tx:method name="update*" propagation="REQUIRED" rollback-for="java.lang.Throwable"/><tx:method name="delete*" propagation="REQUIRED" rollback-for="java.lang.Throwable"/><!-- select,count开头的方法,开启只读,提高数据库访问性能 --><tx:method name="select*" read-only="true"/><tx:method name="query*" read-only="true"/><tx:method name="count*" read-only="true"/><tx:method name="find*" read-only="true"/><tx:method name="get*" read-only="true"/><!-- 对其他方法 使用默认的事务管理 --><tx:method name="*"/></tx:attributes></tx:advice>

解决方法:

1.此处设置read-only的地方全部都是查询,可以不记录查询日志,及查询方法前不加自定义注解@Log

2.去掉事务的read-only属性

Spring AOP 自定义注解记录操作日志相关推荐

  1. JPOM - AOP+自定义注解实现操作日志记录

    文章目录 地址 版本 源码解析-AOP+自定义注解实现操作日志记录 地址 Gitee: https://gitee.com/dromara/Jpom 官网: https://jpom.io/ 一款简而 ...

  2. spring AOP自定义注解方式实现日志管理

    转:spring AOP自定义注解方式实现日志管理 今天继续实现AOP,到这里我个人认为是最灵活,可扩展的方式了,就拿日志管理来说,用Spring AOP 自定义注解形式实现日志管理.废话不多说,直接 ...

  3. spring aop 自定义注解配合swagger注解保存操作日志到mysql数据库含(源码)

    spring aop 自定义注解保存操作日志到mysql数据库 一.思路 二.自定义注解 三.编写操作日志 四.编写操作日志切面\增强 五.使用 六.`注意` 一.思路 利用spring aop 对方 ...

  4. 使用Spring AOP自定义注解方式实现用户操作日志记录

    1,开发环境 操作系统:Windows 7 JDK:1.8.0_161 Eclipse:Mars.2 Release (4.5.2) 2,自定义注解类UserLog @Target({ElementT ...

  5. Spring AOP自定义注解并获取注解的参数

    环境 springboot:1.5 Intellij IDEA:2021.1 序言 最近有个需求,要做方法层面的权限控制.以前在公司使用的是spring security,然后使用注解 如下: @Pr ...

  6. Java之——Spring AOP自定义注解实现日志管理

    转载请注明出处:https://blog.csdn.net/l1028386804/article/details/80295737 1.定义日志类SystemLog package io.mykit ...

  7. springboot自定义注解收集操作日志

    ​对于日志收集,是系统离不开的功能,那么如何实现简易的日志收集呢.市面上最常见的就是使用自定义注解.本文将介绍如何使用自定义注解收集日志. 1.添加自定义注解 @Target({ElementType ...

  8. 使用AOP与注解记录Java日志

    有些时候,我想要把每个运行过的方法接收到的参数.返回值和执行时间等信息记录(通过slf4j 和 log4j)下来.在AspectJ.jcabi-aspects和Java注解的帮助下我实现了这个想法. ...

  9. slf4j注解log报错_SpringBoot自定义日志注解,用于数据库记录操作日志,你用过吗?...

    大家好,我是程序员7歌! 今天我将为大家讲解如何通过自定义注解记录接口访问日志.一般的开发中,有两种方式可以记录日志信息,第一种:把接口日志信息保存到日志文件中,第二种:把接口操作日志保存到数据库中, ...

最新文章

  1. 采用Filter的方法解决Servlet的编码问题
  2. SAP智能机器人流程自动化解决方案
  3. GO语言基础条件、跳转、Array和Slice
  4. 在mac os10.13系统下 ,将py文件打包成可执行程序后, 里面的路径出现的问题
  5. 矩阵运算——平移,旋转,缩放
  6. 软件工程 案例分析作业
  7. Java动态加载类(对反射的基本理解)
  8. 7-Arco大讲堂(二)
  9. python 笔记 之 线程锁
  10. 管理感悟:当领导就要敢于,及时做出决策
  11. mysql数据库攻击与防御pdf_SQL注入攻击与防御 中文PDF清晰扫描版(38.6M)
  12. 无需U盘在Windows下安装Linux系统实现双系统(非子系统)
  13. 如果背单词,可能需要的参考辞典...
  14. Unity3d 真实的植物渲染
  15. 虚幻引擎4艺术大师 - 中文版免费分享
  16. 论述:企业如何做好网络营销推广
  17. 用计算机算3次根号0.00005,使用ORCA在TDDFT下计算旋轨耦合矩阵元和绘制旋轨耦合校正的UV-Vis光谱...
  18. Graphics2D 在一张图片上添加一个带有透明背景的图片或绘制透明图片
  19. 启动计算机时听到嘀嘀声,为何电脑开机时会有滴滴的声音?
  20. 第一部分 数理逻辑 第三章 命题逻辑的推理理论

热门文章

  1. 用友数据库表名参照表
  2. 网易云信携手神州信息,共同打造广东华兴银行线上视频平台
  3. 【python核心编程笔记+习题】-CH5-数字类型
  4. @Aspect aop切面获取请求接口类名、方法名、及参数
  5. 血条加载!百度地图上线复苏指数;Quora推出问答机器人;腾讯绝悟成功用于医疗诊断;使用chatGPT生成推文;GitHub AI项目精选 | ShowMeAI资讯日报
  6. 微盟股价快速飞升的背后:WOS系统将驱动长效增长
  7. mac时间机器文件服务器,使用时间机器备份文件 - 处理文件和文件夹 - macOS使用手册...
  8. mac 时间机器备份慢? 首次备份限速解除!
  9. 文件操作,函数练习及答案
  10. 度娘果然毫无节操,纯粹就是order by 广告费 desc