Java:注解初步学习

注解的分类

按照运行机制分

  1. 源码注解: 注解只在源码中存在,编译成 .class 文件就不存在。
  2. 编译时注解: 注解在源码和.class文件中都存在。例如JDK中@Override@Deprecated@Suppvisewarnings
  3. 运行时注解 :在运行阶段还起作用,甚至会影响运行逻辑的注解。例如Spring中的@Autowired

按来源来分

  1. 来自JDK的注解
  2. 来自第三方的注解
  3. 自定义注解

自定义注解语法要求

@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface Description { // 使用@interface关键字定义注解

    String desc();  // 成员以无参无异常方式声明,返回类型就是参数的类型

    int age() default 18;   // 可以用default为成员指定一个默认值
}

注意点:

  1. 使用@interface自定义注解时,自动继承了java.lang.annotation.Annotation接口,由编译程序自动完成其他细节。在定义注解时,不能继承其他的注解或接口。
  2. 成员类型是受限的,合法的类型包括原始基本类型String,Class,Annotation,Enumeration以及以上所有类型的数组
  3. 如果注解只有一个成员,则成员名必须取名为value(),在使用时可以忽略成员名和赋值号(=)
  4. 注解类可以没有成员,没有成员的注解称为标识注解。

元注解(meta-annotation)

元注解即注解的注解,我们用元注解来定义其他注解。JDK5定义了4个标准meta-annotation类型,分别是@Target@Retention@Documented@Inherited

@Target

说明注解所修饰的对象范围,指注解可以用在什么地方。

  1. CONSTRUCTOR:用于描述构造器
  2. FIELD:用于描述域
  3. LOCAL_VARIABLE:用于描述局部变量
  4. METHOD:用于描述方法
  5. PACKAGE:用于描述包
  6. PARAMETER:用于描述参数
  7. TYPE:用于描述类、接口(包括注解类型) 或enum声明
@Retention

即上文提到的注解在什么时候起作用。

  1. SOURCE: 在源文件中有效(即源文件保留)
  2. CLASS: 在class文件中有效(即class保留)
  3. RUNTIME: 在运行时有效(即运行时保留)
@Documented

用于描述其它类型的annotation应该被作为被标注的程序成员的公共API,因此可以被例如javadoc此类的工具文档化。它是一个标记注解,没有成员。

@Inherited

它是一个标记注解,@Inherited阐述了某个被标注的类型是被继承的。如果一个使用了@Inherited修饰的annotation类型被用于一个class,则这个annotation将被用于该class的子类。

解析注解

通过反射获取类、函数或成员上的运行时注解信息,从而实现动态控制程序运行的逻辑。

@Description(desc = "I am class annotation")
public class Foo {
    @Description(desc = "I am method annotation")
    public String test() {
        return null;
    }
}
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

public class Test {

    public static void main(String[] args) {
        // 1、使用类加载器加载类
        try {
            Class c = Class.forName("me.shenchao.annotation.Foo");
            // 2、找到类上面的注解
            boolean isExist = c.isAnnotationPresent(Description.class);
            if (isExist) {
                // 3、拿到注解实例
                Description d = (Description) c.getAnnotation(Description.class);
                System.out.println(d.desc());
            }
            // 4、 找到方法上的注解
            Method[] methods = c.getMethods();
            for (Method method : methods) {
                boolean isMExist = method.isAnnotationPresent(Description.class);
                if (isMExist) {
                    Description d = method.getAnnotation(Description.class);
                    System.out.println(d.desc());
                }
            }

            // 另一种解析方法
            for (Method method : methods) {
                Annotation[] annotations = method.getAnnotations();
                for (Annotation annotation : annotations) {
                    if (annotation instanceof Description){
                        Description d = (Description) annotation;
                        System.out.println(d.desc());
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Reference

  • 慕课网《Java注解》
  • http://www.cnblogs.com/peida/archive/2013/04/24/3036689.html

 

定义:
注解(Annotation),也叫元数据。一种代码级别的说明。它是JDK5.0及以后版本引入的一个特性,与类、接口、枚举是在同一个层次。
它可以声明在包、类、字段、方法、局部变量、方法参数等的前面,用来对这些元素进行说明,注释。(“java注解”百度百科

作用分类:
①编写文档:通过代码里标识的元数据生成文档
②代码分析:通过代码里标识的元数据对代码进行分析
③编译检查:通过代码里标识的元数据让编译器能过实现基本的编译检查

元注解:
元注解的作用就是注解其他注解。java5.0定义了4个标准的meta-annotation类型,用以对其他的annotation类型做说明,他们分别是:

@Target:
说明了Annotation所修饰的对象的作用:用户描述注解的使用范围(即:被描述的注解可以用在什么地方)
取值(ElementType)有:
1.CONSTRUCTOR:用于描述构造器
2.FIELD:用于描述域
3.LOCAL_VARIABLE:用于描述局部变量
4.METHOD:用于描述方法
5.PACKAGE:用于描述包
6.PARAMETER:用于描述参数
7.TYPE:用于描述类、接口(包括注解类型) 或enum声明

@Retention:
作用:表示需要在什么级别保存该注释信息,用于描述注解的生命周期(即:被描述的注解在什么范围内有效)
取值(RetentionPolicy)
1.SOURCE:在源文件中有效
2.CLASS:在class文件中有效
3.RUNTIME:在运行时有效
取RUNTIME时,注解处理器可以通过反射,获取到该注解的属性值,做一些运行时的逻辑处理。

@Documented:
这是一个标记注解
用于描述其它类型的annotation应该被作为标注的程序成员的公共api,可以文档化

@Inherited:
标记注解
用该注解修饰的注解,会被子类继承。

自定义注解:

使用@interface来声明一个注解。其中的每一个方法实际上是声明了一个配置参数。方法的名称就是参数的名称。返回值的类型就是参数的类型。(返回值只能是基本类型、class、String、enum),可以用default来声明参数的默认值.

格式
public @interface 注解名 {定义体}
注解参数支持的数据类型:
1.所有基本数据类型(int,float,boolean,byte,double,char,long,short)
2.String类型
3.Class类型
4.enum类型
5.Annotation类型
6.以上所有类型的数组
只能用public或者default这两个访问权修饰.注解元素必须有确定的默认值,要么在定义的时候指定,要么在使用的时候指定。

例子:

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

//用于描述域
@Target(ElementType.FIELD)
// 运行是有效
@Retention(RetentionPolicy.RUNTIME)
public @interface Student {
    public enum Sex {
        MAN, WOMAN
    }

    /**
     * 1.参数成员只能用基本类型String,Enum,Class,annotations等数据类型,以及这一些类型的数组
     * 2.只能用public或默认(default)这两个访问权修饰. 3.如果只有一个参数成员,最好把参数名称设为"value"
     */
    String name() default "";

    // public修饰
    public int age() default 18;

    public Sex sex() default Sex.MAN;

}

注解处理器

这是一个自己编写的工具类,通过注解处理器对程序元素中的注解进行操作。
获得Annotation信息的方法:
方法1: T getAnnotation(Class annotationClass): 返回改程序元素上存在的、指定类型的注解,如果该类型注解不存在,则返回null。

方法2:Annotation[] getAnnotations():返回该程序元素上存在的所有注解。

方法3:boolean is AnnotationPresent(Annotation):判断该程序元素上是否包含指定类型的注解,存在则返回true,否则返回false.

方法4:Annotation[] getDeclaredAnnotations():返回直接存在于此元素上的所有注释。与此接口中的其他方法不同,该方法将忽略继承的注释。(如果没有注释直接存在于此元素上,则返回长度为零的一个数组。)该方法的调用者可以随意修改返回的数组;这不会对其他调用者返回的数组产生任何影响。

    public class StudentInfoUtil {

    public static void getStudentInfo(Class<?> clazz) {
        // 获取程序对象所有的Field注释
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            // 判断是否为Student注释
            if (field.isAnnotationPresent(Student.class)) {
                // 获取Annotation对象
                Student student = field.getAnnotation(Student.class);
                System.out.println("姓名" + student.name() + "年龄" + student.age()
                        + "性别" + student.sex());
            }
        }
    }

}

下面写个实例看它怎么用,先写一个student实体bean

public class StudentBean {

    private String name;
    private int age;
    private String sex;

    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 String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

}

再写个测试

   public class Test {
    @Student(name = "Snail", age = 18, sex = Sex.MAN)
    private StudentBean student01;
    @Student(name = "Snail2", age = 19, sex = Sex.MAN)
    private StudentBean student02;

    public static void main(String[] args) {
        StudentInfoUtil.getStudentInfo(Test.class);
    }

}

输出如下:

姓名Snail年龄18性别MAN
姓名Snail2年龄19性别MAN

可以看到,这里注解可以省去重复声明赋值的麻烦。

参考
深入理解Java:注解(Annotation)自定义注解入门
深入理解Java:注解(Annotation)–注解处理器

 

本文:Java:注解初步学习

Loading

Add a Comment

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.