你們都知道註解是實現了java.lang.annotation.Annotation接口,眼見爲實,耳聽爲虛,有時候眼見也不必定是真實的。java
/** * The common interface extended by all annotation types. Note that an * interface that manually extends this one does <i>not</i> define * an annotation type. Also note that this interface does not itself * define an annotation type. * * More information about annotation types can be found in section 9.6 of * <cite>The Java™ Language Specification</cite>. * * The {@link java.lang.reflect.AnnotatedElement} interface discusses * compatibility concerns when evolving an annotation type from being * non-repeatable to being repeatable. * * @author Josh Bloch * @since 1.5 */
元註解 通常用於指定某個註解生命週期以及做用目標等信息。正如源碼的註釋同樣,若是自定義的註解沒有添加元註解就和日常的註釋沒有多大的區別,有了元註解就會讓編譯器將信息編譯進字節碼文件。git
@Target
用於指明被修飾的註解最終能夠做用的目標github
ElementType
是一個枚舉類型spring
ElementType.TYPE:類,接口(包括註釋類型)或枚舉聲明 ElementType.FIELD:字段聲明(包括枚舉常量) ElementType.METHOD:方法聲明 ElementType.PARAMETER:正式參數聲明 ElementType.CONSTRUCTOR:構造器聲明 ElementType.LOCAL_VARIABLE:本地局部變量聲明 ElementType.ANNOTATION_TYPE:註解聲明 ElementType.PACKAGE:包聲明 ElementType.TYPE_PARAMETER:類型參數聲明 jdk1.8新增 ElementType.TYPE_USE:使用一種類型 jdk1.8新增
@Retention
用於指明當前註解的生命週期segmentfault
RetentionPolicy
是一個枚舉類型springboot
RetentionPolicy.SOURCE:編譯器將丟棄註釋。 RetentionPolicy.CLASS:註釋將由編譯器記錄在類文件中,但在運行時不須要由VM保留。 RetentionPolicy.RUNTIME:註釋將由編譯器記錄在類文件中而且在運行時由VM保留,所以能夠反射性地讀取它們。
@Documented
表示具備類型的註釋將由javadoc記錄和默認的相似工具。 這種類型應該用來註釋註解影響註解使用的類型的聲明客戶的元素。 若是使用註解類型聲明記錄,其註解成爲公共API的一部分註釋元素。工具
@Inherited
表示自動繼承註解類型。 若是註解類型上存在繼承的元註解聲明,用戶查詢類的註解類型聲明,類聲明沒有此類型的註解,而後將自動查詢該類的超類註解類型。 將重複此過程,直到爲此註解找到類型,或類層次結構的頂部(對象)到達了。 若是沒有超類具備此類型的註解,那麼查詢將指示有問題的類沒有這樣的註解。請注意,若是帶註解,則此元註解類型無效type
用於註解除類以外的任何內容。 另請注意這個元註解只會致使註解被繼承來自超類; 已實現的接口上的註解沒有效果。this
package com.github.dqqzj.springboot.annotation; import org.springframework.core.annotation.AliasFor; import org.springframework.stereotype.Component; import org.springframework.stereotype.Service; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** * @author qinzhongjian * @date created in 2019-07-28 07:54 * @description: TODO * @since JDK 1.8.0_212-b10 */ @Target(value = {ElementType.TYPE}) @Retention(value = RetentionPolicy.RUNTIME) @Component public @interface Hello { @AliasFor( annotation = Component.class ) String value() default "hi" ; }
如上圖所示註解其實也是使用了代理,並且是JDK代理的。spa
既然是運行時生成的代理類,咱們就能夠在啓動類上添加System.setProperty("sun.misc.ProxyGenerator.saveGeneratedFiles","true")
或者3d
咱們來分析一下生成的代理類
package com.sun.proxy; import com.github.dqqzj.springboot.annotation.Hello; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; import java.lang.reflect.UndeclaredThrowableException; public final class $Proxy1 extends Proxy implements Hello { private static Method m1; private static Method m2; private static Method m4; private static Method m0; private static Method m3; public $Proxy1(InvocationHandler var1) throws { super(var1); } public final boolean equals(Object var1) throws { try { return (Boolean)super.h.invoke(this, m1, new Object[]{var1}); } catch (RuntimeException | Error var3) { throw var3; } catch (Throwable var4) { throw new UndeclaredThrowableException(var4); } } public final String toString() throws { try { return (String)super.h.invoke(this, m2, (Object[])null); } catch (RuntimeException | Error var2) { throw var2; } catch (Throwable var3) { throw new UndeclaredThrowableException(var3); } } public final Class annotationType() throws { try { return (Class)super.h.invoke(this, m4, (Object[])null); } catch (RuntimeException | Error var2) { throw var2; } catch (Throwable var3) { throw new UndeclaredThrowableException(var3); } } public final int hashCode() throws { try { return (Integer)super.h.invoke(this, m0, (Object[])null); } catch (RuntimeException | Error var2) { throw var2; } catch (Throwable var3) { throw new UndeclaredThrowableException(var3); } } public final String value() throws { try { return (String)super.h.invoke(this, m3, (Object[])null); } catch (RuntimeException | Error var2) { throw var2; } catch (Throwable var3) { throw new UndeclaredThrowableException(var3); } } static { try { m1 = Class.forName("java.lang.Object").getMethod("equals", Class.forName("java.lang.Object")); m2 = Class.forName("java.lang.Object").getMethod("toString"); m4 = Class.forName("com.github.dqqzj.springboot.annotation.Hello").getMethod("annotationType"); m0 = Class.forName("java.lang.Object").getMethod("hashCode"); m3 = Class.forName("com.github.dqqzj.springboot.annotation.Hello").getMethod("value"); } catch (NoSuchMethodException var2) { throw new NoSuchMethodError(var2.getMessage()); } catch (ClassNotFoundException var3) { throw new NoClassDefFoundError(var3.getMessage()); } } }
這裏的InvocationHandler
其實是咱們的AnnotationInvocationHandler
,這裏有一個memberValues
,它是一個 Map
鍵值對,鍵是咱們註解屬性名稱,值就是該屬性當初被賦上的值。接下來我調試代碼給你們分享一下奧祕。
Hello hello = TestAnnotation.class.getAnnotation(Hello.class)
這個部分的調試代碼我會忽略直接調試
AnnotationInvocationHandler
的相關方法。
Annotation
接口的熟悉jdk規範的就會發現最底部的s#7RuntimeVisibleAnnotations
這個是運行時可訪問的註解信息,可供咱們反射獲取。
虛擬機規範定義了一系列和註解相關的屬性表,不管是字段、方法或是類自己,若是被註解修飾了,就能夠被寫進字節碼文件。屬性表有如下幾種:
RuntimeVisibleAnnotations:運行時可見的註解 RuntimeInVisibleAnnotations:運行時不可見的註解 RuntimeVisibleParameterAnnotations:運行時可見的方法參數註解 RuntimeInVisibleParameterAnnotations:運行時不可見的方法參數註解 AnnotationDefault:註解類元素的默認值`
說明: 明明只有一個@Hello
註解爲何左側會出現2個代理類的緣由就在這個地方,會多出一個代理類
public final class $Proxy0 extends Proxy implements Retention { //省略無關代碼....... }
@Hello(value = "hi")
RUNTIME
的註解取出並經過動態代理機制生成一個實現註解接口的代理類咱們已經知道了註解的值是存放在Map<String, Object> memberValues
中的,那麼咱們就能夠使用反射獲取並從新賦值。