24.Java 新特性擴展(重復注解、類型注解)

一、重復注解

1、基本介紹
  • 自從 JDK 5 引入注解以來,注解的使用開始流行,在各個框架中被廣泛使用

  • 不過注解有一個很大的限制,在同一個地方不能多次使用同一個注解

  • JDK 8 引入了重復注解的概念

2、具體實現
(1)自定義注解
  • MyAnnotation 注解
package com.my.repeatannotation;import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;// 指定對應的容器
@Repeatable(MyAnnotations.class)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {String value();
}
  • MyAnnotations 注解(重復注解的容器)
package com.my.repeatannotation;import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotations {MyAnnotation[] value();
}
(2)實體類
  • MyUser 類
package com.my.repeatannotation;@MyAnnotation("a1")
@MyAnnotation("a2")
@MyAnnotation("a3")
public class MyUser {@MyAnnotation("b1")@MyAnnotation("b2")@MyAnnotation("b3")private String name;@MyAnnotation("c1")@MyAnnotation("c2")@MyAnnotation("c3")public void say() {};
}
(3)測試
  • RepeatAnnotationTest 類
package com.my.repeatannotation;import java.lang.reflect.Field;
import java.lang.reflect.Method;public class RepeatAnnotationTest {public static void main(String[] args) {Class<MyUser> myUserClass = MyUser.class;// 獲取類的重復注解MyAnnotation[] myAnnotations1 = myUserClass.getAnnotationsByType(MyAnnotation.class);for (MyAnnotation myAnnotation : myAnnotations1) {System.out.println(myAnnotation.value());}// 獲取屬性的重復注解Field[] declaredFields = myUserClass.getDeclaredFields();for (Field declaredField : declaredFields) {declaredField.setAccessible(true);MyAnnotation[] myAnnotations2 = declaredField.getAnnotationsByType(MyAnnotation.class);for (MyAnnotation myAnnotation : myAnnotations2) {System.out.println(myAnnotation.value());}}// 獲取方法的重復注解Method[] declaredMethods = myUserClass.getDeclaredMethods();for (Field declaredField : declaredFields) {MyAnnotation[] myAnnotations3 = declaredField.getAnnotationsByType(MyAnnotation.class);for (MyAnnotation myAnnotation : myAnnotations3) {System.out.println(myAnnotation.value());}}}
}

二、類型注解

1、基本介紹
  • JDK 8 中為 @Target 添加了兩種類型

    • ElementType.TYPE_PARAMETER:表示該注解可以寫在參數列表的數據類型或泛型的聲明語句中

    • ElementType.TYPE_USE:表示該注解可以寫在任何數據類型的聲明語句中

2、具體實現
(1)自定義注解
  • MyType 注解
package com.my.typeannotation;import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;@Target({ElementType.TYPE_USE})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyType {String value();
}
(2)實體類
  • MyStudent 類
package com.my.typeannotation;import java.time.Instant;public class MyStudent {private @MyType("hello1") String name;private @MyType("Hello2") Instant age;public @MyType("Hello3") String say() {return "Hello World";};public String eat(@MyType("Hello4") String food) {return "Hello World";}
}
(3)測試
  • TypeAnnotationTest 類
package com.my.typeannotation;import java.lang.reflect.AnnotatedType;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.TypeVariable;public class TypeAnnotationTest {public static void main(String[] args) {Class<MyStudent> myStudentClass = MyStudent.class;TypeVariable<Class<MyStudent>>[] typeParameters = myStudentClass.getTypeParameters();for (TypeVariable<Class<MyStudent>> typeParameter : typeParameters) {// 獲取類中泛型中的注解MyType myType = typeParameter.getAnnotation(MyType.class);System.out.println(myType.value());}Field[] declaredFields = myStudentClass.getDeclaredFields();for (Field declaredField : declaredFields) {// 獲取屬性的數據類型的注解AnnotatedType annotatedType = declaredField.getAnnotatedType();MyType myType = annotatedType.getAnnotation(MyType.class);System.out.println(myType.value());}Method[] declaredMethods = myStudentClass.getDeclaredMethods();for (Method declaredMethod : declaredMethods) {if (declaredMethod.getName().equals("say")) {// 獲取方法返回值的注解AnnotatedType annotatedReturnType = declaredMethod.getAnnotatedReturnType();MyType myType = annotatedReturnType.getAnnotation(MyType.class);System.out.println(myType.value());} else if (declaredMethod.getName().equals("eat")) {// 獲取方法參數列表的注解AnnotatedType[] annotatedParameterTypes = declaredMethod.getAnnotatedParameterTypes();MyType myType = annotatedParameterTypes[0].getAnnotation(MyType.class);System.out.println(myType.value());}}}
}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/web/64828.shtml
繁體地址,請注明出處:http://hk.pswp.cn/web/64828.shtml
英文地址,請注明出處:http://en.pswp.cn/web/64828.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

后端java開發路由接口并部署服務器(四)

一、安裝IntelliJ IDEA&#xff0c;安裝包下載 1、官網下載 2、網盤資源 安裝包下載完成后進行傻瓜式下一步安裝就可以了 打開IntelliJ IDEA&#xff0c;輸入網盤資源文件內容 三、漢化處理 插件搜索chinese&#xff0c;就會找到相應的插件安裝重啟軟件即可 四、新建后端j…

Vue.js 表單驗證實戰:一個簡單的登錄頁面

修改日期備注2025.1.2初版 一、前言 Vue.js 學習第一天——學會一個帶有簡單表單驗證的登錄頁面。通過這個項目&#xff0c;會對 Vue.js 的核心概念有了更深入的理解&#xff0c;加深掌握如何運用 Vue 的一些強大特性來實現動態交互和數據處理。 二、項目的基本結構 首先&a…

MySQL 鎖那些事

Q1 : MySQL有哪些鎖,功能是什么,如何項目中使用?Q2 : 行鎖是如何實現的?什么情況下會使用行鎖?Q3 : 四種事務隔離形式的行鎖有什么不一樣?讀未提交讀提交可重復讀串行 Q4 : MySQL 的讀寫都是怎樣加鎖的?Q5 : 需要注意什么? Q1 : MySQL有哪些鎖,功能是什么,如何項目中使用…

國產文本編輯器EverEdit - 批量轉碼轉換行符

1 批量轉碼&轉換行符 1.1 應用場景 如果用戶批量在Windows編輯文件&#xff0c;要上傳到異構系統&#xff0c;如&#xff1a;Linux&#xff0c;則需要批量轉換編碼和換行符&#xff0c;此時可以使用EverEdit的批量轉碼功能。 1.2 使用方法 選擇主菜單文檔 -> 批量轉碼…

Java實現下載excel模板,并實現自定義下拉框

GetMapping("excel/download")ApiOperation(value "模板下載")public void getUserRecordTemplate(HttpServletResponse response, HttpServletRequest request) throws IOException {OutputStream outputStream response.getOutputStream();InputStream…

成立一家無人機培訓機構需要哪些基礎配置

成立一家無人機培訓機構&#xff0c;需要一系列基礎配置來確保教學質量、學員安全以及機構的正常運營。以下是根據公開發布的信息整理出的關鍵基礎配置&#xff1a; 一、場地配置 1. 飛行場&#xff1a;提供一個安全、寬敞的室外飛行環境&#xff0c;面積最好大于三千平米&…

交換機性能詳解

1. 背板帶寬 只有模塊化交換機&#xff08;擁有可擴展插槽&#xff0c;可靈活改變端口數量&#xff09;才有這個概念&#xff0c;固定端換機是沒有這個概念的。并且固定端換機的背板容量和交換容量大小是相等的。 背板帶寬是交換機的總數據處理能力&#xff0c;由硬件架構設…

讀“將計算性能調高到極致的基點秘訣”的嘗試

看到一篇文章&#xff0c;說最近閱讀LAMMPS源碼&#xff0c;悟出了很多道理。在計算性能優化這塊&#xff0c;源代碼作者很多寫法我最初不以為意&#xff0c;后來發現是作者有意為之&#xff0c;就是為了把計算性能優化到極致。做計算仿真軟件&#xff0c;也特別需要注意這些吧…

Gitea代碼倉服務搭建

特點與優勢 輕量級:Gitea是一個輕量級的Git服務,提供了快速、穩定的代碼托管和協作開發環境。它資源占用低,適合在資源受限的環境中運行。易于安裝和部署:Gitea提供了簡單易用的安裝和部署方式,支持多種安裝方式,包括二進制文件、Docker容器等,并提供了詳細的文檔和配置…

leetcode hot 小偷

class Solution(object):def rob(self, nums):""":type nums: List[int]:rtype: int"""# 使用動態規劃&#xff0c;把之前的給保存起來ans[0,nums[-1]]for i in range(1,len(nums)):ans.append(max(ans[-1],ans[-2]nums[-1*i-1]))return ans[-1]…

端口被占用

端口8080被占用 哈哈哈&#xff0c;我是因為后端項目跑錯了&#xff0c;兩個項目后端名稱太像了&#xff1b; &#xff08;1&#xff09;netstat -aon | findstr 8080&#xff0c;找到占用8080端口的進程號&#xff0c;獲取對應的進程號pid&#xff1b; &#xff08;2&#…

文件本地和OSS上傳

這里寫目錄標題 前端傳出文件后端本地存儲阿里云OSS存儲上傳Demo實現上傳ConfigurationProperties 前端傳出文件 <!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><title>上傳文件</title> </head&g…

[人工智能] 結合最新技術:Transformer、CLIP與邊緣計算在提高人臉識別準確率中的應用

隨著人工智能的快速發展&#xff0c;特別是深度學習和自然語言處理領域的革命性技術&#xff0c;越來越多的前沿技術被應用于人臉識別中。Transformer架構、CLIP模型以及邊緣計算的結合&#xff0c;正成為提升人臉識別準確率和應用效能的關鍵技術路徑。特別是在多樣化場景下&am…

Python的*args和**kwargs

參考 總結&#xff1a; &#xff08;1&#xff09;*args用于在函數中處理傳遞的位置參數序列&#xff1b; &#xff08;2&#xff09;**kwargs則用于處理傳遞的關鍵字參數字典。 &#xff08;3&#xff09;示例&#xff1a; def complex_function(first, *args, **kwargs)…

Vue3 + ElementPlus動態合并數據相同的單元格(超級詳細版)

最近的新項目有個需求需要合并單元列表。ElementPlus 的 Table 提供了合并行或列的方法&#xff0c;可以參考一下https://element-plus.org/zh-CN/component/table.html 但項目中&#xff0c;后臺數據返回格式和指定合并是動態且沒有規律的&#xff0c;Element 的示例過于簡單&…

免費又開源:企業級物聯網平臺的新選擇 ThingsPanel

在開源領域&#xff0c;選擇合適的開源協議是開發者和企業能否充分利用平臺的關鍵。ThingsPanel&#xff0c;作為一個專注于物聯網的開源平臺&#xff0c;近日將協議從 AGPLv3 改為更開放的 Apache 2.0。這一改變對開發者和用戶意味著什么&#xff1f; 為什么協議要從 AGPLv3 轉…

C# 設計模式(結構型模式):代理模式

C# 設計模式&#xff08;結構型模式&#xff09;&#xff1a;代理模式 在軟件開發中&#xff0c;有時我們需要通過某種方式間接地訪問一個對象&#xff0c;這時就可以使用代理模式&#xff08;Proxy Pattern&#xff09;。代理模式通過引入一個代理對象來控制對目標對象的訪問…

關于AI面試系統2025年趨勢評估!

在快速發展的科技浪潮中&#xff0c;AI技術正以前所未有的速度滲透到各行各業。企業招聘領域&#xff0c;作為人才選拔的關鍵環節&#xff0c;也不例外地迎來了AI面試系統的廣泛應用和持續創新。2025年&#xff0c;AI面試系統不僅成為企業招聘的主流工具&#xff0c;更在智能化…

MySQL 01 02 章——數據庫概述與MySQL安裝篇

一、數據庫概述 &#xff08;1&#xff09;為什么要使用數據庫 數據庫可以實現持久化&#xff0c;什么是持久化&#xff1a;數據持久化意味著將內存中的數據保存到硬盤上加以“固化”持久化的主要作用是&#xff1a;將內存中的數據存儲在關系型數據庫中&#xff0c;當然也可以…

Linux 揮別 WinXP 時代協議,USB RNDIS 即將退場

IT之家 1 月 2 日消息&#xff0c;Linux 基金會研究員 Greg Kroah-Hartman 計劃在 Linux 內核中&#xff0c;徹底移除 USB RNDIS 協議驅動。 IT之家查詢公開資料&#xff0c;USB RNDIS 是一種遠程網絡驅動接口規范&#xff0c;將 USB 設備模擬成網卡&#xff0c;從而在計算機和…