如何在Java中正確的使用注解

如何在Java中正確的使用注解?針對這個問題,這篇文章詳細介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。

成都創(chuàng)新互聯(lián)始終堅持【策劃先行,效果至上】的經(jīng)營理念,通過多達十多年累計超上千家客戶的網(wǎng)站建設(shè)總結(jié)了一套系統(tǒng)有效的全網(wǎng)營銷推廣解決方案,現(xiàn)已廣泛運用于各行各業(yè)的客戶,其中包括:葡萄架等企業(yè),備受客戶贊譽。

注解是jdk1.5新增的特性.大家都知道,jdk1.5在java的發(fā)展史上有著劃時代的意義.而注解的出現(xiàn),在某種程度上顛覆了框架的設(shè)計.比如,spring在注解出現(xiàn)后,改善了原先五大組件的模式,增加了基于注解的實現(xiàn)方式.現(xiàn)在重點講講注解的使用.

元注解:

      jdk1.5定義了4個元注解,元注解的作用是注解其他的注解.

       1.@Retention

       2.@Target

       3.@Documented

       4.@Inherited

       @Retention用于指明該注解存在的時機.參數(shù)有三個值可選:RetentionPolicy.SOURCE,RetentionPolicy.CLASS,RetentionPolicy.RUNTIME可供選擇.分別表示:源碼中保留注解,字節(jié)碼文件中保留注解,運行時保留注解.

       @Target用于指明注解能作用的范圍.比如參數(shù)中設(shè)置為ElementType.TYPE,表示作用于類和接口.如果你用來注解方法,則會發(fā)生編譯錯誤.由此可見它的功能是通過編譯器實現(xiàn)的.

       @Documented表明該注解在使用javadoc工具生成開發(fā)文檔時,也會被納入進去.

       @Inherited表明,某個位置使用該注解,那么在存在Java繼承關(guān)系的地方,該注解也能被繼承過來.這個可能不好理解.下面的代碼加以說明.     

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE,ElementType.METHOD})
@Inherited
public @interface AnnoInherited {
  
}

測試代碼:

public class TestAnnoInherated {
  public static void main(String[] args) {
    Annotation[] annos=new Goo().getClass().getAnnotations();
    for(Annotation a:annos){
      System.out.println(a);
    }
  }
}

@AnnoInherited
class Foo{
  
}

class Goo extends Foo{
  
}

控制臺輸出:

@test.annotation.AnnoInherited()

上例中Goo前面并沒有加注解@AnnoInherited,但是父類Foo前面有,而@AnnoInherited加了元注解@Inherited,所以Foo能繼承過來.

自定義注解:

自定義注解的實例如下.

package test.annotation;

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;

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface AnimalInfo {
  String shout() default "";
  
  //能不能看門
  boolean isGuard() default true;
}

測試代碼:

public class TestAnimalInfo {
  public static void main(String[] args) {
    Animal animal=new Animal();
    AnimalInfo info=animal.getClass().getAnnotation(AnimalInfo.class);
    if(info!=null){
      Annotation anno=info;//此處并沒有報錯.Annotation是一個接口.info是一個注解.這是因為編譯器會將注解編譯成接口,并且繼承了Annotation
      System.out.println("Annotation類信息:"+Annotation.class);
      System.out.println("AnimalInfo類信息:"+AnimalInfo.class);
      Class[] cs=AnimalInfo.class.getInterfaces();
      for(Class c:cs){
        System.out.println(c); //AnimalInfo編譯后就是一個接口,并且繼承了Annotation,這里得到了證實.
      }
      System.out.println("info對象的類信息:"+info.getClass());
      if("wangwang".equals(info.shout())&&info.isGuard()){
        System.out.println("the animal is a dog");
      }else if("miaomiao".equals(info.shout())&&!info.isGuard()){
        System.out.println("the animal is a cat");
      }else{
        System.out.println("the animal is not a dog or cat");
      }
    }else{
      System.out.println("it's not a animal");
    }
  }
}

@AnimalInfo(shout="wangwang",isGuard=true)
class Animal{
  
}

控制臺輸出:

Annotation類信息:interface java.lang.annotation.Annotation
AnimalInfo類信息:interface test.annotation.AnimalInfo
interface java.lang.annotation.Annotation
info對象的類信息:class com.sun.proxy.$Proxy1
the animal is a dog

代碼分析:從控制臺可以看到.@AnimalInfo注解其實編譯后就是接口,并且它繼承了Annnotation.而通過反射獲得的注解實例,名字為$Proxy1,是一個類的對象.可見,該注解實例是JVM通過動態(tài)代理技術(shù)生成的.這也揭示了注解特性的底層實現(xiàn)原理.關(guān)于注解的具體底層技術(shù)原理,這里不再詳談.

關(guān)于如何在Java中正確的使用注解問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識。

網(wǎng)站標(biāo)題:如何在Java中正確的使用注解
文章路徑:http://muchs.cn/article12/pgoidc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供靜態(tài)網(wǎng)站、做網(wǎng)站、軟件開發(fā)網(wǎng)站策劃、Google虛擬主機

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都做網(wǎng)站