Java中的@Repeatable注解的作用詳解
1. 前言
最近無意看到某些注解上有@Repeatable,出于比較好奇,因此稍微研究并寫下此文章。
@Repeatable注解是用來標(biāo)注一個(gè)注解在同一個(gè)地方可重復(fù)使用的一個(gè)注解,比如說你定義了一個(gè)注解,如果你的注解沒有標(biāo)記@Repeatable這個(gè)JDK自帶的注解,那么你這個(gè)注解在引用的地方就只能使用一次。
2. 先說結(jié)論
@Repeatable的作用:使被他注釋的注解可以在同一個(gè)地方重復(fù)使用。
具體使用如下:
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Repeatable(value = MyAnnotationList.class)
public @interface MyAnnotation {
String name();
}
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotationList {
// 被 @Repeatable引用的注解中,必須得有被 @Repeatable注釋的注解(@MyAnnotation)返回類型的value方法
MyAnnotation[] value();
}
public class MyAnnotationTest {
@MyAnnotation(name = "Test1")
@MyAnnotation(name = "Test2")
private void testMethod() {
}
@MyAnnotationList(value = {@MyAnnotation(name = "Test1"), @MyAnnotation(name = "Test2")})
private void testMethod2() {
}
}3. 案例演示
先定義新注解
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
String name();
}創(chuàng)建新類并使用自定義注解
public class MyAnnotationTest {
@MyAnnotation(name = "Test1")
private void testMethod(){
}
}
當(dāng)注解@MyAnnotation還沒被@Repeatable注釋的時(shí)候,在testMethod()方法上使用多次,會(huì)出現(xiàn)下面報(bào)錯(cuò):

將會(huì)提示:@MyAnnotation沒被@Repeatable注解,無法重復(fù)使用@MyAnnotation
因此在@MyAnnotation上使用@MyAnnotation,如下:

4. 注意事項(xiàng)
@Repeatable(value = MyAnnotationList.class) 中引用了 @MyAnnotationList注解,用于@MyAnnotation注解上,有如下幾個(gè)細(xì)節(jié):
細(xì)節(jié)一:在引用注解上的@MyAnnotationList的方法中得有value()方法,如下:

細(xì)節(jié)二:在引用注解上的@MyAnnotationList的方法中得有【被@Repeatable注解的@MyAnnotation注解類型的數(shù)組返回值的value方法】

細(xì)節(jié)三:該案例中,若在方法上重復(fù)使用@MyAnnotation注解,實(shí)際上也會(huì)在運(yùn)行的時(shí)候被包裝成MyAnnotationList[] 里面,如下:

細(xì)節(jié)四:@MyAnnotation可多次使用,但不可多次與@MyAnnotationList一起使用,如下:

細(xì)節(jié)五:@MyAnnotation可多次使用,但僅可一個(gè)與@MyAnnotationList一起使用,但唯一的@MyAnnotation在運(yùn)行的時(shí)候被包裝成MyAnnotationList[] 里面,如下:

到此這篇關(guān)于Java中的@Repeatable注解的作用詳解的文章就介紹到這了,更多相關(guān)@Repeatable注解內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Springboot整合FreeMarker的實(shí)現(xiàn)示例
本文主要介紹了Springboot整合FreeMarker的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02
數(shù)組重排序(如何將所有奇數(shù)都放在所有偶數(shù)前面)的深入分析
本篇文章是對(duì)數(shù)組重排序(如何將所有奇數(shù)都放在所有偶數(shù)前面)的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
springboot 自定義異常并捕獲異常返給前端的實(shí)現(xiàn)代碼
在開發(fā)中,如果用try catch的方式,每個(gè)方法都需要單獨(dú)實(shí)現(xiàn),為了方便分類異常,返回給前端,采用了@ControllerAdvice注解和繼承了RuntimeException的方式來實(shí)現(xiàn),具體實(shí)現(xiàn)內(nèi)容跟隨小編一起看看吧2021-11-11
Spring?Boot集成LiteFlow規(guī)則引擎的詳細(xì)過程
本文詳細(xì)介紹了如何在Spring?Boot應(yīng)用程序中集成LiteFlow規(guī)則引擎,并探討如何使用LiteFlow庫來實(shí)現(xiàn)業(yè)務(wù)流程的規(guī)則處理,將通過具體的示例來展示如何在Spring?Boot應(yīng)用程序中配置和使用LiteFlow規(guī)則引擎,以提高系統(tǒng)的靈活性和可維護(hù)性,感興趣的朋友跟隨小編一起看看吧2024-07-07
如何在SpringBoot項(xiàng)目中使用Oracle11g數(shù)據(jù)庫
這篇文章主要介紹了在SpringBoot項(xiàng)目中使用Oracle11g數(shù)據(jù)庫的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06
SpringBoot集成MongoDB的實(shí)現(xiàn)
本文主要介紹了SpringBoot集成MongoDB的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-01-01

