詳解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());
}
}
}
}
如有疑問請留言或者到本站社區交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。