Java注解——元注解

一、定义

        写在注解定义上的注解叫元注解(注解是给程序提供信息,写在注解上的注解是给注解提供信息,给信息提供信息的叫元信息)。

二、元注解分类

        JDK定义了四种元注解,用来对注解的位置、作用范围、文档抽取、继承进行描述。

1.@Target:

        作用:描述定义的注解可以在哪里(包、类、接口、类成员方法、类成员变量、方法参数、局部变量、枚举类型)使用。不写就是表示可以用在所有位置上。

        JDK定义了ElementType枚举类型用于描述注解可以出现的位置,ElementType有如下枚举值:

  • ElementType.TYPE:能修饰类、接口或枚举类型,但是不能放在属性等其他位置
  • ElementType.FIELD:注解仅用于添加到成员变量
  • ElementType.METHOD:注解仅用于添加到成员方法
  • ElementType.PARAMETER:注解仅用于添加到方法参数
  • ElementType.CONSTRUCTOR:注解仅用于添加到构造方法
  • ElementType.LOCAL_VARIABLE:注解仅用于添加到局部变量
  • ElementType.ANNOTATION_TYPE:注解仅用于添加到注解(元注解)
  • ElementType.PACKAGE:注解仅用于添加到包

2.@Retention

        作用:描述注解的作用范围,即注解的有效范围。表示生命周期。告诉注解信息保留到哪个阶段,如果注释类型声明中不存在 Retention 注释,则保留策略默认为 RetentionPolicy.CLASS。

        JDK定义了三种有效范围,分别是:注解作用于源代码(编译器可读取注解)、注解作用于类文件(在类文件中可读取注解)、注解作用于运行过程(用反射技术读取注解)。

        JDK定义了RetentionPolicy枚举类型用于描述注解作用范围,RetentionPolicy有如下枚举值:

  • RetentionPolicy.SOURCE:注解作用于源代码(编译器要丢弃的注解)
  • RetentionPolicy.CLASS:注解作用于类文件(编译器将把注解记录在类文件中,但在运行时 JVM 不需要保留注释)
  • RetentionPolicy.RUNTIME:注解作用于运行时(编译器将把注解记录在类文件中,在运行时JVM 将保留注解,因此可以反射性地读取)
package anno;
 
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.TYPE;
 
import java.lang.annotation.Documented;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
 
@Target({METHOD,TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface JDBCConfig {
     String ip(); 
     int port() default 3306; 
     String database(); 
     String encoding(); 
     String loginName(); 
     String password(); 
}

3.@Documented

        作用:表示是否将我们的注解生成在Javadoc中。如果一个注解定义时间使用了该元注解,那么产生的javadoc文档就会把注解显示出来。

4.@Inherited

        作用:表示该注解具有继承性。用于子类是可以继承父类中的注解。

  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Java自定义注解通常用于对注解进行注释和描述,其使用方法如下: 1. 定义一个注解,使用@Target、@Retention、@Documented三个注解注解进行描述。 2. 使用@Inherited注解,表示注解可以被继承。 3. 在需要使用注解的类、方法、属性等上面使用定义的注解,通过反射获取注解信息。 示例代码如下: @Target({ElementType.TYPE, ElementType.FIELD, ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited public @interface CustomAnnotation { String value() default ""; } 在需要使用注解的类、方法、属性等上面使用定义的注解: @CustomAnnotation(value = "这是一个自定义注解") public class Test { @CustomAnnotation(value = "这是一个字段注解") private String name; @CustomAnnotation(value = "这是一个方法注解") public void sayHello() { System.out.println("Hello World!"); } } 通过反射获取注解信息: Class testClass = Test.class; CustomAnnotation annotation1 = (CustomAnnotation) testClass.getAnnotation(CustomAnnotation.class); System.out.println(annotation1.value()); // 输出 "这是一个自定义注解" Field nameField = testClass.getDeclaredField("name"); CustomAnnotation annotation2 = nameField.getAnnotation(CustomAnnotation.class); System.out.println(annotation2.value()); // 输出 "这是一个字段注解" Method sayHelloMethod = testClass.getDeclaredMethod("sayHello"); CustomAnnotation annotation3 = sayHelloMethod.getAnnotation(CustomAnnotation.class); System.out.println(annotation3.value()); // 输出 "这是一个方法注解"

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值