Java Annotation注解相關(guān)原理代碼總結(jié)
Java.lang 中自帶的注解
- @Override:表示當(dāng)前的方法定義將覆蓋基類的方法。如果你不小心拼寫錯(cuò)誤,或者方法簽名被錯(cuò)誤拼寫的時(shí)候,編譯器就會(huì)發(fā)出錯(cuò)誤提示。
- @Deprecated:如果使用該注解的元素被調(diào)用,編譯器就會(huì)發(fā)出警告信息。
- @SuppressWarnings:關(guān)閉不當(dāng)?shù)木幾g器警告信息。
- @SafeVarargs:在 Java 7 中加入用于禁止對(duì)具有泛型varargs參數(shù)的方法或構(gòu)造函數(shù)的調(diào)用方發(fā)出警告。
- @FunctionalInterface:Java 8 中加入用于表示類型聲明為函數(shù)式接口
如何定義注解
以下是一個(gè)為標(biāo)記注解(marker annotation), 不包含任何元素
package cn.haidnor.annotation; import java.lang.annotation.*; @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface Test { }
注解的定義也需要一些元注解(meta-annoation),比如 @Target 和 @Retention。
@Target 定義你的注解可以應(yīng)用在哪里(例如是方法還是字段)。
@Retention 定義了注解在哪里可用,在源代碼中(SOURCE),class文件(CLASS)中或者是在運(yùn)行時(shí)(RUNTIME)。
Demo 簡(jiǎn)單實(shí)例
定義注解
以下的代碼中。Target 定義只能在方法上使用,Retention 定義保留域
package cn.haidnor.annotation; import java.lang.annotation.*; @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface UseCase { int id(); String description() default "no description"; }
在類中使用注解
package cn.haidnor.clazz; package cn.haidnor.clazz; import cn.haidnor.annotation.UseCase; import java.util.List; public class PasswordUtils { @UseCase(id = 47, description ="Passwords must contain at least one numeric") public boolean validatePassword(String passwd) { return (passwd.matches("\\w*\\d\\w*")); } @UseCase(id = 48) public String encryptPassword(String passwd) { return new StringBuilder(passwd) .reverse().toString(); } @UseCase(id = 49, description = "New passwords can't equal previously used ones") public boolean checkForNewPassword( List<String> prevPasswords, String passwd) { return !prevPasswords.contains(passwd); } }
對(duì)以上 demo 中的代碼進(jìn)行測(cè)試
package cn.haidnor.test; import cn.haidnor.annotation.UseCase; import cn.haidnor.clazz.PasswordUtils; import java.util.*; import java.util.function.Consumer; import java.util.stream.*; import java.lang.reflect.*; public class UseCaseTracker { public static void main(String[] args) { List<Integer> useCases = IntStream.range(44, 51) .boxed().collect(Collectors.toList()); trackUseCases(useCases, PasswordUtils.class); } public static void trackUseCases(List<Integer> useCasesList, Class<?> clazz) { // getDeclaredMethods() 獲取所有公開的方法 for(Method m : clazz.getDeclaredMethods()) { // getAnnotation() 獲取指定注解 UseCase uc = m.getAnnotation(UseCase.class); if(uc != null) { System.out.print("Found Use Case "); // 提取注解元素值 System.out.println(uc.id()); // 提取注解元素值 System.out.println('\t' + uc.description()); useCasesList.remove( Integer.valueOf( uc.id() ) ); } } // 迭代集合 useCasesList.forEach(new Consumer<Integer>() { @Override public void accept(Integer integer) { System.out.println("Missing use case " + integer); } }); // 以上代碼可以使用箭頭行數(shù)簡(jiǎn)寫 // useCasesList.forEach(i -> System.out.println("Missing use case " + i)); } }
控制臺(tái)輸出結(jié)果
Found Use Case 47 Passwords must contain at least one numeric Found Use Case 48 no description Found Use Case 49 New passwords can't equal previously used ones Missing use case 44 Missing use case 45 Missing use case 46 Missing use case 50
元注解
Java 語(yǔ)言中目前有 5 種標(biāo)準(zhǔn)注解(前面介紹過(guò)),以及 5 種元注解。元注解用于注解其他的注解
注解中可以使用的元素
所有基本類型(int、float、boolean等)
- String
- Class
- enum
- Annotation
- 以上類型的數(shù)組
其他類型,編譯器就會(huì)報(bào)錯(cuò)。注意,也不允許使用任何包裝類型
- 注解的默認(rèn)值
無(wú)論是在源代碼聲明時(shí)還是在注解接口中定義默認(rèn)值時(shí),都不能使用 null 作為其值。
import java.lang.annotation.*; @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface SimulatingNull { int id() default -1; String description() default ""; }
使用反射獲取注解的方法流程圖
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
springboot的LogbackLoggingSystem配置加載流程解析
這篇文章主要介紹了springboot的LogbackLoggingSystem配置加載流程源碼分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11Mybatisplus集成springboot完成分頁(yè)查詢功能(示例代碼)
今天小編給大家分享Mybatisplus集成springboot完成分頁(yè)查詢功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧2023-11-11Java實(shí)現(xiàn)統(tǒng)計(jì)文檔中關(guān)鍵字出現(xiàn)的次數(shù)
這篇文章主要為大家分享了利用Java語(yǔ)言實(shí)現(xiàn)統(tǒng)計(jì)關(guān)鍵字在文檔中出現(xiàn)的次數(shù)的方法,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2022-05-05Java實(shí)現(xiàn)stream的三個(gè)常用方式(toMap,groupingBy,findFirst)
本文主要介紹了Java實(shí)現(xiàn)stream的三個(gè)常用方式,主要包括toMap,groupingBy,findFirst,具有一定的參考價(jià)值,感興趣的可以了解一下2023-10-10SpringBoot整合EasyExcel實(shí)現(xiàn)導(dǎo)入導(dǎo)出數(shù)據(jù)
這篇文章主要為大家詳細(xì)介紹了如何使用Vue、SpringBoot和EasyExcel實(shí)現(xiàn)導(dǎo)入導(dǎo)出數(shù)據(jù)功能,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2022-05-05SpringCloud中Gateway實(shí)現(xiàn)鑒權(quán)的方法
本文主要介紹了SpringCloud中Gateway實(shí)現(xiàn)鑒權(quán)的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11Java 生成隨機(jī)字符串?dāng)?shù)組的實(shí)例詳解
這篇文章主要介紹了Java 生成隨機(jī)字符串?dāng)?shù)組的實(shí)例詳解的相關(guān)資料,主要是利用Collections.sort()方法對(duì)泛型為String的List 進(jìn)行排序,需要的朋友可以參考下2017-08-08