公共字段自动填充

AOP概述

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

什么时候需要用切面类?

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

AOP核心概念

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

切入点表达式-@annotation

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

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

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

}
  • 自定义切面类AutoFillAspect,统一拦截加入AutoFill注解的方法,通过反射为公共字段赋值
/**
 * 自定义切面,实现公共字段自动填充处理逻辑
 */
@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注解
/**
 * 根据id修改分类
 * @param category
 */
@AutoFill(value = OperationType.UPDATE)
void update(Category category);
  • 14
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值