元注解

元注解 : 注解在注解上的注解#

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Component {

String value() default "";

}

@Target (靶)#

  • 决定一个注解的靶,即这个注解可以注解到哪些地方。

  • @Target注解自己的@Target(ElementType.ANNOTATION_TYPE),表示注解在注解上

通过ElementType枚举选择

@Retention (保留)#

  • 决定注解是否保留
  • 由RetentionPolicy枚举选择在什么时候保留
  • SOURCE , CLASS , RUNTIME
  • 如果不加这个注解,默认会认为被修饰的注解是@Retention(RetentionPolicy.CLASS)
public enum RetentionPolicy {
/**
* Annotations are to be discarded by the compiler.
*/
SOURCE, //在编译期间就被丢弃,即只在源码期间保留

/**
* Annotations are to be recorded in the class file by the compiler
* but need not be retained by the VM at run time. This is the default
* behavior.
*/
CLASS, //在class文件时保留, 在加载到VM时会被丢弃,即只在class文件期间保留。 默认策略

/**
* Annotations are to be recorded in the class file by the compiler and
* retained by the VM at run time, so they may be read reflectively.
*
* @see java.lang.reflect.AnnotatedElement
*/
RUNTIME //会被加载进到VM, VM中通过反射是可以获取到此注解
}

@Inherited (遗传)#

  • 一个类使用了用@Inherited修饰的注解,那么子类也会继承这个注解
  • 继承的含义: 子类使用isAnnotationPresent, 或者getAnnotation等反射方法是可以获取到此注解
注意 :
  • 接口用上被@Inherited修饰的注解,其实现类不会继承这个注解
  • 父类的方法用了@Inherited修饰的注解,子类也不会继承这个注解
  • 如果一个注解被@Retention修饰且是RetentionPolicy.RUNTIME, @Inherited注解才起作用

@Documented#

  • 被加载到JavaDoc

参考 :
https://blog.csdn.net/qq_43390895/article/details/100175330

posted @   FynnWang  阅读(48)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· Vue3状态管理终极指南:Pinia保姆级教程
more_horiz
keyboard_arrow_up dark_mode palette
选择主题
menu
点击右上角即可分享
微信分享提示