Java注解

来源:互联网 发布:java 多线程面试 编辑:程序博客网 时间:2024/05/16 10:05

注解相当于一个标记,在程序上加上注解就等于在程序上加上了标记,该标记不会影响代码的实际逻辑,仅仅起到了辅助性的作用,JAVAC编译器、开发工具和其他程序可以用反射来了解你的类以及各种元素上有无任何标记,看你有什么标记,就去干相应的事

元注解

元注解是注解的注解,用于修饰注解,包括@Retention、@Target、@Document、@Inherited

@Retention注解的保留策略

@Documented@Retention(RetentionPolicy.RUNTIME)@Target(ElementType.ANNOTATION_TYPE)public @interface Retention {    RetentionPolicy value();//枚举类型RetentionPolicy有三个可选值,单选}
  • @Retention(RetentionPolicy.SOURCE)—//注解仅存在于源码中,在class字节码文件中不包含
  • @Retention(RetentionPolicy.CLASS)—//默认的保留策略,注解会在class字节码文件中存在,但运行时无法获得,
  • @Retention(RetentionPolicy.RUNTIME)—//注解会在class字节码文件中存在,在运行时可以通过反射获取到

@Retention注解的作用目标

@Documented@Retention(RetentionPolicy.RUNTIME)@Target(ElementType.ANNOTATION_TYPE)public @interface Target {    ElementType[] value();//枚举类型ElementType的数组有8个可选值,多选}
  • @Target(ElementType.TYPE)—//接口、类、枚举、注解
  • @Target(ElementType.FIELD)—//字段、枚举的常量
  • @Target(ElementType.METHOD)—//方法
  • @Target(ElementType.PARAMETER)—//方法参数
  • @Target(ElementType.CONSTRUCTOR)—//构造函数
  • @Target(ElementType.LOCAL_VARIABLE)—//局部变量
  • @Target(ElementType.ANNOTATION_TYPE)—//注解
  • @Target(ElementType.PACKAGE)—//包

@Document说明该注解将被包含在javadoc中

@Documented@Retention(RetentionPolicy.RUNTIME)@Target(ElementType.ANNOTATION_TYPE)public @interface Documented {    //Documented中没有定义属性}

@Inherited说明子类可以继承父类中的该注解

@Documented@Retention(RetentionPolicy.RUNTIME)@Target(ElementType.ANNOTATION_TYPE)public @interface Inherited {    //Inherited中没有属性}

自定义注解

自定义注解要使用@interface注解,一个注解也是一个类

//定义注解@MyAnnotation@Target(ElementType.METHOD)@Retention(RetentionPolicy.RUNTIME)public @interface MyAnnotation {    String value();// 如果注解中有一个属性名字叫value,则在应用时可以省略属性名字不写。    String hello() default "hello";    int[] array() default { 1, 2, 3, 4 };}

保留策略为RetentionPolicy.RUNTIME的注解,在程序中能使用反射的方式从JVM中访问该注解的信息

public class MyAnnotationTest {    @MyAnnotation("fjh")    public void todo() {        System.out.println("todo method");    }    public static void main(String[] args) throws Exception {        Class<MyAnnotationTest> c = MyAnnotationTest.class;        Method m = c.getMethod("todo", new Class[] {});        if (m.isAnnotationPresent(MyAnnotation.class)) {            m.invoke(new MyAnnotationTest(), null);//todo method            MyAnnotation myAnnotation = m.getAnnotation(MyAnnotation.class);            System.out.println(myAnnotation.value());//fjh            System.out.println(myAnnotation.hello());//hello            System.out.println(myAnnotation.array().length);//4        }    }}
原创粉丝点击