注解,就是那么简单!!!

1,897 阅读7分钟

深入理解注解

1、基本介绍

基本概念注解,顾名思义,就是对某一事物进行添加注释说明,会存放一些信息,这些信息可能对以后某个时段来说是很有用处的。Java 注解(Annotation)又称 Java 标注,是 JDK5.0 引入的一种注释机制。Java 语言中的方法变量参数等都可以被标注(添加某些信息)。在编译器生成类文件时,标注可以被嵌入到字节码中。Java 虚拟机可以保留标注内容,在运行时可以通过反射的方式获取到标注内容 。 当然它也支持自定义 的Java 标注。

注解与注释的区别

定义不同:注解与类、接口在同一层次的,是一种描述数据的数据,可以理解为注解就是源代码的元数据。注释则是对源代码的介绍,方便开发者理解代码的所撰写的文字。

作用不同:注解是Java 编译器可以理解的部分,是给编译器看的。通过标记包、类、字段、方法、局部变量、方法参数等元数据,告诉jvm这些元数据的信息。注释是程序员对源代码做一些记忆或提示性描述,是给人来看的。它能告诉开发者这段代码的逻辑、说明、特点等内容,对代码起到解释、说明的作用。

使用范围不同:使用范围不同:注解 ,参与代码编译,以@开头的,与工具一起使用。对于位置、语法、内容有一定的限制。注释 ,可以随意在任务位置填写内容,对代码任何没有影响。

总之,注解可以理解为对类、变量、方法和接口进行规范和约束,注释则理为开发者对代码进行解释而撰写的文字。

注解可以根据来源可以分为系统注解自定义注解第三方注解,系统注解根据用途可以分为内置注解元注解,在下面的文章中,我们主要讲解内置注解元注解自定义注解

2 内置注解

在java.lang包下存在着我们经常看到的注解,分别是@Deprecated@Override和@SuppressWarnings

2.1@Deprecated注解

@Deprecated可以修饰方法变量,被@Deprecated修饰后表示不建议使用,它的存在仅仅是为了兼容以前的程序,由于不能直接把它抛弃,所以将它设置为过时。但是被这个注解修饰的类、方法在高版本的JDK中使用时了可能会出现错误。

源代码如下

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(value={CONSTRUCTOR, FIELD, LOCAL_VARIABLE, METHOD, PACKAGE, PARAMETER, TYPE})
public @interface Deprecated {
}

其中@Documented、@Retention和@Target是元注解,我们在下文介绍

2.2@Override注解

它表明了被注解的方法需要重写父类中的方法,如果某个方法使用了该注解,却没有覆写超类中的方法,编译器就会报出错误。在子类中重写父类或接口的方法,@Overide并不是必须的。但是还是建议使用这个注解,因为在某些情况下,假设你修改了父类的方法的名字,那么之前重写的子类方法将不再属于重写,如果没有@Overide,你将不会察觉到这个子类的方法。有了这个注解修饰,编译器则会提示你这些信息。

源代码如下

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.SOURCE)
public @interface Override {
}

2.3@SuppressWarnings注解

@SuppressWarnings用来抑制编译器生成警告信息,可以修饰的元素为方法方法参数属性局部变量。它可以达到抑制编译器编译时产生警告的目的,使用@SuppressWarnings注解,采用就近原则,比如一个方法出现警告,尽量使用@SuppressWarnings注解这个方法,而不是注解方法所在的类。所属范围越小越好,因为范围大了,不利于发现该类下其他方法的警告信息。 但是我们通常不建议使用@SuppressWarnings注解,使用此注解,开发人员看不到编译时编译器提示的相应的警告,不能选择更好、更新的类、方法或者不能编写更规范的编码。同时后期更新JDK、jar包等源码时,使用@SuppressWarnings注解的代码可能受新的JDK、jar包代码的支持,出现错误,仍然需要修改。

@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE})
@Retention(RetentionPolicy.SOURCE)
public @interface SuppressWarnings {
   String[] value();
}

3、元注解简介

在上面的代码中,我们看到了注解上还有注解,这种修饰注解的注解被称为元注解。事实上,元注解(meta-annotation)的作用就是注解其它的注解,Java在java.lang.annotation包中定义了4个标准的元注解类型,分别为@Target@Retention@Documented@Inherited

3.1@Target

@Target用于描述注解的使用范围,即被描述的注解可以用到什么地方,@Target注解内定义了ElemenType[]数组,数组以枚举类的形式定义了注解的修饰范围。通过下面的源代码我们可以看出,该注解能够用于接口构造器属性方法参数声明注解声明

源代码如下

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Target {
 
    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
}

3.2@Rentention

@Rentention表示需要在什么级别保存该注释信息,用于描述注解的生命周期,通过源代码,我们可以看出,注解内有个RetentionPolicy的值,我们继续深入往下看,RetentionPolicy是个枚举类型的值,它有三个值可供选择,SOURCE是在源代码层面,在编译是将会失效;CLASS作用在class文件中,但是在运行时失效RUNTIME在运行时依旧保留该注解,因此可以通过反射机制来读取注解内的信息。因此,这三个值对应的生命周期大小为:SOURCE<CLASS<RUNTIME,如果不手动添加的话,则默认为CLASS。

源代码如下

@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
}

3.3@Documented

@Documented表明该注解将被包含在javadoc中。该注解用的相对较少。

源代码如下

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Documented {
}

3.4@Inherited

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

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Inherited {
}

4、自定义注解

通过使用元注解的组合,我们可以按照自己的需求来自定义注解,我们通常使用@interface来自定义注解,它自动继承了java.lang.annotation.Annotation接口。接下,我们来以一段代码仔细分析下如何自定义注解:

  • 注解一
public @interface MyAnnotation{
}

注解一是一个最简单的自定义注解,我们可以看到,它以public修饰,以@interface用来声明一个注解,具体的格式为:public @interface 注解名{定义内容},如果要在注解内添加一个参数,该怎样定义呢?

  • 注解二
@Retention(value = RetentionPolicy.RUNTIME)
@interface myAnnotantion3{
    //参数名为vale,当注解内只有一个参数,使用注解时,参数名可省略
    String value();
}

特别需要注意的是,注解内的方法名称就是参数的名称,而返回值类型就是参数类型,我们可以这样使用

@Retention(value = RetentionPolicy.RUNTIME)
@myAnnotantion3("snow")
    public void test2(){
    }

因为注解内只有一个参数,所以在使用注解时,参数名称是可以省略的。

如果,我们我们想添加多个的参数值,该怎么自定义注解呢

  • 注解三
Target(value = {ElementType.METHOD,ElementType.TYPE})
@Retention(value = RetentionPolicy.RUNTIME)
@interface myAnnotaion2{
    /**
     *表示注解的参数,name参数名,String 表示参数的类型
     * 参数加default,在注解内可写可不写
     */
    String name();
    int age();
    int id();
}

同理,我们只需要在相应的位置引用该注解即可

@myAnnotaion2(name = "Simon",age=25,id=23)
    public void test1(){
    }

如果我们向给注解内的参数设定默认值,我们可以这样做

  • 注解四
@Target(value = {ElementType.METHOD,ElementType.TYPE})
@Retention(value = RetentionPolicy.RUNTIME)
@interface myAnnotaion2{
    /**
     *表示注解的参数,name参数名,String 表示参数的类型
     * 参数加default,在注解内可写可不写
     */
    String name() default "";
    int age() default 0;
    int id() default -1;
}

在实际的方法使用中,我们只需要给必须要修改的值赋值即可

 @myAnnotaion2(name = "Simon")
    public void test1(){
        System.out.println("测试注解1");
    }

因此,自定义注解可以归纳如下:

  • @interface用来声明一个注解,格式:public @interface 注解名{定义内容}

  • 其中每一个方法实际上声明了一个配置参数

  • 方法的名称就是参数的名称

  • 返回值类型就是参数的类型(返回值只能时基本类型,Class,String,enum)

  • 可以通过default用来声明参数的默认值

  • 如果只有一个参数成员,一般参数名为value

  • 注解元素必须要有值,我们定义注解元素时,经常使用空字符串0作为默认值

5、获取注解中的参数值

在以上的讲解中,我们使用注解都是对所修饰的类、方法、变量进行规范和约束,在大多数使用场景中,以方法为例,我们需要将注解中的信息同方法联系起来,即将注解中的参数信息的注入到方法中。

5.1参数值是基本类型

  • 首次,我们创建一个带有参数的注解
@Target({ElementType.ANNOTATION_TYPE, ElementType.METHOD})
@Retention(value = RetentionPolicy.RUNTIME)
@interface MyAnnotation5 {
    String name() default " ";

    int age() default 0;
}
  • 将该注解修饰到某一方法上
  @MyAnnotation5(name = "Simon", age = 25)
    public void testInjectValue(String name,int age) {

        System.out.println("获取注解中的参数值:");
        System.out.println(name);
        System.out.println(age);
    }  
  • 反射获取注解中的参数并注入到方法中
  • 反射获取该类的方法
  • 通过方法获取注解中的参数值
  • 将注解中的参数值注入到相应的方法中
		//反射获取类,并得到类中的方法        
 	    Class aClass = InjectValue.class;
        Method method=aClass.getMethod("testInjectValue",String.class,int.class);
		//获取注解中的属性值
        MyAnnotation5 myAnnotation5=method.getAnnotation(MyAnnotation5.class);
        String name=myAnnotation5.name();
        int age=myAnnotation5.age();
		//将属性值注入到相应的方法中
        Object o=aClass.newInstance();
        method.invoke(o,name,age);

5.2参数值是对象

前面我们讲解如何将注解中的参数为基本数据类型注入到方法中,那么如何将注解中的参数为对象注入到方法中呢?

  • 创建一个类用于生成对象
public class Animal {
   private String name;
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}
  • 创建一个类,类中的属性是上一个类的对象,并创建一个带参数的注解,参数的类型为对象
public class AnimalDao {
  private Animal animal;

    public Animal getAnimal() {
        return animal;
    }

    @MyAnnotation6(name = "Dog",age = 12)
    public void setAnimal(Animal animal) {
        this.animal = animal;
    }
}
  • 获取注解中的对象
public class TestInjectObject {
    public static void main(String[] args) throws Exception {
        //1.使用PropertyDescriptor得到想要注入的属性
        PropertyDescriptor descriptor = new PropertyDescriptor("animal", AnimalDao.class);

        //2.得到要想注入属性的具体对象
        Animal animal = (Animal) descriptor.getPropertyType().newInstance();

        //3.得到该属性的写方法
        Method method = descriptor.getWriteMethod();

        //4.得到写方法的注解
        Annotation annotation = method.getAnnotation(MyAnnotation6.class);

        //5.得到注解上的信息
        Method[] methods = annotation.getClass().getMethods();

        //6.将注解上的信息填充到animal对象上
        for (Method m : methods) {
            //得到注解上属性的名字
            String name = m.getName();
            //看看animal对象有没有与之对应的方法
            try {

                PropertyDescriptor descriptor1 = new PropertyDescriptor(name, Animal.class);
                Method method1 = descriptor1.getWriteMethod();
                //得到注解中的值
                Object o = m.invoke(annotation, null);
                //调用animal对象的setter方法,将注解上的值设置进去
                method1.invoke(animal, o);

            } catch (Exception e) {
                continue;
            }
        }
        AnimalDao animalDao = new AnimalDao();
        method.invoke(animalDao, animal);

        System.out.println(animalDao.getAnimal().getName());
        System.out.println(animalDao.getAnimal().getAge());
    }

}

@Target(value = {ElementType.METHOD, ElementType.TYPE})
@Retention(value = RetentionPolicy.RUNTIME)
@interface MyAnnotation6 {
    String name();

    int age();
}

因此,将对象注入到方法中可以总结如下

  • 创建想要获得属性的对象
  • 根据对象获取该属性的方法
  • 得到方法中的注解
  • 获取注解中的信息
  • 将注解信息注入到对象中
  • 将对象中的属性写入到方法中