Java 中的注解 Annotation;元注解;注解解析

淩亂°似流年 2022-12-20 11:27 294阅读 0赞

Java 中的注解

Java 注解(Annotation) 是JDK1.5引入的。同时也定义一套注解,共有7个。

  • 注解的作用:它不是程序本身,却可以对程序作出解释;可以被其他程序读取并解析。
  • 注解的格式:
    单个参数:@Retention(RetentionPolicy.RUNTIME) @Retention(value=RetentionPolicy.RUNTIME)
    多个参数:@Target(value={CONSTRUCTOR, FIELD, LOCAL_VARIABLE, METHOD, PACKAGE, PARAMETER, TYPE})

代码中的注解

  • @Override :重写标记,在子类继承父类后,标记在重写后过后的子类方法上,如果发现父类,或者是引用的接口中没有该方法时,编译报错。
  • @Deprecated:标记过时的代码,不推荐使用的方法。
  • @SuppressWarnings:忽略编译器警告

元注解

元注解就是用来标记注解的注解。

  • @Retention:标记如何存储,三个参数
    1:RetentionPolicy.SOURCE 注释只保留在源文件中,当Java文件编译成class文件时,被标记注解会被遗弃。
    2:RetentionPolicy.CLASS 注解被保留在class文件,但jvm加载class文件时被遗弃,默认的生命周期。
    3:RetentionPolicy.RUNTIME 注解不仅被保留在class文件中,jvm加载class文件之后,仍然存在。

可以查看一下java源码中的英文注释

  1. public enum RetentionPolicy {
  2. /** * Annotations are to be discarded by the compiler. */
  3. SOURCE,
  4. /** * 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. */
  5. CLASS,
  6. /** * 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 */
  7. RUNTIME
  8. }
  • @Documented :标记这些注解是否包含在JavaDoc中。

    /* Indicates that annotations with a type are to be documented by javadoc and similar tools by default. This type should be used to annotate the declarations of types whose annotations affect the use of annotated elements by their clients. If a type declaration is annotated with Documented, its annotations become part of the public API of the annotated elements. @author Joshua Bloch @since 1.5 */
    @Documented
    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.ANNOTATION_TYPE)
    public @interface Documented {
    }

  • @Target :该注解说明了被标记注解的修饰范围。package、type(类、接口、枚举、Annotation类型)、类型成员(方法、构造方法、成员变量、枚举值)、方法参数和本地变量等。

    public enum ElementType {

    1. /** Class, interface (including annotation type), or enum declaration */
    2. TYPE,
    3. /** Field declaration (includes enum constants) */
    4. FIELD,
    5. /** Method declaration */
    6. METHOD,
    7. /** Formal parameter declaration */
    8. PARAMETER,
    9. /** Constructor declaration */
    10. CONSTRUCTOR,
    11. /** Local variable declaration */
    12. LOCAL_VARIABLE,
    13. /** Annotation type declaration */
    14. ANNOTATION_TYPE,
    15. /** Package declaration */
    16. PACKAGE,
    17. /** * Type parameter declaration * * @since 1.8 */
    18. TYPE_PARAMETER,
    19. /** * Use of a type * * @since 1.8 */
    20. TYPE_USE

    }

  • @Inherited :标记这个注解是继承与那个注解类的。

JDK1.7中的注解

  • @SafeVarargs :在声明可变参数的构造方法或方法时,java编译器会报unchecked警告,使用该注解忽略这个警告。
  • @FunctionalInterface :表示一个函数式接口
  • @Repeatable :标识某个注解可以同一个声明多次

自定义注解

一定一个带一个参数的注解

  1. /** * 自定义注解 单个参数 * @author lingyiwin */
  2. @Retention(value = RetentionPolicy.SOURCE)
  3. @Target(value = { ElementType.TYPE,ElementType.METHOD})
  4. public @interface MyAnnotationMethod {
  5. String value();
  6. }

定义一个多个参数的注解

参数可以设置默认值。

  1. /** * 自定义注解 多个参数 * @author lingyiwin */
  2. @Retention(value = RetentionPolicy.RUNTIME)
  3. @Target(value = ElementType.TYPE)
  4. public @interface MyAnnotationMorAargument {
  5. int id() default -1;
  6. int age() default 18;
  7. String stuName() default "";
  8. String[] school() default { "清华","北大"};
  9. }

注解的解析

定义完注解,代码中也使用了该注解,如果没有解析的程序,该注解是没有作用的。

注意:如果自定义注解中 @Retention(value = RetentionPolicy.RUNTIME) 不是RUNTIME 解析时获取不到的。

  1. /** * 定义注解 MyAnnotationTable * 解析时,获取value中的表名 * @author lingyiwin */
  2. @Retention(value = RetentionPolicy.RUNTIME)
  3. @Target(value = { ElementType.TYPE,ElementType.METHOD})
  4. public @interface MyAnnotationTable {
  5. String value();
  6. }
  7. /** * 定义注解MyAnnotationField * 标记Field属性 对应数据库中 列名、类型、长度 * @author lingyiwin */
  8. @Retention(value = RetentionPolicy.RUNTIME)
  9. @Target(value = ElementType.FIELD)
  10. public @interface MyAnnotationField {
  11. String columnName();
  12. String type();
  13. int length();
  14. }
  15. /** * @author lingyiwin * 使用反射解析定义的注解信息,通过获取的信息做一些操作 */
  16. public class ResolveAnnotation {
  17. public static void main(String[] args) {
  18. try {
  19. Class clazz = Class.forName("com.lingyiwin.annotation.Person");
  20. Annotation[] annotations = clazz.getAnnotations();
  21. for (Annotation an :annotations) {
  22. System.out.println(an);
  23. }
  24. MyAnnotationTable myAnnotationTable = (MyAnnotationTable) clazz.getAnnotation(MyAnnotationTable.class);
  25. String value = myAnnotationTable.value();
  26. System.out.println(value);
  27. Field[] fields = clazz.getDeclaredFields();
  28. for (Field field :fields) {
  29. MyAnnotationField annotationField = field.getAnnotation(MyAnnotationField.class);
  30. System.out.println(annotationField.columnName()+":"+annotationField.type()+":"+annotationField.length());
  31. }
  32. } catch (Exception e) {
  33. e.printStackTrace();
  34. }
  35. }
  36. }

查阅资料,总结,写笔记。如果能帮助到你,希望点个收藏和关注。谢谢。

发表评论

表情:
评论列表 (有 0 条评论,294人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Java注解(Annotation)

    一、什么是注解        注解(Annotation)很重要,未来的开发模式都是基于注解的,JPA是基于注解的,Spring2.5以上都是基于注解的,Hibernat

    相关 java 注解Annotation

    注解相当于一种标记,在程序中加了注解就等于为程序打上了某种标记,没加,则等于没有某种标记。 以后,javac编译器,开发工具和其他程序可以用反射来了解你的类及各种元素上有无何

    相关 Java——注解Annotation

    1. 简介 官方解释:Java 注解用于为 Java 代码提供元数据。作为元数据,注解不直接影响你的代码执行,但也有一些类型的注解实际上可以用于这一目的。 注解的定义: