公共字段自动填充

文章目录

AOP概述

AOP:Aspect Oriented Programming (面向切面编程、面向方法编程)

什么时候需要用切面类?

  • 对于一些方法,抽取出来同一类非核心业务,然后可以将提取出来的业务编写成一个切面类,切面类可以;例如加减乘除,加入日志功能,那么日志功能就是非核心业务。
    切面类有什么用?
  • 解决代码混乱问题,非核心业务和核心业务代码处于同一个方法中会影响代码的质量,甚至可能会影响到核心业务

AOP核心概念

  • 连接点:JoinPoint,可以被AOP控制的方法(暗指方法执行时的相关信息)
  • 通知:Advice,指哪些重复的逻辑,也就是共性功能
  • 切入点:PointCut,匹配连接点的条件,通知仅会在切入点方法执行时被应用

切入点表达式-@annotation

@annotation切入点表达式,用于匹配标识有特定注解的方法

使用AOP实现公共字段自动填充

  • 自定义注解AutoFill,用于标识需要进行公共字段填充的方法
java 复制代码
/**
 * 自定义注解,用于标识某个方法需要进行功能字段自动填充处理
 */
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface AutoFill {
    //数据库操作类型
    OperationType value();

}
  • 自定义切面类AutoFillAspect,统一拦截加入AutoFill注解的方法,通过反射为公共字段赋值
java 复制代码
/**
 * 自定义切面,实现公共字段自动填充处理逻辑
 */
@Slf4j
@Aspect
@Component
public class AutoFillAspect {
    /**
   * 切入点
   */
    @Pointcut("execution(* com.sky.mapper.*.*(..))&&@annotation(com.sky.annotation.AutoFill)")
    public void autoFillPointCut(){}

    /**
     * 前置通知,为公共字段赋值
     */
    @Before("autoFillPointCut()")
    public void autoFill(JoinPoint joinPoint){
        log.info("开始进行公共字段的填充");
        //获取当前被拦截的方法上的数据库操作类型
          MethodSignature signature = (MethodSignature) joinPoint.getSignature();//方法签名对象(signature是接口需要向下转型为子对象)
          AutoFill autoFill= signature.getMethod().getAnnotation(AutoFill.class);//获得方法上的注释对象
        OperationType operationType=autoFill.value();//获得数据库操作类型
        //获取到当前被拦截的方法的参数--实体对象
        Object[]args=joinPoint.getArgs();
        if(args==null||args.length==0)return;;
        Object entity= args[0];
        //准备赋值的数据
        LocalDateTime now= LocalDateTime.now();
        Long currentId=BaseContext.getCurrentId();

        if(operationType==OperationType.INSERT){
            try {
              Method setCreateTime=  entity.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_TIME,LocalDateTime.class);
              Method setCreateUser= entity.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_USER,Long.class);
                Method setUpdateTime= entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME,LocalDateTime.class);
                Method setUpdateUser= entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER,Long.class);
                //通过反射赋值
                setCreateTime.invoke(entity,now);
                setCreateUser.invoke(entity,currentId);
                setUpdateTime.invoke(entity,now);
                setUpdateUser.invoke(entity,currentId);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }else if(operationType==OperationType.UPDATE){
            try {
                Method setUpdateTime= entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME,LocalDateTime.class);
                Method setUpdateUser= entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER,Long.class);
                //通过反射赋值
                setUpdateTime.invoke(entity,now);
                setUpdateUser.invoke(entity,currentId);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        //根据当前不同的操作类型,为相应的属性通过反射赋值




    }


}
  • 在Mapper的方法上加入AutoFill注解
java 复制代码
/**
 * 根据id修改分类
 * @param category
 */
@AutoFill(value = OperationType.UPDATE)
void update(Category category);
相关推荐
rannn_1117 小时前
【苍穹外卖|Day4】套餐页面开发(新增套餐、分页查询、删除套餐、修改套餐、起售停售)
java·spring boot·后端·学习
qq_12498707537 小时前
基于JavaWeb的大学生房屋租赁系统(源码+论文+部署+安装)
java·数据库·人工智能·spring boot·计算机视觉·毕业设计·计算机毕业设计
短剑重铸之日7 小时前
《设计模式》第十一篇:总结
java·后端·设计模式·总结
若鱼19197 小时前
SpringBoot4.0新特性-Observability让生产环境更易于观测
java·spring
觉醒大王7 小时前
强女思维:着急,是贪欲外显的相。
java·论文阅读·笔记·深度学习·学习·自然语言处理·学习方法
努力学编程呀(๑•ี_เ•ี๑)7 小时前
【在 IntelliJ IDEA 中切换项目 JDK 版本】
java·开发语言·intellij-idea
码农小卡拉8 小时前
深入解析Spring Boot文件加载顺序与加载方式
java·数据库·spring boot
向上的车轮8 小时前
为什么.NET(C#)转 Java 开发时常常在“吐槽”Java:checked exception
java·c#·.net
Dragon Wu8 小时前
Spring Security Oauth2.1 授权码模式实现前后端分离的方案
java·spring boot·后端·spring cloud·springboot·springcloud
跳动的梦想家h8 小时前
环境配置 + AI 提效双管齐下
java·vue.js·spring