首頁  >  文章  >  Java  >  Java實作註解的使用方法介紹

Java實作註解的使用方法介紹

黄舟
黄舟原創
2017-09-14 10:49:431488瀏覽

這篇文章主要介紹了詳解Java註解的實作與使用方法的相關資料,希望透過本文大家能夠理解掌握Java註解的知識,需要的朋友可以參考下

#Java註解的實作與使用方法

Java註解是java5版本發布的,其作用就是節省設定文件,增強程式碼可讀性。在如今各種框架及開發中非常常見,特此說明一下。

如何建立一個註解

每一個自訂的註解都由四個元註解組成,這四個元註解由java本身提供:

@Target(ElementType.**)

這是一個枚舉,它置頂是該自訂的註解使用的地方,像類別、變數、方法等

@Retention(RetentionPolicy.**)作用是標明註解保存在什麼級別,像在編譯時、class檔案中,vm運行中

@Documented 將此註解包含在javadoc 中,它代表著此註解會被javadoc工具提取成文檔。在doc文件中的內容會因為此註解的資訊內容不同而不同

@Inherited : 在您定義註解後並使用於程式碼上時,預設上父類別中的註解並不會被繼承至子類別中,您可以在定義註解時加上java.lang.annotation.Inherited 限定的Annotation,這讓您定義的Annotation類型被繼承下來。

介紹完理論,開始程式碼(talk is cheap,show your code)


#
package com.yasin.JavaLearn;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * 这是一个类级别的注释,这个注释中有一个name字段,默认值是 yasin
 * @author yasin
 *
 */

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Learn {
  String name() default "yasin";
}


package com.yasin.JavaLearn;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * 这是一个变量级别的注解,注解中有一个字段name,默认值是field
 * @author yasin
 *
 */

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface FiledLearn {
  String name() default "field";


}


#
package com.yasin.JavaLearn;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * 这是一个方法级别的注解
 * @author yasin
 *
 */

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MethodLearn {
  String name() default "method";

}

上面了我定義了三個註解,分別是常用的類別、變數、方法三個層級的註解。

下面我定義一個類,用這三個註解


package com.yasin.JavaLearn;

@Learn
public class Yasin {

  @FiledLearn
  public int level;

  @FiledLearn(name="xq")
  public String xq;

  public String a;


  @MethodLearn(name="test")
  public void setMain(){

  }

  public void setA(){

  }

}

下面就是如何使用這個註解了,註解的提取,都是透過class反射得到對應的變數和方法,在從變數和方法中得到註解。


package com.yasin.JavaLearn;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.apache.log4j.xml.DOMConfigurator;

/**
 * Hello world!
 *
 */
public class App {

  public static void main(String[] args) {

    Learn learn = Yasin.class.getAnnotation(Learn.class);
    System.out.println(learn.name());


    Field[] fields = Yasin.class.getFields();//获取该类所有的字段

    for(Field filed:fields){
      if(filed.isAnnotationPresent(FiledLearn.class)){//校验该字段是否添加这个注解
        System.out.println(filed.getName());
        FiledLearn filedLearn = filed.getAnnotation(FiledLearn.class);
        System.out.println(filedLearn.name());
      }
    }

    Method[] methods = Yasin.class.getMethods();
    for(Method method:methods){
      if(method.isAnnotationPresent(MethodLearn.class)){//校验该方法是否有这个注解
        System.out.println(method.getName());
        MethodLearn methodLearn = method.getAnnotation(MethodLearn.class);
        System.out.println(methodLearn.name());
      }

    }


  }
}

以上是Java實作註解的使用方法介紹的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn