二、自定义注解

在定义自己的注解之前,必须要了解jvm提供的元注解和相关定义注解的语法。

一、元注解

  元注解的作用就是负责注解其他注解。4个标准的meta-annotation类型,用来对其它 annotation类型进行说明

1、@Target

   @Target说明Annotation类型所能修饰程序元素类型范围ElementType,在Annotation类型的声明中使用@Target可以明确Annotation类型可标记的程序元素类型。

@Target(ElementType.TYPE)
public @interface Table {
    public String tableName() default "className";
}

@Target(ElementType.FIELD)
public @interface NoDBColumn {
}

注解@Table 可以用于注解类、接口(包括注解类型) 或enum声明,而注解@NoDBColumn仅用于标记成员变量。

2、@Retention

  说明了被标记Annotation类型的生命周期:某些Annotation类型仅在源代码阶段保留,之后后被丢弃;有些被编译在class文件中;编译在class文件中的Annotation类型可能会被虚拟机忽略,而另一些在class被加载时读取(注意并不影响class的执行,因为Annotation类型与class在使用上是被分离的)。使用这个meta-Annotation可以对 被标记Annotation类型的“生命周期”进行限制。RetentionPoicy

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Column {
    public String name() default "fieldName";
    public String setFuncName() default "setField";
    public String getFuncName() default "getField"; 
    public boolean defaultDBValue() default false;
}

  Column注解的的RetentionPolicy的属性值是RUTIME,这样注解处理器可以通过反射,获取到该注解的属性值,从而去做一些运行时的逻辑处理

3、@Documented

  @Documented用于标记一个Annotation类型,因此可以被例如javadoc此类的工具文档化。是一个标记注解,没有成员。

4、@Inherited

  是一个标记注解,被该注解标注的Annotation类型,在使用Annotation类型标记一个java类时,会同样作用在该类的所有子类上(有注解被标记类中继承或者传递的意思)。如果一个使用了@Inherited修饰的annotation类型被用于一个class,则这个annotation将被用于该class的子类。

  注意:@Inherited Annotation类型表明被标注过的class的子类所继承。类并不从它所实现的接口继承annotation,方法并不从它所重载的方法继承annotation。

  当@Inherited Annotation类型标注的annotation的Retention是RetentionPolicy.RUNTIME,则反射API增强了这种继承性。如果我们使用java.lang.reflect去查询一个@Inherited annotation类型的annotation时,反射代码检查将展开工作:检查class和其父类,直到发现指定的annotation类型被发现,或者到达类继承结构的顶层。

5、@Repeatable

  java8以后支持在同一个地方(某个方法或者某个类等)加上相同的注解,前提是要使用@Repeatable注解,但是在用之前必须知道如何定义一个自定义注解
@Repeatable的使用案例:
https://blog.csdn.net/ljcgit/article/details/81708679

二、自定义注解

  使用@interface自定义注解时,会自动继承java.lang.annotation.Annotation接口,编译器会自动完成其他细节。自定义定义注解时,不能继承其他的注解或接口,其中的每一个方法实际上是声明了一个配置参数。方法的名称就是参数的名称,返回值类型就是参数的类型。可以通过default来声明参数的默认值。

1、定义注解格式

  public @interface 注解名 {定义体}

  注解成员的可支持数据类型:

    1.所有基本数据类型(int,float,boolean,byte,double,char,long,short)
    2.String类型
    3.Class类型
    4.enum类型
    5.Annotation类型
    6.以上所有类型的数组

  Annotation类型里面的成员设定的规定:
  第一:只能用public或默认(default)这两个访问权修饰.例如,String value();这里成员访问修饰符缺省则为defaul默认类型;
  第二:如果只有一个成员,最好把成员名称设为"value"。例如:下面的例子FruitName注解就只有一个参数成员。

import java.lang.annotation.Documented;
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)
@Documented
public @interface FruitName {
    String value() default "";
}


import java.lang.annotation.Documented;
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)
@Documented
public @interface FruitColor {

    /**
     * 颜色属性
     */
    Color fruitColor() default Color.GREEN;
    
     /**
     * 内部枚举类
     */
    public enum Color{BULE,RED,GREEN};

}


import annotation.FruitColor.Color;

public class Apple {
    
    @FruitName("Apple")
    private String appleName;
    
    @FruitColor(fruitColor=Color.RED)
    private String appleColor;

    public void setAppleColor(String appleColor) {
        this.appleColor = appleColor;
    }
    
    public String getAppleColor() {
        return appleColor;
    }

    public void setAppleName(String appleName) {
        this.appleName = appleName;
    }
    
    public String getAppleName() {
        return appleName;
    }
    
    public void displayName(){
        System.out.println("水果的名字是:苹果");
    }
}

2、注解元素的默认值

  注解类型的成员必须有确定的值,要么在定义注解类型成员的默认值中指定,要么在使用注解时指定,非基本数据类型的注解元素的值不可为null。因此, 使用常用空字符串或0作为默认值。这个约束使得处理器很难表现一个注解类型成员的存在或缺失状态,因为每个注解类型的声明中,所有成员都存在,并且都具有相应的值,为了绕开这个约束,我们只能定义一些特殊的值,例如空字符串或者负数,以此表示某个成员不存在,在自定义注解类型时,这已经成为一个习惯用法。例如:

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

/**
 * 水果供应者注解
 * @author peida
 *
 */
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface FruitProvider {
    /**
     * 供应商编号
     */
    public int id() default -1;

    /**
     * 供应商名称
     */
    public String name() default "";

    /**
     * 供应商地址
     */
    public String address() default "";
}

3、最后

  自定义了注解类型,并在需要的时候给相关程序元素添加注解时,如果没有响应的注解信息处理流程,添加的注解实际上没有任何价值。要让注解发挥作用,主要就在于注解处理方法。

posted @ 2021-09-23 01:07  黄河大道东  阅读(377)  评论(0编辑  收藏  举报