java 注解(Annotation)

本文深入讲解Java注解的原理及应用,包括注解的历史背景、基本概念、自定义注解的方法,以及如何利用注解进行代码增强。同时,介绍了三种内置注解,并演示了通过反射获取注解的实际操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    从 JDK 5.0 开始, Java 增加了对元数据(MetaData) 的支持, 也就是 Annotation(注解)。
    Annotation 其实就是代码里的特殊标记, 这些标记可以在编译, 类加载, 运行时被读取, 并执行相应的处理. 通过使用 Annotation, 程序员可以在不改变原有逻辑的情况下, 在源文件中嵌入一些补充信息。
    Annotation 可以像修饰符一样被使用, 可用于修饰包,类, 构造器, 方法, 成员变量, 参数, 局部变量的声明, 这些信息被保存在 Annotation 的 “name=value” 对中。
    Annotation 能被用来为程序元素(类, 方法, 成员变量等) 设置元数据。

    使用 Annotation 时要在其前面增加 @ 符号, 并把该 Annotation 当成一个修饰符使用。用于修饰它支持的程序元素

    三个基本的 Annotation:
    @Override: 限定重写父类方法, 该注解只能用于方法
    @Deprecated: 用于表示某个程序元素(类, 方法等)已过时
    @SuppressWarnings: 抑制编译器警告

自定义Annotation

定义新的 Annotation 类型使用 @interface 关键字
Annotation 的成员变量在 Annotation 定义中以无参数方法的形式来声明. 其方法名和返回值定义了该成员的名字和类型. 

可以在定义 Annotation 的成员变量时为其指定初始值, 指定成员变量的初始值可使用 default 关键字

public @interface MyAnnotation{
           String name() default “atguigu";
}

没有成员定义的 Annotation 称为标记; 包含成员变量的 Annotation 称为元数据 Annotation

JDK 5.0 在 java.lang.reflect 包下新增了 AnnotatedElement 接口, 该接口代表程序中可以接受注解的程序元素.

当一个 Annotation 类型被定义为运行时 Annotation 后, 该注释才是运行时可见, 当 class 文件被载入时保存在 class 文件中的 Annotation 才会被虚拟机读取.

JDK5.0提供了专门在注解上的注解类型,分别是:
    Retention
    Target
    Documented
    Inherited

@Retention: 只能用于修饰一个 Annotation 定义, 用于指定该 Annotation 可以保留多长时间, @Rentention 包含一个 RetentionPolicy 类型的成员变量, 使用 @Rentention 时必须为该 value 成员变量指定值:

    RetentionPolicy.SOURCE: 编译器直接丢弃这种策略的注解

    RetentionPolicy.CLASS: 编译器将把注解记录在 class 文件中. 当运行 Java 程序时, JVM 不会保留注解。 这是默认值

    RetentionPolicy.RUNTIME:编译器将把注释记录在 class 文件中. 当运行 Java 程序时, JVM 会保留注解. 程序可以通过反射获取该注解

    public enum RetentionPolicy{
        SOURCE,
        CLASS,
        RUNTIME
    }

@Target: 用于修饰 Annotation 定义, 用于指定被修饰的 Annotation 能用于修饰哪些程序元素. @Target 也包含一个名为 value 的成员变量。

@Documented: 用于指定被该元 Annotation 修饰的 Annotation 类将被 javadoc 工具提取成文档.
定义为Documented的注解必须设置Retention值为RUNTIME。

@Inherited: 被它修饰的 Annotation 将具有继承性.如果某个类使用了被 @Inherited 修饰的 Annotation, 则其子类将自动具有该注解。

package com.tz.annotation;

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

public class AnnotationTest {
    @Retention(RetentionPolicy.RUNTIME)
    @Target({ElementType.METHOD,ElementType.FIELD})
    public @interface MyAnnotation{
        String names() default "Jery";
    }
}
package com.tz.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface MyTiger {
    String value() default "小赵";
    int age();
}



package com.tz.annotation;

import java.lang.reflect.Method;

public class Person {
    private String name;
    @MyTiger(age = 20)
    public void setName(String name){
        this.name = name;
    }
    @Override
    public String toString(){
        return "";
    }
    public static void main(String[] args) {
        //通过反射获取方法上的注解
        Class class1 = Person.class;
        for( Method method:class1.getDeclaredMethods()){
//          System.out.println(method);
//          for(Annotation an:method.getAnnotations()){
//              System.out.println(an);
//          }

            MyTiger an = method.getAnnotation(MyTiger.class);
            //System.out.println(method+"==========="+an);
            if(an!=null){
                System.out.println(method+"==="+an.value()+"==="+an.age());
            }
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值