自定义注解+AOP完成公共字段填充

在开发中,我们的实体类经常会有几个公共的字段,如下图的创建时间,修改时间就为各个类共有的字段:

目前项目中处理这些字段都是在每一个业务方法中进行赋值操作,如下: 

如果都按照上述的操作方式来处理这些公共字段, 需要在每一个业务方法中进行操作, 编码相对冗余、繁琐,那能不能对于这些公共字段在某个地方统一处理,来简化开发呢?

答案是可以的,我们使用AOP切面编程加上自定义注解,实现功能增强,来完成公共字段自动填充功能。

我们在进行更新或新增操作的时候往往需要创建一个新的实体类对象,这时候就需要对该对象的公共字段进行自动填充。以下为实现思路 


 

1、 新建一个枚举类,定义数据库操作类型

 

2、自定义注解,后续为被该注解标记的方法执行自动填充

 

 

3、 在Mapper层需要增强的方法上打上注解,并标记其操作为更新或插入(两种操作对应增强方法不同)

4、新建一个切面类,定义切点和具体增强方法逻辑 

 

5、以下为方法增强实现 

  1. 通过连接点JoinPonit获取方法上的参数(约定第一个参数为实体类对象)和注解
  2. 获取并判断注解所执行的操作是插入(save)还是修改(update)
  3. 反射获取到的实体类对象 == 》得到公共字段的set方法
  4. 根据获取到的注解分别完成save操作和update操作的公共字段赋值 
    @Before("AutoFillPoint()")
    public void AutoFill(JoinPoint joinPoint) throws InvocationTargetException, IllegalAccessException {
        //获得参数
        Object[] args = joinPoint.getArgs();
        if(args == null || args.length == 0){
            return;
        }
        //约定第一个参数为传入的实体类对象
        Object entity = args[0];
        //TODO 获得方法签名--获得具体方法--获得该方法上的注解
        //获得方法签名
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        //获得具体方法
        Method method = signature.getMethod();
        //获得方法上的注解
        AutoFill annotation = method.getAnnotation(AutoFill.class);

        //TODO 设置需要填充的字段:当前时间、执行操作的用户
        LocalDateTime now = LocalDateTime.now();
        Long user = UserHolder.getUser();

        //TODO 判断注解是save操作还是update操作
        OperationType value = annotation.value();       
        if(value == OperationType.INSERT){
            try {
             //反射获得对应save操作所需要填充的字段,并完成赋值
                Method createTime = entity.getClass().getDeclaredMethod("setCreateTime",LocalDateTime.class);
                Method updateTime = entity.getClass().getDeclaredMethod("setUpdateTime",LocalDateTime.class);
                Method createUser = entity.getClass().getDeclaredMethod("setCreateUser",Long.class);
                Method updateUser = entity.getClass().getDeclaredMethod("setUpdateUser",Long.class);
                createTime.invoke(entity,now);
                updateTime.invoke(entity,now);
                createUser.invoke(entity,user);
                updateUser.invoke(entity,user);
            } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
                e.printStackTrace();
            }
        }
        //反射获得对应update操作所需要填充的字段,并完成赋值
        if(value == OperationType.UPDATE){
            try {
                Method updateTime = entity.getClass().getDeclaredMethod("setUpdateTime",Long.class);
                Method updateUser = entity.getClass().getDeclaredMethod("setUpdateUser",Long.class);
                updateTime.invoke(entity,now);
                updateUser.invoke(entity,user);
            } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
                e.printStackTrace();
            }
        }
        
    }

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

白日日白

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值