Java中如何自定義註解

2021-09-23 13:39:30 字數 693 閱讀 7576

1.註解可修飾類、方法、引數,通常使用得最多的就是對方法的修飾的註解,所以這裡就只講解修飾方法的註解,其他的定義方法也類似。

2.定義註解類myannotation,註解就相當於乙個特殊的介面。

@target(elementtype.method)

@retention(retentionpolicy.runtime)

public @inte***ce myannotation

3. 定義註解介面的實現類,值得注意的是,在這裡我們並不需要實現myannotation介面,而是實現methodbeforeadvice,methodafterreturnningadvice.

methodbeforeadvice : 方法執行前呼叫

methodafterreturningadvice:方法執行結束後呼叫

public class myannotionimpl implements methodbeforeadvice,afterreturningadvice

@override

public void afterreturning(object returnvalue,method method,object objects,object object)

}

小結:在實現類中,也可以按照實際需求只實現其中的某乙個介面。

JAVA自定義註解

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

如何自定義註解

註解即注釋,也叫元資料。一種 級別的說明。它是jdk1.5及以後版本引入的乙個特性,與類 介面 列舉是在同乙個層次。它可以宣告在包 類 字段 方法 區域性變數 方法引數等的前面,用來對這些元素進行說明,注釋。元註解即註解的註解,是放在被定義的乙個註解類的前面 是對註解一種限制。這裡說說以下兩個元註解...

Java 自定義註解Annotation

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