java自定义注解Annotation

港控/mmm° 2022-04-11 11:57 419阅读 0赞

注解,简单的说就是给这个东西定义的一个标签,eg entity=一个人,那么注解可以是 土豪,穷人 等等。

下面来说说java常见的内置注解:

  @Override 当我们想重写一个方法时,在方法上加@Override,当我们方法的名字出错时,编译器就会报错。 定义如下:

    @Retention(RetentionPolicy.SOURCE )

    public @interface Override

  @Deprecated 用来表示某个类的属性或方法已经过时,不想别人再用时,在属性和方法上用@Deprecated修饰。 定义如下:

    @Retention(RetentionPolicy.SOURCE )

    public @interface Deprecated

  @SuppressWarnings 用来压制程序中出来的警告。 定义如下:

    @Retention(RetentionPolicy.SOURCE )

    public @interface SuppressWarnings

自定义注解:

  1. package com.test.testdemo.annotation;
  2. import java.lang.annotation.ElementType;
  3. import java.lang.annotation.Retention;
  4. import java.lang.annotation.RetentionPolicy;
  5. import java.lang.annotation.Target;
  6. /**
  7. * @program: Test
  8. * @description:
  9. * @author: yddacy
  10. * @create: 2018-12-06 09:28
  11. **/
  12. /**
  13. * @Retention :用来说明该注解类的生命周期。它有以下三个参数:
  14. *
  15. * RetentionPolicy.SOURCE : 注解只保留在源文件中
  16. * RetentionPolicy.CLASS : 注解保留在class文件中,在加载到JVM虚拟机时丢弃
  17. * RetentionPolicy.RUNTIME : 注解保留在程序运行期间,此时可以通过反射获得定义在某个类上的所有注解。
  18. */
  19. @Retention(RetentionPolicy.RUNTIME)
  20. /**
  21. * @Target : 用来说明该注解可以被声明在那些元素之前。
  22. *
  23. * ElementType.TYPE:说明该注解只能被声明在一个类前。
  24. * ElementType.FIELD:说明该注解只能被声明在一个类的字段前。
  25. *ElementType.METHOD:说明该注解只能被声明在一个类的方法前。
  26. *ElementType.PARAMETER:说明该注解只能被声明在一个方法参数前。
  27. *ElementType.CONSTRUCTOR:说明该注解只能声明在一个类的构造方法前。
  28. *ElementType.LOCAL_VARIABLE:说明该注解只能声明在一个局部变量前。
  29. *ElementType.ANNOTATION_TYPE:说明该注解只能声明在一个注解类型前。
  30. *ElementType.PACKAGE:说明该注解只能声明在一个包名前。
  31. */
  32. @Target(ElementType.FIELD)
  33. public @interface TestAnnotation {
  34. int id() default -1;
  35. String value() default "";
  36. }

1.元注解———5种:分别为:@Retention、@Documented、@Target、@Inherited、@Repeatable

@Retention

这个注解是表名自定义注解的存活时间,它有以下3个参数

RetentionPolicy.SOURCE : 注解只保留在源文件中。

RetentionPolicy.CLASS : 注解保留在class文件中,在加载到JVM虚拟机时丢弃。

RetentionPolicy.RUNTIME : 注解保留在程序运行期间,此时可以通过反射获得定义在某个类上的所有注解。

@Documented

一个简单的Annotations标记注解,表示是否将注解信息添加在java文档中

@Target

表示该注解用于什么地方。如果不明确指出,该注解可以放在任何地方。以下是一些可用的参数。需要说明的是:属性的注解是兼容的,如果你想给7个属性都添加注解,仅仅排除一个属性,那么你需要在定义target包含所有的属性。

ElementType.TYPE:说明该注解只能被声明在一个类前。
ElementType.FIELD:说明该注解只能被声明在一个类的字段前。
ElementType.METHOD:说明该注解只能被声明在一个类的方法前。
ElementType.PARAMETER:说明该注解只能被声明在一个方法参数前。
ElementType.CONSTRUCTOR:说明该注解只能声明在一个类的构造方法前。
ElementType.LOCAL_VARIABLE:说明该注解只能声明在一个局部变量前。
ElementType.ANNOTATION_TYPE:说明该注解只能声明在一个注解类型前。
ElementType.PACKAGE:说明该注解只能声明在一个包名前。

@Inherited

自定义注解加上这个元注解,然后自定义注解再去注解一个父类,如果有子类继承父类,那么这个子类也同样会继承父类的自定义注解。

@Repeatable

看下面代码@Repeatable 注解了 Person。而 @Repeatable 后面括号中的类相当于一个容器注解。里面的属性可以相当于一个数组看待,这个元注解是在jdk1.8才有的

  1. @Repeatable(Persons.class)
  2. public @interface Person{
  3. String role() default "";
  4. }
  5. @Person(role="CEO")
  6. @Person(role="husband")
  7. @Person(role="father")
  8. @Person(role="son")
  9. public class Man {
  10. String name="";
  11. }

注解的使用:

1.先定义一个注解

  1. package com.test.testdemo.annotation;
  2. import java.lang.annotation.ElementType;
  3. import java.lang.annotation.Retention;
  4. import java.lang.annotation.RetentionPolicy;
  5. import java.lang.annotation.Target;
  6. @Retention(RetentionPolicy.RUNTIME)
  7. @Target({ElementType.FIELD})
  8. public @interface TestAnnotation {
  9. int id() default -1;
  10. String value() default "";
  11. }

2.定义一个实体类,加上注解:

  1. package com.test.testdemo.model;
  2. import com.test.testdemo.annotation.TestAnnotation;
  3. public class TestModel {
  4. @TestAnnotation(value = "张三")
  5. private String name;
  6. @TestAnnotation(id = 18)
  7. private String age;
  8. @TestAnnotation(value = "三年级")
  9. private String grade;
  10. @TestAnnotation(id = 88)
  11. private String score;
  12. }

3.在main方法里面测试,利用反射获取注解的值

  1. public static void main(String[] args){
  2. Class clazz = TestModel.class;
  3. Field[] cField = clazz.getDeclaredFields();
  4. for (Field field : cField) {
  5. TestAnnotation fruitName = field.getAnnotation(TestAnnotation.class);
  6. if (fruitName != null) {
  7. System.out.println(fruitName.value());
  8. }
  9. }
  10. }

上面main方法运行的结果:

  1. 张三
  2. 三年级

好了,现在注解的使用就完成了

发表评论

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

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

相关阅读