【Java注解】jdk8中注解的新特新
创始人
2025-05-30 06:34:07

jdk8中注解的新特新:

1.可重复注解

2.类型注解

一:可重复注解

什么是重复注解?可重复写的注解。

例如:

注解:

import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.ElementType.LOCAL_VARIABLE;@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE})
public @interface MyAnnotation {String value() default "hello";
}

使用: 

@MyAnnotation(value = "hi")
@MyAnnotation(value = "abc")
class Person {
}

但是这样写还差点意思,我们还要做一些配置

在jdk8之前的写法

创建MyAnntation数组

public @interface MyAnnotations {MyAnnotation[] value();
}

 使用:

@MyAnnotations({@MyAnnotation(value = "hi"),@MyAnnotation(value = "abc")})
class Person {
}

jdk8以后

 在注解MyAnntation中引入一个注解@Repeatable():可重复的

 注解MyAnntation如下

import java.lang.annotation.*;import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.ElementType.LOCAL_VARIABLE;@Inherited
@Repeatable(MyAnnotations.class)
@Retention(RetentionPolicy.RUNTIME)
@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE})
public @interface MyAnnotation {String value() default "hello";
}

在MyAnnotations注解中的声明周期以及可修饰的结构的要与MyAnnotation注解中的保持一致

注解MyAnnotations:

import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.ElementType.LOCAL_VARIABLE;@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE})
public @interface MyAnnotations {MyAnnotation[] value();
}

使用:

@MyAnnotation(value = "hi")
@MyAnnotation(value = "abc")
class Person {}

 二:类型注解

在Java 8之前:注解只能是在声明的地方所使用,

Java8开始,注解可以应用 在任何地方。

ElementType.TYPE_PARAMETER 表示该注解能写在类型变量的声明语句中(如:泛型声明)。

ElementType.TYPE_USE 表示该注解能写在使用类型的任何语句中。

 1.ElementType.TYPE_PARAMETER 使用:

class Generic<@MyAnnotation T>{}

但是这个时候会报错,我们在MyAnnotation中可使用的类型中加入TYPE_PARAMETER

注解MyAnnotation: 

import java.lang.annotation.*;import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.ElementType.LOCAL_VARIABLE;@Inherited
@Repeatable(MyAnnotations.class)
@Retention(RetentionPolicy.RUNTIME)
@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE,TYPE_PARAMETER})
public @interface MyAnnotation {String value() default "hello";
}

总结: ① 在MyAnnotation上声明@Repeatable成员值为MyAnnotations.class

            ② MyAnnotation的Target和Retention等元注解与MyAnnotations相同 

 2.ElementType.TYPE_USE

同上方法同理在MyAnnotation中可使用的类型中加入ElementType.TYPE_USE

import java.lang.annotation.*;import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.ElementType.LOCAL_VARIABLE;@Inherited
@Repeatable(MyAnnotations.class)
@Retention(RetentionPolicy.RUNTIME)
@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE,TYPE_PARAMETER,TYPE_USE})
public @interface MyAnnotation {String value() default "hello";
}

 使用:

class Generic<@MyAnnotation T>{public void show() throws @MyAnnotation RuntimeException{ArrayList<@MyAnnotation String> list = new ArrayList<>();int num = (@MyAnnotation int) 10L;}}

感谢观看!!!

相关内容

热门资讯

一顿特别的火锅:奉节积分银行“... 自2025年起,奉节县启动新时代文明实践“积分银行”2.0版升级,以“正向加分、负向扣分”为核心,重...
原创 关... “100%关税直接砍到6.1%,每年放行4.9万辆中国电动车”——2026年1月16日,加拿大总理卡...
华电新能以精品筑根基 央企担当... 来源:金桔网 开年以来,华电新能(600930.SH)作为中国华电以风力发电、太阳能发电为主的新能源...
原创 欧... 别急着高喊胜利,欧洲自己先打起了退堂鼓。——过去这一周,巴黎、罗马、柏林的朋友圈里流传着这样的悄悄话...
数千人在哥本哈根游行抗议美国图...   新华社哥本哈根1月17日电(记者张玉亮 李函林)数千名丹麦人17日在首都哥本哈根举行游行示威,抗...