詳解Java注解的實(shí)現(xiàn)與使用方法
詳解Java注解的實(shí)現(xiàn)與使用方法
Java注解是java5版本發(fā)布的,其作用就是節(jié)省配置文件,增強(qiáng)代碼可讀性。在如今各種框架及開發(fā)中非常常見,特此說明一下。
如何創(chuàng)建一個(gè)注解
每一個(gè)自定義的注解都由四個(gè)元注解組成,這四個(gè)元注解由java本身提供:
@Target(ElementType.**)
這是一個(gè)枚舉,它置頂是該自定義的注解使用的地方,像類、變量、方法等
@Retention(RetentionPolicy.**)作用是標(biāo)明注解保存在什么級(jí)別,像在編譯時(shí)、class文件中,vm運(yùn)行中
@Documented 將此注解包含在 javadoc 中 ,它代表著此注解會(huì)被javadoc工具提取成文檔。在doc文檔中的內(nèi)容會(huì)因?yàn)榇俗⒔獾男畔?nèi)容不同而不同
@Inherited : 在您定義注解后并使用于程序代碼上時(shí),預(yù)設(shè)上父類別中的注解并不會(huì)被繼承至子類別中,您可以在定義注解時(shí)加上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;
/**
* 這是一個(gè)類級(jí)別的注釋,這個(gè)注釋中有一個(gè)name字段,默認(rèn)值是 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;
/**
* 這是一個(gè)變量級(jí)別的注解,注解中有一個(gè)字段name,默認(rèn)值是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;
/**
* 這是一個(gè)方法級(jí)別的注解
* @author yasin
*
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MethodLearn {
String name() default "method";
}
上面了我定義了三個(gè)注解,分別是常用的類、變量、方法三個(gè)級(jí)別的注解。
下面我定義一個(gè)類,使用這三個(gè)注解
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(){
}
}
下面就是如何使用這個(gè)注解了,注解的提取,都是通過class反射得到相應(yīng)的變量和方法,在從變量和方法中獲得注解。
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)){//校驗(yàn)該字段是否添加這個(gè)注解
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)){//校驗(yàn)該方法是否有這個(gè)注解
System.out.println(method.getName());
MethodLearn methodLearn = method.getAnnotation(MethodLearn.class);
System.out.println(methodLearn.name());
}
}
}
}
如有疑問請(qǐng)留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
相關(guān)文章
Java實(shí)現(xiàn)簡(jiǎn)易拼圖游戲的方法詳解
這篇文章主要介紹了如何利用Java語言實(shí)現(xiàn)簡(jiǎn)易拼圖游戲,幫助大家更好的理解和使用Java開發(fā)游戲,感興趣的朋友可以跟隨小編一起學(xué)習(xí)一下2022-05-05
SpringBoot使用CommandLineRunner和ApplicationRunner執(zhí)行初始化業(yè)務(wù)方式
這篇文章主要介紹了SpringBoot使用CommandLineRunner和ApplicationRunner執(zhí)行初始化業(yè)務(wù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08
java中循環(huán)刪除list中元素的方法總結(jié)
下面小編就為大家?guī)硪黄猨ava中循環(huán)刪除list中元素的方法總結(jié)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-12-12
Java實(shí)現(xiàn)滑動(dòng)窗口算法的示例代碼
滑動(dòng)窗口算法是一種高效解決子數(shù)組、子字符串問題的算法,廣泛應(yīng)用于數(shù)據(jù)流處理、網(wǎng)絡(luò)限流和字符串操作等場(chǎng)景,本文將詳細(xì)解析滑動(dòng)窗口算法的核心思想、常見問題及其實(shí)現(xiàn)方式,需要的朋友可以參考下2025-03-03
Spring中使用atomikos+druid實(shí)現(xiàn)經(jīng)典分布式事務(wù)的方法
這篇文章主要介紹了Spring中使用atomikos+druid實(shí)現(xiàn)經(jīng)典分布式事務(wù)的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-06-06
簡(jiǎn)單談?wù)凧VM、JRE和JDK的區(qū)別與聯(lián)系
簡(jiǎn)單的說JDK是用于開發(fā)的而JRE是用于運(yùn)行Java程序的。JDK和JRE都包含了JVM,從而使得我們可以運(yùn)行Java程序。JVM是Java編程語言的核心并且具有平臺(tái)獨(dú)立性。2016-05-05

