美文网首页
Java注解

Java注解

作者: AC编程 | 来源:发表于2020-08-03 10:56 被阅读0次

    一、Annotation(注解)

    从JDK 1.5开始, Java增加了对元数据(MetaData)的支持,也就是 Annotation(注解)。

    注解其实就是代码里的特殊标记,它用于替代配置文件:传统方式通过配置文件告诉类如何运行,有了注解技术后,开发人员可以通过注解告诉类如何运行。在Java技术里注解的典型应用是:可以通过反射技术去得到类里面的注解,以决定怎么去运行类。

    注解可以标记在包、类、属性、方法,方法参数以及局部变量上,且同一个地方可以同时标记多个注解。

    import java.lang.annotation.*;
    
    @Target({ElementType.TYPE}) // 该注解用在类上
    @Retention(RetentionPolicy.RUNTIME) // 运行时可通过反射机制获取该注解
    @Documented // 该注解包含到javadoc中
    // @Inherited 该注解可被继承
    
    public @interface AlanChenController {
    
        String value() default "";
    }
    
    

    二、meta-annotation(元注解)

    除了直接使用JDK 定义好的注解,我们还可以自定义注解,在JDK 1.5中提供了4个标准的用来对注解类型进行注解的注解类,我们称之为 meta-annotation(元注解),他们分别是:@Documented、@Retention、@Target 、@Inherited

    1.1 @Documented

    Documented注解的作用是:描述在使用 javadoc 工具为类生成帮助文档时是否要保留其注解信息。

    1.2 @Retention

    Reteniton注解的作用是:描述注解保留的时间范围(即:被描述的注解在它所修饰的类中可以被保留到何时) 。

    Reteniton注解用来限定那些被它所注解的注解类在注解到其他类上以后,可被保留到何时,一共有三种策略,定义在RetentionPolicy枚举中。

    @Documented
    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.ANNOTATION_TYPE)
    public @interface Retention {
        /**
         * Returns the retention policy.
         * @return the retention policy
         */
        RetentionPolicy value();
    }
    
    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,
    
        /**
         * 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
    }
    
    1.3 @Target

    Target注解的作用是:描述注解的使用范围(即:被修饰的注解可以用在什么地方) 。

    Target注解用来说明那些被它所注解的注解类可修饰的对象范围:注解可以用于修饰 packages、types(类、接口、枚举、注解类)、类成员(方法、构造方法、成员变量、枚举值)、方法参数和本地变量(如循环变量、catch参数),在定义注解类时使用了@Target 能够更加清晰的知道它能够被用来修饰哪些对象,它的取值范围定义在ElementType 枚举中。

    @Documented
    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.ANNOTATION_TYPE)
    public @interface Target {
        /**
         * Returns an array of the kinds of elements an annotation type
         * can be applied to.
         * @return an array of the kinds of elements an annotation type
         * can be applied to
         */
        ElementType[] value();
    }
    
    public enum ElementType {
        /** Class, interface (including annotation type), or enum declaration */
        TYPE,
    
        /** Field declaration (includes enum constants) */
        FIELD,
    
        /** Method declaration */
        METHOD,
    
        /** Formal parameter declaration */
        PARAMETER,
    
        /** Constructor declaration */
        CONSTRUCTOR,
    
        /** Local variable declaration */
        LOCAL_VARIABLE,
    
        /** Annotation type declaration */
        ANNOTATION_TYPE,
    
        /** Package declaration */
        PACKAGE,
    
        /**
         * Type parameter declaration
         *
         * @since 1.8
         */
        TYPE_PARAMETER,
    
        /**
         * Use of a type
         *
         * @since 1.8
         */
        TYPE_USE
    }
    
    • TYPE:表示可以用来修饰类、接口、注释类型或枚举类型;
    • PACKAGE:用来修饰包;
    • PARAMETER:可用用来修饰参数;
    • ANNOTATION_TYPE:可以用来修饰注解类型;
    • METHOD:可以用来修饰方法;
    • FILED:可以用来修饰属性(包括枚举常量);
    • CONSTURCTOR:可用用来修饰构造器;
    • LOCAL_VARIABLE:可用来修饰局部变量;
    1.4 @Inherited

    Inherited注解的作用是:使被它修饰的注解具有继承性(如果某个类使用了被@Inherited修饰的注解,则其子类将自动具有该注解)。

    接下来我们使用代码来进行测试,首先创建一个被@Inherited修饰的注解类MyInheritedAnnotation。

    import java.lang.annotation.ElementType;
    import java.lang.annotation.Inherited;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;
     
    @Inherited
    @Target(ElementType.TYPE)
    @Retention(RetentionPolicy.RUNTIME)
    public @interface MyInheritedAnnotation {
     
        public String name() default "pengjunlee";
    }
    

    创建一个带有 MyInheritedAnnotation 注解的父类和一个无任何注解的子类。

    @MyInheritedAnnotation(name="parent")
    public class Parent {
     
    }
    
    public class Child extends Parent{
     
        public static void main(String[] args) {
            Class<Child> child=Child.class;
            MyInheritedAnnotation annotation = child.getAnnotation(MyInheritedAnnotation.class);
            System.out.println(annotation.name());
        }
    }
    

    运行程序,打印结果如下:

    parent
    

    三、注解应用举例

    首先自定义一个注解类

    package com.pengjunlee;
     
    import java.lang.annotation.ElementType;
    import java.lang.annotation.Inherited;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;
     
    @Target({ ElementType.TYPE, ElementType.METHOD })
    @Retention(RetentionPolicy.RUNTIME)
    @Inherited
    public @interface MyAnnotation {
     
        public String name() default "pengjunlee";
    }
    

    在 AnnotationTest 中使用反射获取注解信息。

    package com.pengjunlee;
     
    import java.lang.annotation.Annotation;
    import java.lang.reflect.Method;
     
    @MyAnnotation(name = "name of type")
    public class AnnotationTest {
     
        @MyAnnotation(name = "name of method")
        public String hello() {
            return "hello";
        }
     
        public static void main(String[] args) throws NoSuchMethodException, SecurityException {
     
            Class<AnnotationTest> annotationTest = AnnotationTest.class;
            // 获取类上的所有注解
            Annotation[] annotations = annotationTest.getAnnotations();
            for (Annotation annotation : annotations) {
                // 获取注解的全类名
                System.out.println(annotation.annotationType().getName());
            }
     
            // 获取 hello() 方法
            Method method = annotationTest.getMethod("hello", new Class[] {});
     
            // hello() 方法上是否有 MyAnnotation 注解
            if (method.isAnnotationPresent(MyAnnotation.class)) {
     
                // 获得注解
                MyAnnotation annotation = method.getAnnotation(MyAnnotation.class);
     
                // 获取注解的内容
                System.out.println(annotation.name());
     
            }
        }
    }
    

    运行程序,打印结果如下

    com.pengjunlee.MyAnnotation
    name of method
    

    资料来源:JAVA核心知识点--元注解详解

    相关文章

      网友评论

          本文标题:Java注解

          本文链接:https://www.haomeiwen.com/subject/znrrrktx.html