摘要:知識(shí)點(diǎn)總結(jié)注解解析注解知識(shí)點(diǎn)總結(jié)注解通過反射獲取類函數(shù)或成員上的運(yùn)行時(shí)注解信息,從而實(shí)現(xiàn)動(dòng)態(tài)控制程序運(yùn)行的邏輯。
Java知識(shí)點(diǎn)總結(jié)(注解-解析注解)
@(Java知識(shí)點(diǎn)總結(jié))[Java, 注解]
通過反射獲取類、函數(shù)或成員上的運(yùn)行時(shí)注解信息,從而實(shí)現(xiàn)動(dòng)態(tài)控制程序運(yùn)行的邏輯。
使用注解步驟:
定義注解
類中使用注解
解析注解
示例:
import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target(value={ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) public @interface MethodAnnotation { String value(); }
import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target(value={ElementType.FIELD}) @Retention(RetentionPolicy.RUNTIME) public @interface FieldAnnotation { String columnName(); String type(); int length(); }
import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target(value={ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) public @interface TableAnnotation { String value(); }
@TableAnnotation("tb_student") public class Student { @FieldAnnotation(columnName="id",type="int",length=10) private int id; @FieldAnnotation(columnName="sname",type="varchar",length=10) private String name; @FieldAnnotation(columnName="age",type="int",length=3) private int age; @MethodAnnotation("get方法上的注解") public int getId() { return id; } @MethodAnnotation("set方法上的注解") public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
import java.lang.annotation.Annotation; import java.lang.reflect.Field; import java.lang.reflect.Method; public class Demo3 { // 使用反射獲取注解,生成類對(duì)應(yīng)的數(shù)據(jù)庫表 private static void test1(Class clazz) { // 獲得類所有的注解 Annotation[] annotations = clazz.getAnnotations(); for (Annotation a : annotations) { System.out.println(a); } // 通過注解的名字獲取注解 TableAnnotation t = (TableAnnotation) clazz.getAnnotation(TableAnnotation.class); System.out.println(t.value()); // 獲得屬性上的注解 try { Field f = clazz.getDeclaredField("age"); FieldAnnotation fa = f.getAnnotation(FieldAnnotation.class); System.out.println("字段名稱:" + fa.columnName() + ",字段類型:" + fa.type() + ",字段長(zhǎng)度:" + fa.length()); } catch (NoSuchFieldException | SecurityException e) { e.printStackTrace(); } // 根據(jù)獲得的表名、字段信息,拼出DDL語句,然后使用JDBC執(zhí)行這個(gè)SQL語句,在數(shù)據(jù)庫中生成相關(guān)的表 } //獲取方法上的注解 private static void test2(Class clazz) { // 獲取所有方法上的注解 Method[] ms = clazz.getMethods(); for (Method m : ms) { boolean isExist = m.isAnnotationPresent(MethodAnnotation.class); if (isExist) { MethodAnnotation ma = m.getAnnotation(MethodAnnotation.class); System.out.println(ma); } } // 獲取指定方法上的注解 try { Method method = clazz.getMethod("getId", null); Annotation[] as = method.getAnnotations(); for (Annotation a : as) { if (a instanceof MethodAnnotation) { MethodAnnotation ma = (MethodAnnotation) a; System.out.println(ma); } } } catch (NoSuchMethodException | SecurityException e) { e.printStackTrace(); } } public static void main(String[] args) { Class clazz = null; try { clazz = Class.forName("com.gs.Student"); } catch (ClassNotFoundException e) { e.printStackTrace(); } test1(clazz); test2(clazz); } }
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://m.hztianpu.com/yun/71561.html
摘要:中的詳解必修個(gè)多線程問題總結(jié)個(gè)多線程問題總結(jié)有哪些源代碼看了后讓你收獲很多,代碼思維和能力有較大的提升有哪些源代碼看了后讓你收獲很多,代碼思維和能力有較大的提升開源的運(yùn)行原理從虛擬機(jī)工作流程看運(yùn)行原理。 自己實(shí)現(xiàn)集合框架 (三): 單鏈表的實(shí)現(xiàn) 自己實(shí)現(xiàn)集合框架 (三): 單鏈表的實(shí)現(xiàn) 基于 POI 封裝 ExcelUtil 精簡(jiǎn)的 Excel 導(dǎo)入導(dǎo)出 由于 poi 本身只是針對(duì)于 ...
摘要:注解提供了一種安全的類似注釋的機(jī)制,用來將任何的信息或元數(shù)據(jù)與程序元素類方法成員變量等進(jìn)行關(guān)聯(lián)。為程序的元素類方法成員變量加上更直觀更明了的說明,這些說明與程序的業(yè)務(wù)邏輯無關(guān),并且提供給指定的工具或框架使用。 什么是注解? Annotation 是 Java5 之后開始引入的新特性,中文為注解。注解提供了一種安全的類似注釋的機(jī)制,用來將任何的信息或元數(shù)據(jù)(metadata)與程序元素(...
摘要:多線程編程這篇文章分析了多線程的優(yōu)缺點(diǎn),如何創(chuàng)建多線程,分享了線程安全和線程通信線程池等等一些知識(shí)。 中間件技術(shù)入門教程 中間件技術(shù)入門教程,本博客介紹了 ESB、MQ、JMS 的一些知識(shí)... SpringBoot 多數(shù)據(jù)源 SpringBoot 使用主從數(shù)據(jù)源 簡(jiǎn)易的后臺(tái)管理權(quán)限設(shè)計(jì) 從零開始搭建自己權(quán)限管理框架 Docker 多步構(gòu)建更小的 Java 鏡像 Docker Jav...
摘要:我們定義注解元素時(shí),經(jīng)常使用空字符串作為默認(rèn)值。也經(jīng)常使用負(fù)數(shù)比如表示不存在的含義示例既可以修飾方法,也可以修飾類運(yùn)行時(shí)使用關(guān)鍵字定義注解成員以無參無異常方式聲明。方法的名稱就是參數(shù)的名稱可以使用為成員指定一個(gè)默認(rèn)值浙江大學(xué)清華大學(xué)張三 Java知識(shí)點(diǎn)總結(jié)(注解-自定義注解) @(Java知識(shí)點(diǎn)總結(jié))[Java, 注解] 使用@interface自定義注解時(shí),自動(dòng)繼承了java.lan...
摘要:知識(shí)點(diǎn)總結(jié)注解元注解知識(shí)點(diǎn)總結(jié)注解元注解的作用就是負(fù)責(zé)注解其他注解。 Java知識(shí)點(diǎn)總結(jié)(注解-元注解 ) @(Java知識(shí)點(diǎn)總結(jié))[Java, 注解] 元注解的作用就是負(fù)責(zé)注解其他注解。 Java5.0定義的元注解: @Target @Retention @Documented @Inherited @Target 用于描述注解的使用范圍 @Target(value=Elemen...
閱讀 2892·2023-04-26 02:00
閱讀 2850·2019-08-30 15:54
閱讀 968·2019-08-30 11:15
閱讀 1564·2019-08-29 15:31
閱讀 976·2019-08-29 14:12
閱讀 555·2019-08-29 13:08
閱讀 891·2019-08-27 10:51
閱讀 2769·2019-08-26 12:17