Java的注解Annotation应用

记录:482

场景:Java的注解Annotation应用,包括但不限于注解作用在类上、注解作用在字段上、注解作用在构造函数上、注解作用在方法上、注解作用在方法形式参数上、注解作用在方法的返回参数上,一个注解作用在多个场景上。

版本:JDK 1.8,Spring Boot 2.6.3。

1.基础

1.1Annotation

在JDK的java.lang.annotation.Annotation中对Method的官方说明如下:

The common interface extended by all annotation types. Note that an interface that manually extends this one does not define an annotation type. Also note that this interface does not itself define an annotation type. More information about annotation types can be found in section 9.6 of The Java™ Language Specification. The reflect.AnnotatedElement interface discusses compatibility concerns when evolving an annotation type from being non-repeatable to being repeatable.

1.2Java 反射机制

Java 反射机制是在运行状态中,对于任意一个类,都能够获得这个类的所有属性和方法,对于任意一个对象都能够调用它的任意一个属性和方法。这种在运行时动态的获取信息以及动态调用对象的方法的功能称为Java 的反射机制。

2.注解作用在类上

注解作用在类上,在创建类的对象实例时,可以根据注解信息做一些拦截操作。

2.1定义注解

//作用在类上的注解
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Deep01ClassAno {
    String code();
    String value() default "";
}

2.2使用注解

@Deep01ClassAno(code = "deep01", value = "福建")
public class DeepWorker {
    @Deep02FieldAno(code = "deep02", value = "厦门")
    private String cityInfo;

    @Deep04ConstructorAno(code = "deep04", value = "创建城市")
    public DeepWorker() {
        this.cityInfo = "厦门";
    }

    @Deep03MethodAno(code = "deep03", value = "海沧")
    public @Deep06ReturnAno(code = "deep06", value = "海沧地铁2号线")
    String getDistrictInfo(@Deep05ParameterAno(code = "deep05", value = "地铁") String code) {
        return "接收信息: " + code;
    }
}

2.3获取和解析注解

Class<?> clz01 = DeepWorker.class;
//1.从类上获取注解常用方式
Annotation ann0101 = clz01.getDeclaredAnnotation(Deep01ClassAno.class);
Annotation[] ann0102 = clz01.getDeclaredAnnotations();
Annotation ann0103 = clz01.getAnnotation(Deep01ClassAno.class);
Annotation[] ann0104 = clz01.getAnnotations();
//2.解析注解值
Deep01ClassAno deep01 = (Deep01ClassAno) ann0101;
System.out.println("deep01注解,code=" + deep01.code() + ",value=" + deep01.value());

3.注解作用在字段上

注解作用在字段上,可以根据注解信息对字段做些校验等操作。

3.1定义注解

//作用在字段上的注解
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Deep02FieldAno {
    String code();
    String value() default "";
}

3.2使用注解

public class DeepWorker {
    @Deep02FieldAno(code = "deep02", value = "厦门")
    private String cityInfo;
}

3.3获取和解析注解

Class<?> clz02 = DeepWorker.class;
Field field = clz02.getDeclaredField("cityInfo");
//1.从字段上获取注解常用方式
Annotation ann0201 = field.getDeclaredAnnotation(Deep02FieldAno.class);
Annotation[] ann0202 = field.getDeclaredAnnotations();
Annotation ann0203 = field.getAnnotation(Deep02FieldAno.class);
Annotation[] ann0204 = field.getAnnotations();
//2.解析注解值
Deep02FieldAno deep02 = (Deep02FieldAno) ann0201;
System.out.println("deep02注解,code=" + deep02.code() + ",value=" + deep02.value());

4.注解作用在方法上

注解作用在方法上,比如:在反射操作时,可以根据注解信息找到需要执行的方法。

4.1定义注解

//作用在方法上的注解
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Deep03MethodAno {
    String code();
    String value() default "";
}

4.2使用注解

public class DeepWorker {
    @Deep03MethodAno(code = "deep03", value = "海沧")
    public @Deep06ReturnAno(code = "deep06", value = "海沧地铁2号线")
    String getDistrictInfo(@Deep05ParameterAno(code = "deep05", value = "地铁") String code) {
        return "接收信息: " + code;
    }
}

4.3获取和解析注解

Class<?> clz03 = DeepWorker.class;
Method method = clz03.getDeclaredMethod("getDistrictInfo", String.class);
//1.从方法上获取注解常用方式
Annotation ann0301 = method.getDeclaredAnnotation(Deep03MethodAno.class);
Annotation[] ann0302 = method.getDeclaredAnnotations();
Annotation ann0303 = method.getAnnotation(Deep03MethodAno.class);
Annotation[] ann0304 = method.getAnnotations();
//2.解析注解值
Deep03MethodAno deep03 = (Deep03MethodAno) ann0301;
System.out.println("deep03注解,code=" + deep03.code() + ",value=" + deep03.value());

5.注解作用在构造函数上

注解作用在构造函数上,可以根据注解信息做一些需求判断。

5.1定义注解

@Target(ElementType.CONSTRUCTOR)
@Retention(RetentionPolicy.RUNTIME)
public @interface Deep04ConstructorAno {
    String code();
    String value() default "";
}

5.2使用注解

@Deep01ClassAno(code = "deep01", value = "福建")
public class DeepWorker {
    @Deep04ConstructorAno(code = "deep04", value = "创建城市")
    public DeepWorker() {
        this.cityInfo = "厦门";
    }
}

5.3获取和解析注解

Class<?> clz04 = DeepWorker.class;
Constructor<?> constructor = clz04.getConstructor();
//1.从构造函数上获取注解常用方式
Annotation ann0401 = constructor.getDeclaredAnnotation(Deep04ConstructorAno.class);
Annotation[] ann0402 = constructor.getDeclaredAnnotations();
Annotation ann0403 = constructor.getAnnotation(Deep04ConstructorAno.class);
Annotation[] ann0404 = constructor.getAnnotations();
//2.解析注解值
Deep04ConstructorAno deep04 = (Deep04ConstructorAno) ann0401;
System.out.println("deep04注解,code=" + deep04.code() + ",value=" + deep04.value());

6.注解作用在方法形式参数上

注解作用在方法形式参数上,可以根据注解信息做些校验等操作。

6.1定义注解

//作用在形式参数上的注解
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
public @interface Deep05ParameterAno {
    String code();
    String value() default "";
}

6.2使用注解

public class DeepWorker {
    @Deep03MethodAno(code = "deep03", value = "海沧")
    public @Deep06ReturnAno(code = "deep06", value = "海沧地铁2号线")
    String getDistrictInfo(@Deep05ParameterAno(code = "deep05", value = "地铁") String code) {
        return "接收信息: " + code;
    }
}

6.3获取和解析注解

Class<?> clz05 = DeepWorker.class;
Method method02 = clz05.getDeclaredMethod("getDistrictInfo", String.class);
//1.从方法上获取形式参数的注解常用方式
Annotation[][] ann0501 = method02.getParameterAnnotations();
//2.解析注解值-遍历二维数组
for (int i = 0; i < ann0501.length; i++) {
    if (ann0501[i] == null) continue;
    for (int j = 0; j < ann0501[i].length; j++) {
        Deep05ParameterAno deep05 = (Deep05ParameterAno) ann0501[i][j];
        System.out.println("deep05注解,code=" + deep05.code() + ",value=" + deep05.value());
    }
}

7.注解作用在方法的返回参数上

注解作用在方法返回参数上,可以根据注解信息规范返回值格式等操作。

7.1定义注解

//作用在返回值上的注解
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Deep06ReturnAno {
    String code();
    String value() default "";
}

7.2使用注解

public class DeepWorker {
    @Deep03MethodAno(code = "deep03", value = "海沧")
    public @Deep06ReturnAno(code = "deep06", value = "海沧地铁2号线")
    String getDistrictInfo(@Deep05ParameterAno(code = "deep05", value = "地铁") String code) {
        return "接收信息: " + code;
    }
}

7.3获取和解析注解

Class<?> clz06 = DeepWorker.class;
Method method03 = clz06.getDeclaredMethod("getDistrictInfo", String.class);
//1.从方法上获取返回值注解常用方式
Annotation ann0601 = method.getDeclaredAnnotation(Deep06ReturnAno.class);
Annotation[] ann0602 = method.getDeclaredAnnotations();
Annotation ann0603 = method.getAnnotation(Deep06ReturnAno.class);
Annotation[] ann0604 = method.getAnnotations();
//2.解析注解值
Deep06ReturnAno deep06 = (Deep06ReturnAno) ann0601;
System.out.println("deep06注解,code=" + deep06.code() + ",value=" + deep06.value());

8.注解作用在多个场景上

一个注解根据ElementType配置,可以作用在不同场景上。

8.1定义注解

//作用在多中场合的注解
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Deep07MultipleAno {
    String code();
    String value() default "";
}

8.2使用注解

@Deep07MultipleAno(code = "deep07-01", value = "福建")
public class DeepWorker02 {

    @Deep07MultipleAno(code = "deep07-02", value = "厦门")
    public String getCity01(String code) {
        return "厦门";
    }
    public @Deep07MultipleAno(code = "deep07-03", value = "海沧") String getCity02() {
        return "海沧";
    }
}

8.3获取和解析注解

Class<?> clz07 = DeepWorker02.class;
//1.注解作用在类上
Annotation ann0701 = clz07.getDeclaredAnnotation(Deep07MultipleAno.class);
//2.注解作用在方法上
Method method04 = clz07.getDeclaredMethod("getCity01", String.class);
Annotation ann0702 = method04.getDeclaredAnnotation(Deep07MultipleAno.class);
//3.注解作用在方法的返回值上
Method method05 = clz07.getDeclaredMethod("getCity02");
Annotation ann0703 = method05.getDeclaredAnnotation(Deep07MultipleAno.class);
Deep07MultipleAno deep0701 = (Deep07MultipleAno) ann0701;
Deep07MultipleAno deep0702 = (Deep07MultipleAno) ann0702;
Deep07MultipleAno deep0703 = (Deep07MultipleAno) ann0703;
System.out.println("deep0701注解,code=" + deep0701.code() + ",value=" + deep0701.value());
System.out.println("deep0702注解,code=" + deep0702.code() + ",value=" + deep0702.value());
System.out.println("deep0703注解,code=" + deep0703.code() + ",value=" + deep0703.value());

以上,感谢。

2024年3月1日

  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
项目:使用 JavaScript 编写的杀死幽灵游戏(附源代码) 杀死鬼魂游戏是使用 Vanilla JavaScript、CSS 和 HTML 画布开发的简单项目。这款游戏很有趣。玩家必须触摸/杀死游荡的鬼魂才能得分。您必须将鼠标悬停在鬼魂上 - 尽量得分。鬼魂在眨眼间不断从一个地方移动到另一个地方。您必须在 1 分钟内尽可能多地杀死鬼魂。 游戏制作 这个游戏项目只是用 HTML 画布、CSS 和 JavaScript 编写的。说到这个游戏的特点,用户必须触摸/杀死游荡的幽灵才能得分。游戏会根据你杀死的幽灵数量来记录你的总分。你必须将鼠标悬停在幽灵上——尽量得分。你必须在 1 分钟内尽可能多地杀死幽灵。游戏还会显示最高排名分数,如果你成功击败它,该分数会在游戏结束屏幕上更新。 该游戏包含大量的 javascript 以确保游戏正常运行。 如何运行该项目? 要运行此游戏,您不需要任何类型的本地服务器,但需要浏览器。我们建议您使用现代浏览器,如 Google Chrome 和 Mozilla Firefox。要玩游戏,首先,单击 index.html 文件在浏览器中打开游戏。 演示: 该项目为国外大神项目,可以作为毕业设计的项目,也可以作为大作业项目,不用担心代码重复,设计重复等,如果需要对项目进行修改,需要具备一定基础知识。 注意:如果装有360等杀毒软件,可能会出现误报的情况,源码本身并无病毒,使用源码时可以关闭360,或者添加信任。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值