元注解
目前,Java中存在五个元注解,分别是 @Target、@Retention、@Documented、@Inherited、@Repeatable
一、元注解的定义
所谓元注解,其主要作用就是负责注解其他注解,为其他注解提供了相关的解释说明
二、元注解的使用
@Target
用于指定注解的使用范围
ElementType.TYPE:类、接口、注解、枚举
ElementType.FIELD:字段、枚举常量
ElementType.METHOD:方法
ElementType.PARAMETER:形式参数
ElementType.CONSTRUCTOR:构造方法
ElementType.LOCAL_VARIABLE:局部变量
ElementType.ANNOTATION_TYPE:注解
ElementType.PACKAGE:包
ElementType.TYPE_PARAMETER:类型参数
ElementType.TYPE_USE:类型使用
@Retention
用于指定注解的保留策略
RetentionPolicy.SOURCE:注解只保留在源码中,在编译时会被编译器丢弃
RetentionPolicy.CLASS:(默认的保留策略) 注解会被保留在Class文件中,但不会被加载到虚拟机中,运行时无法获得
RetentionPolicy.RUNTIME:注解会被保留在Class文件中,且会被加载到虚拟机中,可以在运行时获得
@Documented
用于将注解包含在javadoc中
默认情况下,javadoc是不包括注解的,但如果使用了@Documented注解,则相关注解类型信息会被包含在生成的文档中
@Inherited
用于指明父类注解会被子类继承得到
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface ParentAnnotation {
String key();
String value();
}
@ParentAnnotation(key = "key", value = "value")
public class Parent {
}
public class Son extends Parent {
public static void main(String[] args) {
Annotation[] annotations = Son.class.getAnnotations();
System.out.println(Arrays.toString(annotations));
}
}
@Repeatable
用于声明标记的注解为可重复类型注解,可以在同一个地方多次使用
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Repeatable(RepeatableAnnotation.class)
public @interface TestAnnotation {
String key();
String value();
}
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface RepeatableAnnotation {
TestAnnotation[] value();
}
@TestAnnotation(key = "key1", value = "value1")
@TestAnnotation(key = "key2", value = "value2")
public class Test {
public static void main(String[] args) {
RepeatableAnnotation annotation = Test.class.getAnnotation(RepeatableAnnotation.class);
TestAnnotation[] testAnnotations = annotation.value();
for (TestAnnotation testAnnotation : testAnnotations) {
System.out.println(testAnnotation);
}
}
}
自定义注解
一、Demo

二、项目中使用(kotlin)
@Target(AnnotationTarget.CLASS)
@Retention
@Component
annotation class FormPropertiesProvider(val module: Module)
interface FormPropertiesLoader : PropertiesLoader
private val formPropertiesLoaders: List<FormPropertiesLoader>,
@GetMapping("/basics/forms/{formId}")
@Operation(summary = "查询(表单)基础数据(自动填充);formId为表单id")
@Parameters(Parameter(name = "formId", description = "表单id"))
fun findFormContext(@PathVariable("formId") formId: String): List<KeyValue> {
val localFormId = formLocator.locate(formId) ?: return emptyList()
val loader = formPropertiesLoaders.firstOrNull {
val loadable = it::class.findAnnotation<FormPropertiesProvider>() ?:
return@firstOrNull false
loadable.module == localFormId.module.toEnum<Module>()
} ?: return emptyList()
return loader.load(localFormId.id).map { KeyValue(it.key, it.value) }
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了