美文网首页
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核心知识点--元注解详解

相关文章

  • 菜鸟学服务端技术----Spirng基础

    注解 Java基础加强总结(一)——注解(Annotation) java中的注解是如何工作的? java 注解 ...

  • 自定义注解

    java annotation基础 java注解分为标准注解和元注解。 标准注解是java为我们提供的预定义的注解...

  • Java注解的使用

    Java注解的使用 参考 廖雪峰java教程 使用注解 什么是注解(Annotation)?注解是放在Java源码...

  • Java原生注解和Spring注解的说明

    注解 java 原生注解 Spring 中的注解 一 Java原生注解 Java注解是在JDK1.5以后引入的新特...

  • Java 注解

    JAVA注解 Java 自带注解(系统注解) @Override 表示重写注解 @Deprecated 表示过时的...

  • 1.8 Java 注解annotation

    1.1 注解声明 Java注解Annotation,有声明注解和元注解 元注解:Java提供的元注解,所谓元注解就...

  • Java注解学习总结(脑图)

    注解的提取测试:定义注解: 测试注解提取: 参考:《Java编程思想》java注解

  • Java注解

    Java注解(Annotation)详解(一)——概述及JDK自带注解 Java注解(Annotation)详解(...

  • JAVA-注解 Annotation

    JAVA-注解 Annotation sschrodinger 2018/6/4 基本 注解 Java 注解用于为...

  • Java注解简介篇

    摘要 本文详细介绍java注解是什么,如何声明java注解,如何解析java注解。最后介绍JDK提供的几大基本注解...

网友评论

      本文标题:Java注解

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