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

news/发布时间2024/5/18 6:08:39

应用场景

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

如何解决呢

思路:利用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 {arg.getClass().getDeclaredMethod("setUpdateTime",updateTime.getClass());arg.getClass().getDeclaredMethod("setCreateTime",createTime.getClass());arg.getClass().getDeclaredMethod("setCreateUser",createUser.getClass());arg.getClass().getDeclaredMethod("setUpdateUser",updateUser.getClass());} 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());arg.getClass().getDeclaredMethod("setUpdateTime",updateTime.getClass());} catch (Exception e) {throw new RuntimeException(e);}}}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.ulsteruni.cn/article/81773274.html

如若内容造成侵权/违法违规/事实不符,请联系编程大学网进行投诉反馈email:xxxxxxxx@qq.com,一经查实,立即删除!

相关文章

关于diffusion model一些统计和数学的基础知识

likelihood-based models,通过(近似)最大似然直接学习分布的probability density(或mass)函数。典型的基于似然的模型包括自回归模型、归一化流模型、基于能量的模型(EBMs)和变分自编码器(VAEs)。 概率质量函数(Probability Mass Function,PMF):概率质量函数用于描述离散随…

AutoCAD C# 两不平行直线倒圆弧算法

参考的博客:https://www.cnblogs.com/JJBox/p/14300098.html 下面是计算示例主要计算代码:var peo = new PromptEntityOptions("选择直线1"){AllowNone = false,AllowObjectOnLockedLayer = false};peo.SetRejectMessage("请选择直线Line");peo.AddAllow…

2. 基础配置

1. 配置文件格式 1.1 配置文件自动提示功能消失解决方案 ​​ 1.2 SpringBoot配置文件加载顺序(了解) application.properties > application.yml > application.yaml 1.3 注意事项 SpringBoot核心配置文件名为application SpringBoot内置属性过多,且所有属性集中…

搭建自己的博客

基于github和Hexo 搭建自己的博客 【摘要】该教程基于个人的虚拟机和个人的GitHub,过程会详细注明对应的安装包的版本。 1、搭建hexo环境 环境配置 本地虚拟机:ubuntu 20.4(也可以基于对应的服务器) Hexo搭建步骤 1. 安装nodejs和npm 由于Ubuntu20通过apt安装nodejs默认只能…

06.数组

1.数组概述 数组是相同类型数据的有序集合; 数组描述的是相同类型的若干各数据,按照一定的先后次序排列组合而成; 其中,每一个数据称作一个数组元素,每个数组元素可以通过一个下标来访问它们。 2.数组声明创建 首先必须声明数组变量,才能在程序中使用数组: dataType[] a…

.Net 8.0 下的新RPC,IceRPC之如何创建连接connection

作者引言很高兴啊,我们来到了IceRPC之如何创建连接connection,基础引导,让自已不在迷茫,快乐的畅游世界。如何创建连接connection学习如何使用IceRPC,创建和接受连接。连接有什么用途? 连接在 IceRPC 中发挥着核心作用: 通过连接向服务端发送请求,然后通过同一连接收到响应…