公共字段自动填充

文章目录

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);
相关推荐
En^_^Joy几秒前
Kubernetes流量负载:Service与Ingress全解析(K8s)
java·容器·kubernetes
w_t_y_y2 分钟前
工具Cursor(五)Rules&Skill
java·开发语言
h7ml11 分钟前
企业微信“群机器人”消息合并转发:用Disruptor做环形队列的Java实例
java·机器人·企业微信
颇有几分姿色14 分钟前
Spring Boot Actuator未授权访问漏洞 【原理扫描】修复
java·spring boot·后端
1candobetter15 分钟前
JAVA后端开发——Spring Boot 多环境配置与实践
java·开发语言·spring boot
沛沛老爹19 分钟前
Web开发者实战:多模态Agent技能开发——语音交互与合成技能集成指南
java·开发语言·前端·人工智能·交互·skills
Wpa.wk20 分钟前
Docke-compose 搭建 testLink环境
java·经验分享·测试工具·容器·testlink
小北方城市网22 分钟前
Spring Boot Actuator+Prometheus+Grafana 生产级监控体系搭建
java·spring boot·python·rabbitmq·java-rabbitmq·grafana·prometheus
shehuiyuelaiyuehao23 分钟前
图书管理系统
java·服务器·前端
IUGEI2 小时前
从原理到落地:DAG在大数据SLA中的应用
java·大数据·数据结构·后端·算法