Annotation 最终奥义,元注解
2019獨角獸企業重金招聘Python工程師標準>>>
當你自定義出一個注解的時候,這個注解其實是沒法使用的,因為你沒有為注解指定的生存范圍,生存的目標.。因此為了限定自定義注解,就出現了元注解這一個概念.
服務于注解的注解就是元注解。
其中元注解有一下的幾個常見的注解:
?? ?***@Retention:注解的保留范圍
? ?? /**
???? * Annotations are to be discarded by the compiler.
???? */
?? ??? ?RetentionPolicy.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.
???? */
?? ???? RetentionPolicy.CLASS:注解存在于源字節碼文件中,僅僅存在class的字節碼文件中
??? /**
???? * 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.
???? */
???? RetentionPolicy.RUNTIME:注解存在于運行時, 在運行的時候,才能獲取到的注解,這也是注解取代xml文件的重要核心。
?? ?***@Target:注解出現的位置
?? ?@Documented: 用于指定被該元 Annotation 修飾的 Annotation 類將被 javadoc 工具提取成文檔.
?? ?@Inherited: 被它修飾的 Annotation 將具有繼承性.如果某個類使用了被 @Inherited 修飾的 Annotation, 則其子類將自動具有該注解
例子:
@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME?) public?@interface?Person?{String??name()?default?"";int?age();boolean?married()?default?false;String?[]childrenName()?default?""; }最后一點,記著,注解是可以嵌套的!!!???? 一個注解里面可能還有其他注解作為成員變量,在這就不舉例了
轉載于:https://my.oschina.net/anyyang/blog/374192
總結
以上是生活随笔為你收集整理的Annotation 最终奥义,元注解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 轻松搞定RabbitMQ(二)——工作队
- 下一篇: 在 Ubuntu 14.04 中配置 P