美文网首页
2018-02-28Java注解

2018-02-28Java注解

作者: EdisonJQ | 来源:发表于2018-03-01 11:02 被阅读4次

参考:《java编程思想》

一。注解分为3类:

1.标准注解(3个):

  • @Override:标识该方法继承自超类。
  • @Deprecated:表示该类或者方法已经不推荐使用。
  • @SuppressWarnings:用于忽略的编译器警告信息。

2.元注解(4个):

  • @Documented:将此注解包含在javadoc中。

  • @Inherited:允许子类继承父类中的注解。

  • @Target:描述注解的使用范围。
    ElementType取值:

              1.CONSTRUCTOR:用于描述构造器
    
              2.FIELD:用于描述域
    
              3.LOCAL_VARIABLE:用于描述局部变量
    
              4.METHOD:用于描述方法
    
              5.PACKAGE:用于描述包
    
              6.PARAMETER:用于描述参数
    
              7.TYPE:用于描述类、接口(包括注解类型) 或enum声明
    
  • Retention:表示需要在什么级别保存该注解信息。
    RetentionPolicy参数取值:

              1.SOURCE:注解将被编译器丢弃。
    
              2.CLASS:注解在class文件中可用,但会被VM丢弃。
    
              3.RUNTIME:VM将在运行期保留注解,因此可以通过反射机制来读取注解的信息。
    

3.自定义注解:

元注解专职负责注解其他的注解,因此举例以下自定义注解:
TestAnnotation.java注解类

@Documented
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface TestAnnotation {
    int id();    //这个就是注解元素

    String desc() default "no description";
}

值得注意:
1、注解元素的类型为:

  • 所有基本类型(int,float,boolean等)。
  • String
  • Class
  • enum
  • Annotation
  • 以上类型的数组

2、注解元素默认值的限制:
注解元素的默认值不能为null,需要定义表示空时,可以自己定义特殊的值如:int id() default -1;String desc() default "";

二.自定义注解实战:

想象数据库的各种元素:数据库名称,约束关系,字段属性等。
看以下关系:


类关系.png

先定义DBTable注解,用于定义数据库的名称:

package db;

import java.lang.annotation.*;

@Documented
@Inherited
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface DBTable {
    String value() default "";//默认为空,不能写null
}

再看Constants,用于定义约束关系:

package db;

import java.lang.annotation.*;

@Documented //生成javadoc
@Inherited  //让子类继承父类的注解
@Target(ElementType.FIELD)  //修饰字段
@Retention(RetentionPolicy.RUNTIME) //运行时保留注解
public @interface Constants {
    boolean primaryKey() default false;

    boolean allowNull() default true;
}

再看ColumnString,定义字段属性:

package db;

import java.lang.annotation.*;

@Documented
@Inherited
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ColumnString {
    String value() default "";

    Constants constants() default @Constants;
}

再看ColumnInteger,定义字段属性:

package db;

import java.lang.annotation.*;

@Documented
@Inherited
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ColumnInteger {
    int value() default -1;

    Constants constants() default @Constants;
}

所有注解都准备好了,现在定义Person类:

package db;

@DBTable("Person")
public class Person {
    @ColumnInteger(value = 1, constants = @Constants(primaryKey = true, allowNull = false))
    private int id;
    @ColumnInteger(value = 24)
    private int age;
    @ColumnString(value = "dxj")
    private String name;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

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

现在要编写注解解析器:

public class AnonotationTest {
    public static void main(String[] args) {
        getPersonAnnotation(Person.class);
    }

    public static void getPersonAnnotation(Class<?> cl) {
        //获取类的注解
        Annotation[] declaredAnnotations = cl.getDeclaredAnnotations();
        for (Annotation annotation : declaredAnnotations) {
            if (annotation instanceof DBTable) {
                System.out.println("数据库名称:" + ((DBTable) annotation).value());
            }
        }
        //获取类的字段
        Field[] declaredFields = cl.getDeclaredFields();
        for (Field f : declaredFields) {
            //获取字段的注解
            Annotation[] fDeclaredAnnotations = f.getDeclaredAnnotations();
            for (Annotation annotation : fDeclaredAnnotations) {
                if (annotation instanceof ColumnString) {
                    System.out.println("字段名:" + f.getName() + ",字段长度:" + ((ColumnString) annotation).value() +
                            ",是否主键:" + ((ColumnString) annotation).constants().primaryKey() +
                            ",是否允许为空:" + ((ColumnString) annotation).constants().allowNull());
                }
                if (annotation instanceof ColumnInteger) {
                    System.out.println("字段名:" + f.getName() + ",字段长度:" + ((ColumnInteger) annotation).value() +
                            ",是否主键:" + ((ColumnInteger) annotation).constants().primaryKey() +
                            ",是否允许为空:" + ((ColumnInteger) annotation).constants().allowNull());
                }
            }
        }
    }
}

最后输出的结果为:


注解输出结果.png

注解解析器通过反射获取注解上的值。

相关文章

  • 2018-02-28Java注解

    参考:《java编程思想》 一。注解分为3类: 1.标准注解(3个): @Override:标识该方法继承自超类。...

  • 注解学习笔记

    什么是注解注解分类注解作用分类 元注解 Java内置注解 自定义注解自定义注解实现及使用编译时注解注解处理器注解处...

  • 注解与反射

    注解 声明一个注解类型 元注解 在定义注解时,注解类也能够使用其他的注解声明。对注解类型进行注解的注解类,我们称之...

  • 1.8 Java 注解annotation

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

  • 注解的使用

    元注解 注解 注解本质就是接口: 元注解:修饰注解的注解 自定义注解 Text.java FruitName.ja...

  • 注解

    Java注解 注解 元注解 自定义注解 元注解:负责注解其他注解 共有4个标准的meta-annotation类型...

  • Spring高级应用之组合注解和元注解

    1.核心概念: 元注解:可以注解在其他注解上的注解;被注解的注解成为组合注解; 2.组合注解的定义步骤 定义组合注...

  • 2016.10.13-关于注解的自定义和注解的解析

    注解可以分为:1、标识性注解(没有成员变量) 2、注解 3、元注解(注解的注解) 1、注解的自定义 自定义注解的格...

  • 自定义注解

    注解分类 1、代码注解2、编译时注解3、运行时注解 注解范例 使用注解的类 注解解析类 注解实战 需求1、有一张用...

  • 【JAVA】注解

    元注解 用来定义、声明注解的注解。 @Inherited注解 使用此注解声明出来的自定义注解,在使用此自定义注解时...

网友评论

      本文标题:2018-02-28Java注解

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