自定義註解並通過反射獲取該註解的資訊

2021-10-04 21:38:37 字數 1406 閱讀 3984

網上部落格千百篇·····

@target(

)@retention(retentionpolicy.runtime)

public @inte***ce enableexport

@target 該註解可以被貼在**(字段、方法、類等)常量elementtype.type表示可以被貼在類上@retention 該註解的生命週期enableexport 註解名稱string value() 表示該註解有乙個值value,預設值為「***」

使用

本文用到的反射方法

//獲取class檔案

class claz=user.class;

class claz=new user(

).getclass();

//獲取類中所有的字段資訊

field[

] fields=claz.getdeclaredfields();

//獲取某個欄位的值

object obj=fields[0].get(user)

;//獲取字段值一定要有物件

//設定字段可以操作,如果欄位為private,則不能進行獲取值的操作

fields[0].setaccessible(true)

;//判斷該欄位是否貼了某個註解

boolean b=fields[0].isannotationpresent(註解的位元組碼)

;

上**:

public void makeworkbook(map model, workbook workbook)

else

sheet sheet = workbook.createsheet(sheetname)

; row title = sheet.createrow(0)

; field[

] fields = claz.getdeclaredfields();

//建立第一行:標題行

for(int i = 0, j = 0; i < fields.length; i++)

} //建立資料行

for(int i = 0; i < pojolist.size(

); i++)

catch (exception e)}}

}

本文可以結合我的另一篇部落格看springmvc匯出excel

自定義註解 反射

需求 在做許可權平台的時候,需要寫乙個查詢審計記錄列表的介面。這個需求看起來非常簡單,就是從資料庫單錶裡面查出資料,返回json格式。然而,資料庫裡有兩列,修改前和修改後的json。需要翻譯成字串整個返回。資料庫的source欄位的json長這樣 status used end date is de...

java反射,通過自定義註解獲取get方法

1.新建乙個類配置自定義註解 target設定註解可用範圍 retention設定什麼使用啟用註解 target retention retentionpolicy.runtime public inte ce config2.新建乙個資料實體並給所需要的方法設註解 public class stu...

自定義註解和反射例項

自定義註解和反射例項 author 這個例子是說實體和資料庫對應表的對映,可以在實體上面使用註解。public class tannotationclass 下面是備註接的類 class person 下面是自定義註解 target elementtype.field 註解是給屬性使用的 reten...