淺談Java自定義注解相關(guān)知識(shí)
一、自定義注解格式
分析 Java 中自帶的 @Override 注解 , 源碼如下 :
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.SOURCE)
public @interface Override {
}
注解分為兩部分 :
① 元注解 ;
② public @interface 注解名稱 ;
二、注解本質(zhì)分析
按照 public @interface 注解名稱 格式 , 寫出一個(gè)注解 , 編譯該注解代碼生成 Annotation.class 字節(jié)碼文件 ;
public @interface Annotation {
}
使用 javap 命令反編譯Annotation.class 字節(jié)碼文件 , 查看該注解的實(shí)際代碼 ;
反編譯命令如下 :
javap Annotation.class
輸出內(nèi)容 :
D:\002_Project\004_Java_Learn\Annotation\out\production\Annotation>javap Annotation.class
Compiled from "Annotation.java"
public interface Annotation extends java.lang.annotation.Annotation {
}

注解的本質(zhì)是一個(gè) interface 接口 , 注解接口默認(rèn)繼承了 java.lang.annotation.Annotation 接口 ;
public interface Annotation extends java.lang.annotation.Annotation {
}
三、注解屬性及類型
注解的本質(zhì)是接口 , 接口中可以定義 常量 和 方法 ;
在注解中定義 接口方法 , 就是 注解的屬性 ;
為注解添加屬性 : 接口中的方法都是抽象方法 , 其中 public abstract 可以省略 ;
public @interface Annotation {
public abstract String path();
}
注解屬性使用格式 :
@注解名稱(屬性名稱 = 屬性值)
注解屬性使用 : 在相關(guān)的代碼上使用
@Annotation(path = "")
Student(String name, int age){
}
四、注解屬性類型
注解屬性 ( 接口方法 ) 返回值類型要求 :
① 基本數(shù)據(jù)類型 : byte , short , int , long , float , double , char , boolean ;
② 字符串類型 : String ;
③ 枚舉類型 : enum ;
④ 注解類型 ;
⑤ 以上類型的數(shù)組形式 ;
注解屬性返回值必須是以上的類型 , 不能設(shè)置其它類型返回值 , 否則會(huì)報(bào)錯(cuò) ;
注解中定義了屬性 , 在使用注解時(shí) , 需要 給 注解屬性 賦值 ;
定義 注解屬性 時(shí) , 可以 使用 default 關(guān)鍵字 指定屬性默認(rèn)值 , 下面代碼中 , 制定 注解屬性 intValue 值類型為 int 整型 , 默認(rèn)值 88 ;
int intValue() default 88;
如果 注解屬性 指定了默認(rèn)值 , 在使用注解時(shí) , 可以選擇 不為該屬性賦值 ( 此時(shí)使用默認(rèn)屬性值 ) , 也可以進(jìn)行賦值 ( 指定一個(gè)新的屬性值 ) ;
如果 注解屬性 沒有指定默認(rèn)值 , 則使用 注解 時(shí) , 必須為其指定一個(gè)默認(rèn)值 , 否則編譯時(shí)報(bào)錯(cuò) ;
數(shù)組類型 的 注解屬性 賦值 時(shí) , 使用大括號(hào)進(jìn)行賦值 , 大括號(hào)內(nèi)是數(shù)組元素 , 如果只有一個(gè)屬性 , 可以省略大括號(hào) ,
注解 聲明示例 :
public @interface Annotation {
/**
* 字符串類型
* @return
*/
String stringValue();
/**
* int 基本類型
* @return
*/
int intValue() default 88;
/**
* 枚舉類型
* @return
*/
Number enumValue();
/**
* 注解類型
* @return
*/
Annotation2 annotationValue();
/**
* 字符串?dāng)?shù)組類型
* @return
*/
String[] stringArrayValue();
}
枚舉類 :
public enum Number {
ONE, TWO, THREE
}
Annotation2 注解類 :
public @interface Annotation2 {
}
注解使用示例 :
/**
* 注解生成文檔
*
* @author hsl
* @version 0.1
* @since 1.5
*/
public class Student {
/**
* 構(gòu)造函數(shù)
* @param name 參數(shù)一
* @param age 參數(shù)二
*/
@Annotation(
stringValue = "tom",
enumValue = Number.ONE,
annotationValue = @Annotation2,
stringArrayValue = {"tom", "jerry"})
Student(String name, int age){
}
@SuppressWarnings("all")
@Override
public String toString() {
return super.toString();
}
}
代碼分析 : 重點(diǎn)關(guān)注注解的使用 , 使用注解時(shí) , 需要給 沒有默認(rèn)值 的 注解屬性 賦值 , 格式為 注解屬性名稱 = 對(duì)應(yīng)類型屬性值 , 如果 注解屬性 有默認(rèn)值 , 則
@Annotation(stringValue = "tom", enumValue = Number.ONE, stringArrayValue = {"tom", "jerry"})
五、注解屬性賦值簡(jiǎn)化操作
如果 注解屬性 名稱是 value , 并且 注解中只有 1 1 1 個(gè)屬性 , 那么在使用 注解 為 注解屬性 賦值時(shí) , 可以省略注解名稱 , 直接傳入 注解屬性值 ;
示例 : JDK 自帶的 SuppressWarnings 注解 ,
@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE})
@Retention(RetentionPolicy.SOURCE)
public @interface SuppressWarnings {
String[] value();
}
注解使用 : 使用 SuppressWarnings 注解時(shí) , 直接傳入 “all” 參數(shù) , 省略了注解屬性名稱 ;
@SuppressWarnings("all")
@Override
public String toString() {
return super.toString();
}
滿足兩個(gè)條件 , 才能使用上述簡(jiǎn)化方式 ;
到此這篇關(guān)于淺談Java自定義注解相關(guān)知識(shí)的文章就介紹到這了,更多相關(guān)Java自定義注解內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解析SpringSecurity+JWT認(rèn)證流程實(shí)現(xiàn)
這篇文章主要介紹了解析SpringSecurity+JWT認(rèn)證流程實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
Springboot中攔截GET請(qǐng)求獲取請(qǐng)求參數(shù)驗(yàn)證合法性核心方法
這篇文章主要介紹了Springboot中攔截GET請(qǐng)求獲取請(qǐng)求參數(shù)驗(yàn)證合法性,在Springboot中創(chuàng)建攔截器攔截所有GET類型請(qǐng)求,獲取請(qǐng)求參數(shù)驗(yàn)證內(nèi)容合法性防止SQL注入,這種方法適用攔截get類型請(qǐng)求,需要的朋友可以參考下2023-08-08
springboot?ElasticSearch如何配置自定義轉(zhuǎn)換器ElasticsearchCustomConver
這篇文章主要介紹了springboot?ElasticSearch如何配置自定義轉(zhuǎn)換器ElasticsearchCustomConversions問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08
Java利用TCP協(xié)議實(shí)現(xiàn)客戶端與服務(wù)器通信(附通信源碼)
這篇文章主要介紹了Java利用TCP協(xié)議實(shí)現(xiàn)客戶端與服務(wù)器通信(附通信源碼),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
JAVA 自定義線程池的最大線程數(shù)設(shè)置方法
這篇文章主要介紹了JAVA 自定義線程池的最大線程數(shù)設(shè)置方法,文中示例代碼非常詳細(xì),供大家參考和學(xué)習(xí),感興趣的朋友可以了解下2020-06-06
淺談java.util.concurrent包中的線程池和消息隊(duì)列
這篇文章主要介紹了淺談java.util.concurrent包中的線程池和消息隊(duì)列,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08
Mybatis 動(dòng)態(tài)sql if 判讀條件等于一個(gè)數(shù)字的案例
這篇文章主要介紹了Mybatis 動(dòng)態(tài)sql if 判讀條件等于一個(gè)數(shù)字的案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-11-11

