news 2026/7/3 5:40:19

注解的基本语法

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
注解的基本语法

定义注解

使用@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); } } }
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/7/3 11:13:36

告别泰拉瑞亚原版限制:tModLoader模组开发实战手册

告别泰拉瑞亚原版限制:tModLoader模组开发实战手册 【免费下载链接】tModLoader A mod to make and play Terraria mods. Supports Terraria 1.4 (and earlier) installations 项目地址: https://gitcode.com/gh_mirrors/tm/tModLoader 还在为泰拉瑞亚原版内…

作者头像 李华
网站建设 2026/7/2 7:07:59

define和typedef的区别详解

前言在C/C编程中,#define和typedef都常用于为类型或值起一个别名,但它们的工作原理、适用场景和行为差异却天差地别。很多初学者甚至有一定经验的开发者都容易混淆二者,写出“看似正确却暗藏隐患”的代码。本文将深入剖析#define与typedef的本…

作者头像 李华
网站建设 2026/7/1 2:17:25

Windows本地实时语音转文字:TMSpeech让你的电脑变身智能会议助手

Windows本地实时语音转文字:TMSpeech让你的电脑变身智能会议助手 【免费下载链接】TMSpeech 腾讯会议摸鱼工具 项目地址: https://gitcode.com/gh_mirrors/tm/TMSpeech 还在为会议记录手忙脚乱?在线课程听得一知半解?今天我要向你介绍…

作者头像 李华
网站建设 2026/7/3 3:15:13

Excel5

笔记:CtrlC:复制; CtrlX:剪切; CtrlV:粘贴。按住Ctrl键移动单元格,等同于复制粘贴。选择多列,双击某两列列标中间的间隙,等同于以最合适的列宽显示文字内容。&#x…

作者头像 李华
网站建设 2026/7/4 1:20:51

破釜沉舟!准大三民办二本生的逆袭战书:C语言起步,死磕嵌入式

a. 自我介绍:既然醒了,就拼命跑 大家好,我是一名准大三的学生,就读于一所民办二本院校。说实话,前两年我过得浑浑噩噩,对电脑的认知基本停留在打游戏和写Word文档上。看着马上要面临秋招和毕业,…

作者头像 李华