Java自定义注解详解以及代码演示

本文详细介绍了Java注解的概念、作用,重点讲解了内置注解如@Override、@Deprecated和@SuppressWarnings,以及元注解@Target、@Retention、@Documented和@Inherited的用法。此外,还阐述了自定义注解的创建规则,并提供了实际应用示例,帮助读者理解注解的使用和反射获取注解信息的方法。

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

什么是注解:

  • Annontation 是Java5开始引入的新特征,中文名称叫注解。
  • 它提供了一种安全的类似注释的机制,用来将任何的信息戒元数据(metadata)与程序元素(类、方法、成员变量等)进行关联。
  • 为程序的元素(类、方法、成员变量)加上更直观更明了的说明,这些说明信息是与程序的业务逻辑无关,并且供指定的工具戒框架使用。
  • Annontation像一种修饰符一样,应用于包、类型、构造方法、方法、成员变量、参数及本地变量的声明语句中。
  • Java注解是附加在代码中的一些元信息,用于一些工具在编译、运行时进行解析和使用,起到说明、配置的功能。
  • 注解不会也不能影响代码的实际逻辑,仅仅起到辅劣性的作用。包含在java.lang.annotation 包中。

注解的作用:

  • 生成文档。这是最常见的,也是java 最早提供的注解。常用的有@param @return 等
  • 跟踪代码依赖性,实现替代配置文件功能。
  • 在编译时进行格式检查。如@override 放在方法前,如果你这个方法并不是覆盖了超类方法,则编译时就能检查出。

注解的原理 >>>>>>> 反射

内置注解

  • @Override:定义在java.lang.Override中,此注释只适用于修饰方法,表示一个方法声明打算重写超类中的另一个方法声明
  • @Deprecated:定义在java.lang.Deprecated中,此注释可以修饰方法、属性、类,表示不鼓励程序员使用这样的元素,通常是因为它很危险或者存在更好的选择
  • @SuppressWarnings:定义在java.lang.SuppressWarnings中,用来抑制编写编译时的警告信息

元注解

  • 元注解的作用是负责注解其他注解,java中定义了四个标准的meta-annotation类型,他们被用来提供对其他annotation类型作说明
  • 这些类型和它们所支持的类在java.lang.annotation包中
    • @Target:用来描述注解的使用范围(注解可以用在什么地方)
      • ElemenetType.CONSTRUCTOR 构造器声明
      • ElemenetType.FIELD 域声明(包括 enum 实例)
      • ElemenetType.LOCAL_VARIABLE 局部变量声明
      • ElemenetType.METHOD 方法声明
      • ElemenetType.PACKAGE 包声明
      • ElemenetType.PARAMETER 参数声明
      • ElemenetType.TYPE 类,接口(包括注解类型)或enum声明
    • @Retention:表示需要在什么级别保存该注释信息,描述注解的生命周期
      ▪ Source < Class < Runtime
      • RetentionPolicy.SOURCE 注解将被编译器丢弃
      • RetentionPolicy.CLASS 注解在class文件中可用,但会被JVM丢弃
      • RetentionPolicy.RUNTIME JVM将在运行期也保留注释,因此可以通过反射机制读取注解的信息
    • @Document:说明该注解将被包含在javadoc中
    • @Inherited:说明子类可以继承父类中的该注解

自定义注解

  • 使用@interfac自定义注解时,自劢继承了java.lang.annotation.Annotation接口
  • 使用规则:
    • @interface用来声明一个注解,格式:public @interface 注解名{}
    • 其中的每一个方法实际上是声明了一个配置参数
    • 方法的名称就是参数的名称
    • 返回值类型就是参数的类型(返回值叧能是基本类型,Class,String,enum)
    • 可以default来声明参数的默认值
    • 如果叧有一个参数成员,一般参数名为value
    • 注解元素必须要有值,我们定义注解元素时,经常使用空字符串,0作为默认值

注解完整的调用方式:

//target用来声明当前自定义的注解适合适用于什么地方,类,方法,变量,包。。。。
@Target({ElementType.METHOD,ElementType.TYPE})
//retention用来表示当前注解适用于什么环境,是源码级别还是类级别还是运行时环境,一般都是运行时环境
@Retention(RetentionPolicy.CLASS)
//表示该注解是否是显示在javadoc中
@Documented
//表示当前注解是否能够被继承
@Inherited
@interface MyAnnotation{

    //定义的方式看起来像是方法,但是实际上使用在使用注解的时候填写的参数的名称,默认的名称是value
    //自定义注解中填写的所有方法都需要在使用注解的时候,添加值,很麻烦,因此包含默认值
    String name() default "zhangsan";
    int age() default 12;
    int id() default 1;
    String[] likes() default {"a","b","c"};
}

实际中使用的注解:

package annotation;

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

public class MyAnnotation {
    /**
     * 注解类
     * 
     * @author suguoliang
     *
     */
    @Target(ElementType.TYPE)
    @Retention(RetentionPolicy.RUNTIME)
    public @interface MyClassAnnotation {
        String uri();

        String desc();
    }

    /**
     * 构造方法注解
     * 
     * @author suguoliang
     *
     */
    @Target(ElementType.CONSTRUCTOR)
    @Retention(RetentionPolicy.RUNTIME)
    public @interface MyConstructorAnnotation {
        String uri();

        String desc();
    }

    /**
     * 方法注解
     * 
     * @author suguoliang
     *
     */
    @Target(ElementType.METHOD)
    @Retention(RetentionPolicy.RUNTIME)
    public @interface MyMethodAnnotation {
        String uri();

        String desc();
    }

    /**
     * 字段注解
     * 
     * @author suguoliang
     *
     */
    @Target(ElementType.FIELD)
    @Retention(RetentionPolicy.RUNTIME)
    public @interface MyFieldAnnotation {
        String uri();

        String desc();
    }

    /**
     * 可以同时应用到类和方法上
     * 
     * @author 尐蘇
     *
     */
    @Target({ ElementType.TYPE, ElementType.METHOD })
    @Retention(RetentionPolicy.RUNTIME)
    public @interface MyClassAndMethodAnnotation {
        // 定义枚举
        public enum EnumType {
            util, entity, service, model
        }

        // 设置默认值
        public EnumType classType() default EnumType.util;

        // 数组
        int[] arr() default { 3, 7, 5 };

        String color() default "blue";
    }
}
package annotation;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

import annotation.MyAnnotation.MyClassAndMethodAnnotation;
import annotation.MyAnnotation.MyClassAndMethodAnnotation.EnumType;
import annotation.MyAnnotation.MyClassAnnotation;
import annotation.MyAnnotation.MyConstructorAnnotation;
import annotation.MyAnnotation.MyFieldAnnotation;
import annotation.MyAnnotation.MyMethodAnnotation;

@MyClassAnnotation(desc = "The Class", uri = "com.sgl.annotation")
@MyClassAndMethodAnnotation(classType = EnumType.util)
public class TestAnnotation {
    @MyFieldAnnotation(desc = "The Class Field", uri = "com.sgl.annotation#id")
    private String id;

    @MyConstructorAnnotation(desc = "The Class Constructor", uri = "com.sgl.annotation#constructor")
    public TestAnnotation() {
    }

    public String getId() {
        return id;
    }

    @MyMethodAnnotation(desc = "The Class Method", uri = "com.sgl.annotation#setId")
    public void setId(String id) {
        this.id = id;
    }

    @MyMethodAnnotation(desc = "The Class Method sayHello", uri = "com.sgl.annotation#sayHello")
    public void sayHello(String name) {
        if (name == null || name.equals("")) {
            System.out.println("hello world!");
        } else {
            System.out.println(name + "\t:say hello world");
        }
    }

    public static void main(String[] args) throws Exception {
        Class<TestAnnotation> clazz = TestAnnotation.class;
        // 获取类注解
        MyClassAnnotation myClassAnnotation = clazz.getAnnotation(MyClassAnnotation.class);
        System.out.println(myClassAnnotation.desc() + "+" + myClassAnnotation.uri());

        // 获得构造方法注解
        Constructor<TestAnnotation> constructors = clazz.getConstructor(new Class[] {});// 先获得构造方法对象
        MyConstructorAnnotation myConstructorAnnotation = constructors.getAnnotation(MyConstructorAnnotation.class);// 拿到构造方法上面的注解实例
        System.out.println(myConstructorAnnotation.desc() + "+" + myConstructorAnnotation.uri());

        // 获得方法注解
        Method method = clazz.getMethod("setId", new Class[] { String.class });// 获得方法对象
        MyMethodAnnotation myMethodAnnotation = method.getAnnotation(MyMethodAnnotation.class);
        System.out.println(myMethodAnnotation.desc() + "+" + myMethodAnnotation.uri());

        // 获得字段注解
        Field field = clazz.getDeclaredField("id");// 暴力获取private修饰的成员变量
        MyFieldAnnotation myFieldAnnotation = field.getAnnotation(MyFieldAnnotation.class);
        System.out.println(myFieldAnnotation.desc() + "+" + myFieldAnnotation.uri());
    }
}

参考连接:

Java的自定义注解及通过反射获取注解

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值