java自定義註解的使用 例

2021-09-23 14:19:29 字數 899 閱讀 4231

背景:產品同類的訂單會有不同的細節操作(增、改等),但是主流程確是一樣的(校驗、返回結果等);不同類產品採用類似動態**的模式,利用反射機制,找到對應的邏輯實體執行對應的方法。現在處理的是細化到業務實體的公共主流程中不同操作的不同方法。

clazz= policyconst.get(requesttype);

if(clazz == null) return null;

標準入口就不詳述了。

自定義註解:

@retention(runtime) 

@target()

public @inte***ce requesttype

這裡實現的作用是通過requesttype型別來控制執行的方法;

呼叫方式:

//繼承抽象類的方式

super.findmethod(this, head.getrequesttype(), execute).invoke(this, jsonrequest);

方法定義:(通過特殊字元+request型別==>確定呼叫的方法)

@requesttype(execute+policyconst.requesttype)

public queryresultobject executefun_001(jsonrequest jsonrequest) {}

核心方法:

public static method findmethod(object bean, string requesttype, string method_suffix) 

}if(beans.isempty(method))

return method;

}

JAVA自定義註解

target elementtype.method retention retentionpolicy.runtime documented public inte ce ignoreauth retention retentionpolicy.source 註解僅存在於原始碼中,在class位元組...

Java 自定義註解Annotation

target elementtype.method retention retentionpolicy.runtime public inte ce operationtype這是乙個比較簡單的自定義註解,target 說明的是註解的作用範圍 1.constructor 用於描述構造器 2.fiel...

自定義註解的使用

今日份小計 關於註解的使用當然是老生常談的話題了,關於元註解以及標註註解的部落格網上很多,自定義註解的使用大致分為三個步驟 1 首先自定義乙個註解 documented retention retentionpolicy.runtime target elementtype.method publi...