自定義註解

2021-10-10 09:59:42 字數 2668 閱讀 5122

元註解:描述註解的註解

@target、@retention(保留策略:source-原始檔,class-class檔案,runtime-jvm載入class檔案後依舊存在)、@document、@inherited

自定義註解:除以上四個,其他都是自定義註解

使用:1·宣告註解

@retention

(retentionpolicy.runtime)

@target()

@documented

public @inte***ce

paramlog

2·利用aop使之生效

@aspect

@component

@profile()

/**不指定,任何環境下都能註冊這個元件

開發環境develop、測試環境test、生產環境master

資料來源:(/dev) (/test) (/master)

加了環境標識的bean,只有這個環境被啟用的時候才能註冊到容器中。預設是default環境

*/public

class

paramlogaspect

/** * 在切點之前織入

** @param joinpoint

* @throws throwable

*/@before

("paramlog()"

)public

void

dobefore

(joinpoint joinpoint)

throws throwable "

, request.

getrequesturl()

.tostring()

);// 列印描述資訊

logger.

info

("description : {}"

, methoddescription)

;// 列印 http method

);// 列印呼叫 controller 的全路徑以及執行方法

logger.

info

("class method : {}.{}"

, joinpoint.

getsignature()

.getdeclaringtypename()

, joinpoint.

getsignature()

.getname()

);// 列印請求的 ip

logger.

info

("ip : {}"

, request.

getremoteaddr()

);// 列印請求入參

logger.

info

("request args : {}"

,getparams

(joinpoint));

}/**

* 在切點之後織入

** @throws throwable

*/@after

("paramlog()"

)public

void

doafter()

throws throwable

/** * 環繞

** @param proceedingjoinpoint

* @return

* @throws throwable

*/@around

("paramlog()"

)public object doaround

(proceedingjoinpoint proceedingjoinpoint)

throws throwable "

, jsonobject.

tojsonstring

(result));

// 執行耗時

logger.

info

("time-consuming : {} ms"

, system.

currenttimemillis()

- starttime)

;return result;

}/**

* 獲取切面註解的描述

** @param joinpoint 切點

* @return 描述資訊

* @throws exception

*/public string getaspectlogdescription

(joinpoint joinpoint)

throws exception }}

return description.

tostring()

;}private string getparams

(joinpoint joinpoint)

params += jsonobject.

tojsonstring

(joinpoint.

getargs()

[i]);}

}return params;

}}

自定義註解

target elementtype.field retention retentionpolicy.runtime public inte ce setvalue以上就是乙個自定義的註解,下面來進行說明。target elementtype.field 表示支援該註解的程式元素,field就是屬性...

自定義註解

三個重要元註解 target 即註解的作用域,用於說明註解的使用範圍 即註解可以用在什麼地方,比如類的註解,方法註解,成員變數註解等等 elemenettype.constructor 構造器宣告 elemenettype.field 域宣告 包括 enum 例項 elemenettype.loca...

自定義註解

三個重要元註解 target 即註解的作用域,用於說明註解的使用範圍 即註解可以用在什麼地方,比如類的註解,方法註解,成員變數註解等等 elemenettype.constructor 構造器宣告 elemenettype.field 域宣告 包括 enum 例項 elemenettype.loca...