总结一下公共字段(aop加自定义注解加反射)

应用场景

在一些业务类的创建中,我们需要反复对不同的类的同一个属性进行赋值,那么问题就出现了 **代码冗余**

如何解决呢

思路:利用aop创造一个切面

如何创造一个切面呢

实质上就是扫描加设置增强位置 那么如何扫描到对应的赋值方法上呢 这里需要用到自定义注解了 自定义注解:
//这里的OperationType 是定义的一个枚举类型,value是里面的枚举常量,在我们标识对应的方法中可以使用不同的常量来执行不同的判断
//表明作用对象是方法
@Target(value = ElementType.METHOD)
//运行时实现的,aop必写
@Retention(RetentionPolicy.RUNTIME)
public @interface AutoFill {
    OperationType value();
}

注解完成

注解完成,开始操刀aop切面


@Aspect//切面需要加的注解
@Component
@Slf4j
public class AutoFillAspect {
//这里标识,此切面的范围,也就是需要被增强的类,为了准确定位,我们还需要确认注解也在上面,使用poincut是为了减少冗余代码
  @Pointcut("execution(* com.sky.mapper.*.*(..))&&@annotation(com.sky.annotation.AutoFill)")
    public void autoFillPointcut(){}
//before在执行前进行一个增强的操作,参数基本都是JoinPoint
    @Before("autoFillPointcut()")
    public void autoFill(JoinPoint joinPoint){
    log.info("首位增强");
//通过参数获取签名
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
//通过签名获取自定义注解的值
        AutoFill aution = signature.getMethod().getAnnotation(AutoFill.class);
        OperationType value = aution.value();
//通过参数获取对应的类
        Object[] args = joinPoint.getArgs();
        if(args.length==0||args== null){
            return;
        }
        Object arg = args[0];
//通过判断注解值后,getDeclaredMethod调用类里面的赋值方法
        if (value==OperationType.INSERT) {
            LocalDateTime updateTime = LocalDateTime.now();
            LocalDateTime createTime=LocalDateTime.now();
            Long createUser= BaseContext.getCurrentId();
            Long updateUser=BaseContext.getCurrentId();
            try {
                Method setUpdateTime=  arg.getClass().getDeclaredMethod("setUpdateTime",updateTime.getClass());
                Method setCreateTime=   arg.getClass().getDeclaredMethod("setCreateTime",createTime.getClass());
                Method setCreateUser=   arg.getClass().getDeclaredMethod("setCreateUser",createUser.getClass());
                Method setUpdateUser=   arg.getClass().getDeclaredMethod("setUpdateUser",updateUser.getClass());
//调取方法后,调用添加方法
                setUpdateUser.invoke(arg,updateUser);
                setCreateUser.invoke(arg,createUser);
                setCreateTime.invoke(arg,createTime);
                setUpdateTime.invoke(arg,updateTime);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }else if(value==OperationType.UPDATE){
            LocalDateTime updateTime = LocalDateTime.now();
            Long updateUser=BaseContext.getCurrentId();

            try {
                arg.getClass().getDeclaredMethod("setUpdateUser",updateUser.getClass()).invoke(arg,updateUser);
                arg.getClass().getDeclaredMethod("setUpdateTime",updateTime.getClass()).invoke(arg,updateTime);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    }
}
posted @   浮白呀  阅读(36)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示