Java注解开发与应用案例
Java注解开发与应用案例
Annotation(注解)是JDK5.0及以后版本引入的,可以对包、类、属性、方法的描述,给被述对象打上标签,被打上标签后的类、属性、方法将被赋予特殊的“功能”;打个比喻,一个人如果学会飞,那这个人就是超人,所以如果给“人”这个对象打上了“会飞”的标签,那么这个人便成为“超人”。注解一般被框架解析和执行,新建立注解后,必须要有对应的框架解析和执行才有意义。下面举一个自定义注解例子:
自定义注解
它类似于新创建一个接口类文件,但为了区分,我们需要将它声明为@interface,下面,我们用代码来演示。
package com.annotation.test; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) public @interface FruitColor { enum Color{RED,YELLOW,WHITE} Color fruitColor() default Color.RED; }
package com.annotation.test; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) public @interface FruitName { String value() default ""; }
package com.annotation.test; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) public @interface FruitProvider { int id() default 0; String user() default ""; String address() default ""; }
以上是定義的蘋果的相關信息,下面,我們開始使用上面定義的註解來進行具體的實現:
package com.annotation.test; import com.annotation.test.FruitColor.Color; public class Apple { @FruitName(value="FuShi Apple") private String fruitName; @FruitColor(fruitColor=Color.RED) private String fruitColor; @FruitProvider(id=1,user="Tom",address="China") private FruitProvider provider; }
上面的Apple類是使用我們自己定義的註解來對類成員進行修飾。接下來,我們獲取我們的註釋信息:
1 package com.annotation.test; 2 3 import java.lang.reflect.Field; 4 5 public class Test { 6 7 8 public static void getFruitInfo(String clas){ 9 try { 10 Class<?> cls = Class.forName(clas); 11 Field[] fields = cls.getDeclaredFields(); 12 13 for (Field field : fields) { 14 if(field.isAnnotationPresent(FruitName.class)==true){ 15 FruitName name = field.getAnnotation(FruitName.class); 16 System.out.println("Fruit Name:"+name.value()); 17 } 18 if(field.isAnnotationPresent(FruitColor.class)){ 19 FruitColor color = field.getAnnotation(FruitColor.class); 20 System.out.println("Fruit Color:"+color.fruitColor()); 21 } 22 if(field.isAnnotationPresent(FruitProvider.class)){ 23 FruitProvider Provider = field.getAnnotation(FruitProvider.class); 24 System.out.println("Fruit FruitProvider: ProviderID:"+Provider.id()+" Provider:"+Provider.user() +" ProviderAddress:"+Provider.address()); 25 } 26 } 27 28 29 } catch (ClassNotFoundException e) { 30 e.printStackTrace(); 31 } 32 } 33 34 public static void main(String[] args) { 35 getFruitInfo("com.annotation.test.Apple"); 36 } 37 38 }
輸出結果爲:
Fruit Name:FuShi Apple Fruit Color:RED Fruit FruitProvider: ProviderID:1 Provider:Tom ProviderAddress:China
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】