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

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

如果都按照上述的操作方式来处理这些公共字段, 需要在每一个业务方法中进行操作, 编码相对冗余、繁琐,那能不能对于这些公共字段在某个地方统一处理,来简化开发呢?
答案是可以的,我们使用AOP切面编程加上自定义注解,实现功能增强,来完成公共字段自动填充功能。
我们在进行更新或新增操作的时候往往需要创建一个新的实体类对象,这时候就需要对该对象的公共字段进行自动填充。以下为实现思路
1、 新建一个枚举类,定义数据库操作类型

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

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

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

5、以下为方法增强实现
- 通过连接点JoinPonit获取方法上的参数(约定第一个参数为实体类对象)和注解
- 获取并判断注解所执行的操作是插入(save)还是修改(update)
- 反射获取到的实体类对象 == 》得到公共字段的set方法
- 根据获取到的注解分别完成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();
            }
        }
        
    }



















