注解是什么?
①、引用自維基百科的內容:
java注解又稱java標注,是jdk5.0版本開始支持加入源代碼的特殊語法 元數據 。
java語言中的類、方法、變量、參數和包等都可以被標注。和javadoc不同,java標注可以通過反射獲取標注內容。在編譯器生成類文件時,標注可以被嵌入到字節碼中。java虛擬機可以保留標注內容,在運行時可以獲取到標注內容。 當然它也支持自定義java標注。
②、引用自網絡的內容:
java 注解是在 jdk5 時引入的新特性,注解(也被稱為 元數據 )為我們在代碼中添加信息提供了一種形式化的方法,使我們可以在稍后某個時刻非常方便地使用這些數據。
元注解是什么?
元注解 的作用就是負責注解其他注解。java5.0定義了4個標準的meta-annotation(元注解)類型,它們被用來提供對其它 annotation類型作說明。
標準的元注解:
@target
@retention
@documented
@inherited
在詳細說這四個元數據的含義之前,先來看一個在工作中會經常使用到的 @autowired 注解,進入這個注解里面瞧瞧: 此注解中使用到了@target、@retention、@documented 這三個元注解 。
1
2
3
4
5
6
|
@target ({elementtype.constructor, elementtype.method, elementtype.parameter, elementtype.field, elementtype.annotation_type}) @retention (retentionpolicy.runtime) @documented public @interface autowired { boolean required() default true ; } |
@target元注解:
@target注解,是專門用來限定某個自定義注解能夠被應用在哪些java元素上面的,標明作用范圍;取值在java.lang.annotation.elementtype 進行定義的。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
public enum elementtype { /** 類,接口(包括注解類型)或枚舉的聲明 */ type, /** 屬性的聲明 */ field, /** 方法的聲明 */ method, /** 方法形式參數聲明 */ parameter, /** 構造方法的聲明 */ constructor, /** 局部變量聲明 */ local_variable, /** 注解類型聲明 */ annotation_type, /** 包的聲明 */ package } |
根據此處可以知道 @autowired 注解的作用范圍:
1
2
|
// 可以作用在 構造方法、方法、方法形參、屬性、注解類型 上 @target ({elementtype.constructor, elementtype.method, elementtype.parameter, elementtype.field, elementtype.annotation_type}) |
@retention元注解:
@retention注解,翻譯為持久力、保持力。即用來修飾自定義注解的生命周期。
注解的生命周期有三個階段:
- java源文件階段;
- 編譯到class文件階段;
- 運行期階段;
同樣使用了retentionpolicy 枚舉類型對這三個階段進行了定義:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
public enum retentionpolicy { /** * annotations are to be discarded by the compiler. * (注解將被編譯器忽略掉) */ source, /** * annotations are to be recorded in the class file by the compiler * but need not be retained by the vm at run time. this is the default * behavior. * (注解將被編譯器記錄在class文件中,但在運行時不會被虛擬機保留,這是一個默認的行為) */ class , /** * annotations are to be recorded in the class file by the compiler and * retained by the vm at run time, so they may be read reflectively. * (注解將被編譯器記錄在class文件中,而且在運行時會被虛擬機保留,因此它們能通過反射被讀取到) * @see java.lang.reflect.annotatedelement */ runtime } |
再詳細描述下這三個階段:
①、如果被定義為 retentionpolicy.source,則它將被限定在java源文件中,那么這個注解即不會參與編譯也不會在運行期起任何作用,這個注解就和一個注釋是一樣的效果,只能被閱讀java文件的人看到;
②、如果被定義為 retentionpolicy.class,則它將被編譯到class文件中,那么編譯器可以在編譯時根據注解做一些處理動作,但是運行時jvm(java虛擬機)會忽略它,并且在運行期也不能讀取到;
③、如果被定義為 retentionpolicy.runtime,那么這個注解可以在運行期的加載階段被加載到class對象中。那么在程序運行階段,可以通過反射得到這個注解,并通過判斷是否有這個注解或這個注解中屬性的值,從而執行不同的程序代碼段。
注意:實際開發中的自定義注解幾乎都是使用的 retentionpolicy.runtime 。
@documented元注解:
@documented注解,是被用來指定自定義注解是否能隨著被定義的java文件生成到javadoc文檔當中。
@inherited元注解:
@inherited注解,是指定某個自定義注解如果寫在了父類的聲明部分,那么子類的聲明部分也能自動擁有該注解。
@inherited注解只對那些@target被定義為 elementtype.type 的自定義注解起作用。
自定義注解實現:
在了解了上面的內容后,我們來嘗試實現一個自定義注解:
根據上面自定義注解中使用到的元注解得知:
①、此注解的作用范圍,可以使用在類(接口、枚舉)、方法上;
②、此注解的生命周期,被編譯器保存在class文件中,而且在運行時會被jvm保留,可以通過反射讀取;
自定義注解的簡單使用:
上面已經創建了一個自定義的注解,那該怎么使用呢?下面首先描述下它簡單的用法,后面將會使用其結合攔截器和aop切面編程進行實戰應用;
應用場景實現
在了解了上面注解的知識后,我們乘勝追擊,看看它的實際應用場景是腫么樣的,以此加深下我們的理解;
實現的 demo 項目是以 springboot 實現的,項目工程結構圖如下:
場景一:自定義注解 + 攔截器 = 實現接口響應的包裝
使用自定義注解 結合 攔截器 優雅的實現對api接口響應的包裝。
在介紹自定義實現的方式之前,先簡單介紹下普遍的實現方式,通過兩者的對比,才能更加明顯的發現誰最優雅。
普通的接口響應包裝方式:
現在項目絕大部分都采用的前后端分離方式,所以需要前端和后端通過接口進行交互;目前在接口交互中使用最多的數據格式是 json,然后后端返回給前端的最為常見的響應格式如下:
1
2
3
4
5
6
7
8
|
{ #返回狀態碼 code:integer, #返回信息描述 message:string, #返回數據值 data:object } |
項目中經常使用枚舉類定義狀態碼和消息,代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
/** * @author 【 木子雷 】 公眾號 * @title: responsecode * @description: 使用枚舉類封裝好的響應狀態碼及對應的響應消息 * @date: 2019年8月23日 下午7:12:50 */ public enum responsecode { success( 1200 , "請求成功" ), error( 1400 , "請求失敗" ); private integer code; private string message; private responsecode(integer code, string message) { this .code = code; this .message = message; } public integer code() { return this .code; } public string message() { return this .message; } } |
同時項目中也會設計一個返回響應包裝類,代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
import com.alibaba.fastjson.jsonobject; import java.io.serializable; /** * @author 【 木子雷 】 公眾號 * @title: response * @description: 封裝的統一的響應返回類 * @date: 2019年8月23日 下午7:07:13 */ @suppresswarnings ( "serial" ) public class response<t> implements serializable { /** * 響應數據 */ private t date; /** * 響應狀態碼 */ private integer code; /** * 響應描述信息 */ private string message; public response(t date, integer code, string message) { super (); this .date = date; this .code = code; this .message = message; } public t getdate() { return date; } public void setdate(t date) { this .date = date; } public integer getcode() { return code; } public void setcode(integer code) { this .code = code; } public string getmessage() { return message; } public void setmessage(string message) { this .message = message; } @override public string tostring() { return jsonobject.tojsonstring( this ); } } |
最后就是使用響應包裝類和狀態碼枚舉類 來實現返回響應的包裝了:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
@getmapping ( "/user/findalluser" ) public response<list<user>> findalluser() { logger.info( "開始查詢所有數據..." ); list<user> findalluser = new arraylist<>(); findalluser.add( new user( "木子雷" , 26 )); findalluser.add( new user( "公眾號" , 28 )); // 返回響應進行包裝 response response = new response(findalluser, responsecode.success.code(), responsecode.success.message()); logger.info( "response: {} \n" , response.tostring()); return response; } |
在瀏覽器中輸入網址: http://127.0.0.1:8080/v1/api/user/findalluser 然后點擊回車,得到如下數據:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
{ "code" : 1200 , "date" : [ { "age" : 26 , "name" : "木子雷" }, { "age" : 28 , "name" : "公眾號" } ], "message" : "請求成功" } |
通過看這中實現響應包裝的方式,我們能發現什么問題嗎?
答:代碼很冗余,需要在每個接口方法中都進行響應的包裝;使得接口方法包含了很多非業務邏輯代碼;
有沒有版本進行優化下呢? en en 思考中。。。。。 啊,自定義注解 + 攔截器可以實現呀!
自定義注解實現接口響應包裝:
①、首先創建一個進行響應包裝的自定義注解:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
/** * @author 【 木子雷 】 公眾號 * @package_name: com.lyl.annotation * @classname: responseresult * @description: 標記方法返回值需要進行包裝的 自定義注解 * @date: 2020-11-10 10:38 **/ @target ({elementtype.type, elementtype.method}) @retention (retentionpolicy.runtime) @documented public @interface responseresult { } |
②、創建一個攔截器,實現對請求的攔截,看看請求的方法或類上是否使用了自定義的注解:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
/** * @author 【 木子雷 】 公眾號 * @package_name: com.lyl.interceptor * @classname: responseresultinterceptor * @description: 攔截器:攔截請求,判斷請求的方法或類上是否使用了自定義的@responseresult注解, * 并在請求內設置是否使用了自定義注解的標志位屬性; * @date: 2020-11-10 10:50 **/ @component public class responseresultinterceptor implements handlerinterceptor { /** * 標記位,標記請求的controller類或方法上使用了到了自定義注解,返回數據需要被包裝 */ public static final string response_annotation = "response_annotation" ; /** * 請求預處理,判斷是否使用了自定義注解 */ @override public boolean prehandle(httpservletrequest request, httpservletresponse response, object handler) throws exception { // 請求的接口方法 if (handler instanceof handlermethod) { final handlermethod handlermethod = (handlermethod) handler; final class <?> clazz = handlermethod.getbeantype(); final method method = handlermethod.getmethod(); // 判斷是否在類對象上加了注解 if (clazz.isannotationpresent(responseresult. class )) { // 在請求中設置需要進行響應包裝的屬性標志,在下面的responsebodyadvice增強中進行處理 request.setattribute(response_annotation, clazz.getannotation(responseresult. class )); } else if (method.isannotationpresent(responseresult. class )) { // 在請求中設置需要進行響應包裝的屬性標志,在下面的responsebodyadvice增強中進行處理 request.setattribute(response_annotation, method.getannotation(responseresult. class )); } } return true ; } } |
③、創建一個增強controller,實現對返回響應進行包裝的增強處理:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
/** * @author 【 木子雷 】 公眾號 * @package_name: com.lyl.interceptor * @classname: responseresulthandler * @description: 對 返回響應 進行包裝 的增強處理 * @date: 2020-11-10 13:49 **/ @controlleradvice public class responseresulthandler implements responsebodyadvice<object> { private final logger logger = loggerfactory.getlogger( this .getclass()); /** * 標記位,標記請求的controller類或方法上使用了到了自定義注解,返回數據需要被包裝 */ public static final string response_annotation = "response_annotation" ; /** * 請求中是否包含了 響應需要被包裝的標記,如果沒有,則直接返回,不需要重寫返回體 * * @param methodparameter * @param aclass * @return */ @override public boolean supports(methodparameter methodparameter, class <? extends httpmessageconverter<?>> aclass) { servletrequestattributes ra = (servletrequestattributes) requestcontextholder.getrequestattributes(); httpservletrequest sr = (httpservletrequest) ra.getrequest(); // 查詢是否需要進行響應包裝的標志 responseresult responseresult = (responseresult) sr.getattribute(response_annotation); return responseresult == null ? false : true ; } /** * 對 響應體 進行包裝; 除此之外還可以對響應體進行統一的加密、簽名等 * * @param responsebody 請求的接口方法執行后得到返回值(返回響應) */ @override public object beforebodywrite(object responsebody, methodparameter methodparameter, mediatype mediatype, class <? extends httpmessageconverter<?>> aclass, serverhttprequest serverhttprequest, serverhttpresponse serverhttpresponse) { logger.info( "返回響應 包裝進行中。。。" ); response response; // boolean類型時判斷一些數據庫新增、更新、刪除的操作是否成功 if (responsebody instanceof boolean ) { if (( boolean ) responsebody) { response = new response(responsebody, responsecode.success.code(), responsecode.success.message()); } else { response = new response(responsebody, responsecode.error.code(), responsecode.error.message()); } } else { // 判斷像查詢一些返回數據的情況,查詢不到數據返回 null; if ( null != responsebody) { response = new response(responsebody, responsecode.success.code(), responsecode.success.message()); } else { response = new response(responsebody, responsecode.error.code(), responsecode.error.message()); } } return response; } } |
④、最后在 controller 中使用上我們的自定義注解;在 controller 類上或者 方法上使用@responseresult自定義注解即可; 在瀏覽器中輸入網址: http://127.0.0.1:8080/v1/api/user/findalluserbyannotation 進行查看:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
// 自定義注解用在了方法上 @responseresult @getmapping ( "/user/findalluserbyannotation" ) public list<user> findalluserbyannotation() { logger.info( "開始查詢所有數據..." ); list<user> findalluser = new arraylist<>(); findalluser.add( new user( "木子雷" , 26 )); findalluser.add( new user( "公眾號" , 28 )); logger.info( "使用 @responseresult 自定義注解進行響應的包裝,使controller代碼更加簡介" ); return findalluser; } |
至此我們的接口返回響應包裝自定義注解實現設計完成,看看代碼是不是又簡潔,又優雅呢。
總結:本文針對此方案只是進行了簡單的實現,如果有興趣的朋友可以進行更好的優化。
場景二:自定義注解 + aop = 實現優雅的使用分布式鎖
分布式鎖的最常見的使用流程:
先看看最為常見的分布式鎖使用方式的實現,然后再聊聊自定義注解怎么優雅的實現分布式鎖的使用。
普通的分布式鎖使用方式:
通過上面的代碼可以得到一個信息:如果有很多方法中需要使用分布式鎖,那么每個方法中都必須有獲取分布式鎖和釋放分布式鎖的代碼,這樣一來就會出現代碼冗余;
那有什么好的解決方案嗎? 自定義注解使代碼變得更加簡潔、優雅;
自定義注解優雅的使用分布式鎖:
①、首先實現一個標記分布式鎖使用的自定義注解:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
/** * @author 【 木子雷 】 公眾號 * @package_name: com.lyl.annotation * @classname: getdistributedlock * @description: 獲取redis分布式鎖 注解 * @date: 2020-11-10 16:24 **/ @target (elementtype.method) @retention (retentionpolicy.runtime) @documented public @interface getdistributedlock { // 分布式鎖 key string lockkey(); // 分布式鎖 value,默認為 lockvalue string lockvalue() default "lockvalue" ; // 過期時間,默認為 300秒 int expiretime() default 300 ; } |
②、定義一個切面,在切面中對使用了 @getdistributedlock 自定義注解的方法進行環繞增強通知:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
/** * @author: 【 木子雷 】 公眾號 * @package_name: com.lyl.aop * @classname: distributedlockaspect * @description: 自定義注解結合aop切面編程優雅的使用分布式鎖 * @date: 2020-11-10 16:52 **/ @component @aspect public class distributedlockaspect { private final logger logger = loggerfactory.getlogger( this .getclass()); @autowired redisservice redisservice; /** * around 環繞增強通知 * * @param joinpoint 連接點,所有方法都屬于連接點;但是當某些方法上使用了@getdistributedlock自定義注解時, * 則其將連接點變為了切點;然后在切點上織入額外的增強處理;切點和其相應的增強處理構成了切面aspect 。 */ @around (value = "@annotation(com.lyl.annotation.getdistributedlock)" ) public boolean handlerdistributedlock(proceedingjoinpoint joinpoint) { // 通過反射獲取自定義注解對象 getdistributedlock getdistributedlock = ((methodsignature) joinpoint.getsignature()) .getmethod().getannotation(getdistributedlock. class ); // 獲取自定義注解對象中的屬性值 string lockkey = getdistributedlock.lockkey(); string lockvalue = getdistributedlock.lockvalue(); int expiretime = getdistributedlock.expiretime(); if (redisservice.trygetdistributedlock(lockkey, lockvalue, expiretime)) { // 獲取分布式鎖成功后,繼續執行業務邏輯 try { return ( boolean ) joinpoint.proceed(); } catch (throwable throwable) { logger.error( "業務邏輯執行失敗。" , throwable); } finally { // 最終保證分布式鎖的釋放 redisservice.releasedistributedlock(lockkey, lockvalue); } } return false ; } } |
③、最后,在 controller 中的方法上使用 @getdistributedlock 自定義注解即可;當某個方法上使用了 自定義注解,那么這個方法就相當于一個切點,那么就會對這個方法做環繞(方法執行前和方法執行后)增強處理;
在瀏覽器中輸入網址: http://127.0.0.1:8080/v1/api/user/getdistributedlock 回車后觸發方法執行:
1
2
3
4
5
6
7
8
9
|
// 自定義注解的使用 @getdistributedlock (lockkey = "userlock" ) @getmapping ( "/user/getdistributedlock" ) public boolean getuserdistributedlock() { logger.info( "獲取分布式鎖..." ); // 寫具體的業務邏輯 return true ; } |
通過自定義注解的方式,可以看到代碼變得更加簡潔、優雅。
場景三:自定義注解 + aop = 實現日志的打印
先看看最為常見的日志打印的方式,然后再聊聊自定義注解怎么優雅的實現日志的打印。
普通日志的打印方式:
通過看上面的代碼可以知道,如果每個方法都需要打印下日志,那將會存在大量的冗余代碼;
自定義注解實現日志打印:
①、首先創建一個標記日志打印的自定義注解:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
/** * @author: 【 木子雷 】 公眾號 * @package_name: com.lyl.annotation * @classname: printlog * @description: 自定義注解實現日志打印 * @date: 2020-11-10 18:05 **/ @target (elementtype.method) @retention (retentionpolicy.runtime) @documented public @interface printlog { } |
②、定義一個切面,在切面中對使用了 @printlog 自定義注解的方法進行環繞增強通知:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
/** * @author: 【 木子雷 】 公眾號 * @package_name: com.lyl.aop * @classname: printlogaspect * @description: 自定義注解結合aop切面編程優雅的實現日志打印 * @date: 2020-11-10 18:11 **/ @component @aspect public class printlogaspect { private final logger logger = loggerfactory.getlogger( this .getclass()); /** * around 環繞增強通知 * * @param joinpoint 連接點,所有方法都屬于連接點;但是當某些方法上使用了@printlog自定義注解時, * 則其將連接點變為了切點;然后在切點上織入額外的增強處理;切點和其相應的增強處理構成了切面aspect 。 */ @around (value = "@annotation(com.lyl.annotation.printlog)" ) public object handlerprintlog(proceedingjoinpoint joinpoint) { // 獲取方法的名稱 string methodname = joinpoint.getsignature().getname(); // 獲取方法入參 object[] param = joinpoint.getargs(); stringbuilder sb = new stringbuilder(); for (object o : param) { sb.append(o + "; " ); } logger.info( "進入《{}》方法, 參數為: {}" , methodname, sb.tostring()); object object = null ; // 繼續執行方法 try { object = joinpoint.proceed(); } catch (throwable throwable) { logger.error( "打印日志處理error。。" , throwable); } logger.info( "{} 方法執行結束。。" , methodname); return object; } } |
③、最后,在 controller 中的方法上使用 @printlog 自定義注解即可;當某個方法上使用了 自定義注解,那么這個方法就相當于一個切點,那么就會對這個方法做環繞(方法執行前和方法執行后)增強處理;
1
2
3
4
5
6
7
|
@printlog @getmapping (value = "/user/findusernamebyid/{id}" , produces = "application/json;charset=utf-8" ) public string findusernamebyid( @pathvariable ( "id" ) int id) { // 模擬根據id查詢用戶名 string username = "木子雷 公眾號" ; return username; } |
④、在瀏覽器中輸入網址: http://127.0.0.1:8080/v1/api/user/findusernamebyid/66 回車后觸發方法執行,發現控制臺打印了日志:
進入《findusernamebyid》方法, 參數為: 66;
findusernamebyid 方法執行結束。。
使用自定義注解實現是多優雅,代碼看起來簡介干凈,越瞅越喜歡;趕快去你的項目中使用吧, 嘿嘿。。。
以上就是java 自定義注解的魅力的詳細內容,更多關于java 自定義注解的資料請關注服務器之家其它相關文章!
原文鏈接:https://leishen6.github.io/2020/11/15/understand_annotations_charm/