Java元註解

來源:互聯網
上載者:User

標籤:gre   成員變數   忽略   運行   runtime   其他   oid   splay   顏色   

元註解的作用是負責註解其他註解。Java定義了4中標準的元註解類型,他們被用來提供對其他註解的說明。

@target

@Retention

@Documented

@Inherited

這些類型可以和他們所支援的類在Java.lang.annotation包中找到

每個元註解的作用:

@target

修飾了annotation所修飾的物件範圍,annotation可被用於package,type(類,介面,枚舉,annotation),和類型成員(方法,構造方法,成員變數,枚舉值)、方法參數和本地變數(迴圈變數和catch參數)在annotation型別宣告中,使用target更加清晰其修飾的目標。

作用:用於描述註解用在什麼地方

取值ElementType

constructor:描述構造器

Filed:描述域

local_variable:用於描述局部變數

method:用於描述方法

package:用於描述包

parameter:用於描述參數

type:用於描述類,介面,包括註解類型或者枚舉類型

使用執行個體:

@Target(ElementType.Type)

public @interface table{

  //資料表名稱註解,預設值為類名稱

  public String tableName() default “ClassName”;

}

 

@Target(ElementType.Feild)

public @interface NoDBColumn{

 

}

@Retention

@Retention定義了該annotation被保留的時間,有些annotation僅出現在原始碼中,有些被編譯在class檔案中,編譯在class檔案中的annotation可能被虛擬機器忽略,有些在class被裝載時讀取(並不影響class的運行,annotation與class在使用時是分離的)使用Retention可以的影響annotation的生命週期。

作用:表示需要在什麼層級儲存注釋資訊

retentionplicy

source:在源檔案中有效

class在class檔案中有效

runtime在運行時有效

retention有唯一的value作為成員,取值來自Java.lang.annotation.retationpolicy的枚舉類型值

@Target(ElementType.Filed)

@Retention(RetentionPolicy.Runtime)

public @interface Column{

  public String name() default "fieldName";

  public String setFuncName() default "setFiled";

  public String getFuncName() default "getField";

  public boolean defaultDBValue() default false;

}

column註解的RetentionPolicy的屬性值是Runtime,這樣註解處理器可以通過反射,擷取

該註解的屬性值,從而做一些運行時的邏輯處理

@Documented

用於描述其他類型的annotation應該被作為被標註的程式成員的公用API,因此可以被Javadoc工具文檔化,documented是一個標記註解,沒有成員。

@Target(ElementType.Field)

@Retention(RetentionPolicy.RUntime)

@Documented

public @interface Column{

  public String name() default "fieldName";

  public String setFuncName() default "setField";

  public String getFunName() default "getField";

  public boolean defaultDBValue() default false;

}

@Inherited

@Inherited是一個標記註解,闡述了某個被標註的類型是被繼承的。如果一個使用了@Inherited修飾的annotation類型被用於一個class,則這個annotation將被用於該class的子類。

@Inherited annotation類型是被標註過的class的子類所繼承,類並不從它所實現的介面繼承annotation,方法並不從它所重載的方法繼承annotation。

當@Inherited annotation類型標註的annotation的retention是retentionpolicy.runtime,則反射API增強了這種繼承性,如果我們使用Java.lang.reflect去查詢一個@Inherited annotation,代碼檢查將展開工作:展開class和其父類,知道發現指定的annotation。

@Inherited

public @interface Greeting{

  public enum FontColor{blue,red,green}

  String name();

  FontColor fontColor() default FontColor.green;

}

使用annotation定義註解時,自動繼承了Java.lang.annotation介面,由編譯器自動完成其他細節。在定義註解時,不能繼承其他註解或者介面。@interface用來聲明一個註解,其中的每個方法表示聲明一個配置參數。方法的名稱是參數的名稱,傳回值類型是參數的類型,傳回值類型只能是基本類型,class,String,enum,通過default來聲明參數的預設值。

註解參數的可支援資料類型:

基礎資料型別 (Elementary Data Type)

String類型

Class類型

enum類型

annotation類型

以上所有類型的數組

annotation類型中的參數

1.只能用public或者預設default這兩個訪問權修飾,

如String value() 這裡把方法設定為default預設類型,

2.參數成員只能用上面的類型,String value() 這裡的參數成員是String

3.如果只有一個參數成員,則把參數名稱設定為value,後面加小括弧

 

@Target(ElementType.Field)

@Retention(RetentionPolicy.runtime)

@Documented

public @interface fruitName{

  String value() default "";

}

//水果顏色註解

@Target(ElementType.Field)

@Retention(RetentionPolicy.runtime)

@Documented

public @interface FruitColor{

  //顏色枚舉

  public enum Color{Blue,RED,Green};

  //顏色屬性

  Color fruitColor() default Color.green;

}

 

public Class Apple{

  @FruitName("Apple")

  private String appleName;

@FruitColor(fruitColor=color.red)

private String appleColor;

public void setAppleColor(String AppleColor){

  this.appleColor = appleColor;

}

public String getAppleColor(){

  return appleColor;

}

public void setAppleName(String appleName){

  this.appleName=appleName;

}

public String getAppleName(){

  return appleName;

}

public void displayName(){

  system.out.println("水果的名字是蘋果");

}

}

註解元素必須有確定的值,要麼在定義註解預設值中指定,要麼在使用註解時指定,非基本類型的註解值不可為null,常常使用Null 字元串或者0為預設值,

 1 package annotation; 2  3 import java.lang.annotation.Documented; 4 import java.lang.annotation.ElementType; 5 import java.lang.annotation.Retention; 6 import java.lang.annotation.RetentionPolicy; 7 import java.lang.annotation.Target; 8  9 /**10  * 水果供應者註解11  * @author peida12  *13  */14 @Target(ElementType.FIELD)15 @Retention(RetentionPolicy.RUNTIME)16 @Documented17 public @interface FruitProvider {18     /**19      * 供應商編號20      * @return21      */22     public int id() default -1;23     24     /**25      * 供應商名稱26      * @return27      */28     public String name() default "";29     30     /**31      * 供應商地址32      * @return33      */34     public String address() default "";35 }
          

 

Java元註解

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.