導航:首頁 > 編程語言 > java注釋作用

java注釋作用

發布時間:2025-01-30 17:11:38

A. java 自定義的註解有什麼作用

自定義註解,可以應用到反射中,比如自己寫個小框架。

如實現實體類某些屬性不自動賦值,或者驗證某個對象屬性完整性等

本人自己用過的驗證屬性值完整性:

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public@interfaceIgnoreProperty{
}
然後實體類中:
{

@IgnoreProperty
=1L;

@IgnoreProperty
privateIntegerresearchId;

@IgnoreProperty
privateTarUseruserId;

privateStringversion;

privateStringgrade;
....
}

然後action類中
//驗證數據完整性

Class<TarResearch>userClass=TarResearch.class;

Field[]field=userClass.getDeclaredFields();

for(inti=0;i<field.length;i++){

if(field[i].getAnnotation(IgnoreProperty.class)!=null){

continue;

}

Stringfie=field[i].getName().substring(0,1).toUpperCase()

+field[i].getName().substring(1);

Methodmethod=userClass.getMethod("get"+fie);

Objectobj=method.invoke(u);

if(obj==null){

sendResponseMsg(response,"數據錯誤");

returnnull;

}

}
閱讀全文

與java注釋作用相關的資料

熱點內容
程序員入職拼多多 瀏覽:119
pc如何訪問伺服器配置 瀏覽:76
pdfpower 瀏覽:42
linux系統運行級別 瀏覽:229
android下載文件demo 瀏覽:949
人體解剖圖pdf 瀏覽:823
php拋出異常處理 瀏覽:502
java字元串轉為對象 瀏覽:617
西貝爾壓縮機怎樣 瀏覽:616
phpzookeeper擴展 瀏覽:587
vue壓縮後的js可以反編嗎 瀏覽:854
javachar數組賦值 瀏覽:103
自動畫頸線指標源碼 瀏覽:72
訪問共享文件夾錯誤代碼 瀏覽:504
代號pdf 瀏覽:19
java編碼規范pdf 瀏覽:146
linuxshellread 瀏覽:226
st單片機命名 瀏覽:861
華為路由器彈出提示命令 瀏覽:887
伺服器地址失效 瀏覽:642