自定义注解

元注解

目前,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();
        // [@com.ParentAnnotation(key=key, value=value)]
        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) {
            // @com.TestAnnotation(key=key1, value=value1)
            // @com.TestAnnotation(key=key2, value=value2)
            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) }
    }

posted @   Acaak  阅读(43)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示