当前位置: 首页 > news >正文

注解的基本语法

定义注解

使用@interface关键字来定义注解:

public @interface AutoFill { }

元注解

元注解是用来注解其他注解的注解,Java提供了以下几种元注解:

@Target - 指定注解可以应用的目标元素类型

@Retention - 指定注解的保留策略

@Documented - 表示注解应该被包含在Javadoc中

@Inherited - 表示注解可以被继承

@Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface AutoFill { /** * 数据库操作类型:INSERT、UPDATE */ OperationType value(); }

示例代码展示了一个用于公共字段自动填充的自定义注解,@Target明确注解可在方法上使用,@Retention明确在程序运行时可见。

注解元素

注解中可以定义元素,这些元素可以有默认值:

public enum OperationType { /** * 更新操作 */ UPDATE, /** * 插入操作 */ INSERT }

示例自定义注解中的value方法则用来返回上示枚举类型数据,明确 使用该注解的方法 的作用,使用方式如下:

/** * 更新员工信息 * @param employee */ @AutoFill(OperationType.UPDATE) void updateById(Employee employee);

当注解只有一个方法且方法名为 value 时,使用时可以省略方法名,如果方法不叫 value,就必须明确指定方法名:

@Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface AutoFill { /** * 数据库操作类型:INSERT、UPDATE */ OperationType type(); }
/** * 更新员工信息 * @param employee */ @AutoFill(type = OperationType.UPDATE) void updateById(Employee employee);

自定义注解的使用

通过反射处理注解

我们可以使用反射机制在运行时读取和处理注解:

@Aspect @Component @Slf4j public class AutoFillAspect { /** * 公共字段自动填充切入点 */ @Pointcut("execution(* com.sky.mapper.*.*(..)) && @annotation(com.sky.annotation.AutoFill)") public void autoFillPointCut() {} /** * 公共字段自动填充 */ @Before("autoFillPointCut()") public void autoFill(JoinPoint joinPoint) throws Throwable { log.info("公共字段自动填充通知开始"); MethodSignature signature = (MethodSignature)joinPoint.getSignature(); AutoFill autoFill= signature.getMethod().getAnnotation(AutoFill.class); // 获取数据库操作类型 Enum operationType = autoFill.value(); // 从ThreadLocal中获取当前登录用户的id Long currentId = BaseContext.getCurrentId(); // 获取当前时间 LocalDateTime now = LocalDateTime.now(); // 从joinPoint中获取参数 Object[] args = joinPoint.getArgs(); if(args==null || args.length==0){ return; } // 从参数中获取实体对象 Object entity = args[0]; // 调用实体对象的方法,设置创建时间、创建人、更新时间、更新人 if(operationType==OperationType.INSERT){ try{ Method setCreateTime = entity.getClass().getDeclaredMethod("setCreateTime", LocalDateTime.class); Method setUpdateTime = entity.getClass().getDeclaredMethod("setUpdateTime", LocalDateTime.class); Method setCreateUser = entity.getClass().getDeclaredMethod("setCreateUser", Long.class); Method setUpdateUser = entity.getClass().getDeclaredMethod("setUpdateUser", Long.class); setCreateTime.invoke(entity, now); setUpdateTime.invoke(entity, now); setCreateUser.invoke(entity, currentId); setUpdateUser.invoke(entity, currentId); }catch (Exception e){ log.error("公共字段自动填充通知异常", e); } }else if(operationType==OperationType.UPDATE){ try{ Method setUpdateTime = entity.getClass().getDeclaredMethod("setUpdateTime", LocalDateTime.class); Method setUpdateUser = entity.getClass().getDeclaredMethod("setUpdateUser", Long.class); setUpdateTime.invoke(entity, now); setUpdateUser.invoke(entity, currentId); }catch (Exception e){ log.error("公共字段自动填充通知异常", e); } } }
http://www.jsqmd.com/news/1099728/

相关文章:

  • FreeRTOS学习笔记(二)
  • [对比学习LangChain和MAF-16]基于Checkpoint的持久化
  • C中单向链表之增删改查
  • 导入Seata-server所需SQL
  • 四川大学《微积分I-1》期末试卷及答案2016-2025学年PDF
  • OpenHarness源码研究-5-基础设施
  • 什么是配置中心?有哪些常见的配置中心?
  • 【车载 AOSP 16 蓝牙(bluedroid)服务】【qcom 平台双蓝牙】【13.耳机如何协商采样率:从 AVDTP 到 AAC 44100 的一条路】
  • 第六周学习报告
  • 我做了一个基于心理测评和场景记忆的 AI 伴侣产品 CandyAI
  • 爆品之后:新消费品牌如何用数字化穿越增长瓶颈?
  • YOLO目标检测论文实战指南:从模型改进到实验写作全流程
  • 2.1.8 this指针
  • 免费开源NoFences桌面分区管理工具:3步打造高效整洁Windows桌面
  • Day10 | SFT 训练实操——用 QLoRA 微调 Qwen3-8B
  • BetterJoy完整指南:让Switch手柄在PC游戏上完美运行
  • 智谱大模型LLM一面,人麻了!!!
  • 【JAVA毕设源码分享】基于springboot的小区公共收益管理系统 的设计与实现(程序+文档+代码讲解+一条龙定制)
  • 光电经纬仪测量中的坐标系体系及其应用
  • CPT Markets:把外汇用户支持体系做到位——维度复盘与提示整理
  • 抖音内容批量采集与智能管理工具:从零到精通的完整指南
  • OpenAI / Claude API 报错 401、403、429 怎么解决?一文讲清 API Key 失效排查思路
  • 量子虚时演化算法原理与sine-Gordon模型模拟实践
  • FreeCAD源码分析: Property View
  • 我一个人 11 天交付了两个模块——不是会分身,是让两个 AI 打了配合
  • 1115.交替打印FooBar
  • 【课程设计/毕业设计】基于 SpringBoot 的农业设备销售订单管理系统的设计与实现 基于 SpringBoot 的智慧农机综合服务管理系统【附源码、数据库、万字文档】
  • 修改很简单,但网上讲这点的文档不多,因此多记一笔。另外基于out_ptr会临时转移所有权这点来看,共享所有权模型的std::shared_ptr其实并不适合使用out_ptr,虽然标准没有禁止甚至还要
  • playwright-拖拽验证码
  • LeWorldModel:基于JEPA的轻量化世界模型实践指南