深入理解Java注解的使用方法
注解是jdk1.5新增的特性.大家都知道,jdk1.5在java的發(fā)展史上有著劃時(shí)代的意義.而注解的出現(xiàn),在某種程度上顛覆了框架的設(shè)計(jì).比如,spring在注解出現(xiàn)后,改善了原先五大組件的模式,增加了基于注解的實(shí)現(xiàn)方式.現(xiàn)在重點(diǎn)講講注解的使用.
元注解:
jdk1.5定義了4個(gè)元注解,元注解的作用是注解其他的注解.
1.@Retention
2.@Target
3.@Documented
4.@Inherited
@Retention用于指明該注解存在的時(shí)機(jī).參數(shù)有三個(gè)值可選:RetentionPolicy.SOURCE,RetentionPolicy.CLASS,RetentionPolicy.RUNTIME可供選擇.分別表示:源碼中保留注解,字節(jié)碼文件中保留注解,運(yùn)行時(shí)保留注解.
@Target用于指明注解能作用的范圍.比如參數(shù)中設(shè)置為ElementType.TYPE,表示作用于類和接口.如果你用來注解方法,則會(huì)發(fā)生編譯錯(cuò)誤.由此可見它的功能是通過編譯器實(shí)現(xiàn)的.
@Documented表明該注解在使用javadoc工具生成開發(fā)文檔時(shí),也會(huì)被納入進(jìn)去.
@Inherited表明,某個(gè)位置使用該注解,那么在存在Java繼承關(guān)系的地方,該注解也能被繼承過來.這個(gè)可能不好理解.下面的代碼加以說明.
@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{
}
控制臺(tái)輸出:
@test.annotation.AnnoInherited()
上例中Goo前面并沒有加注解@AnnoInherited,但是父類Foo前面有,而@AnnoInherited加了元注解@Inherited,所以Foo能繼承過來.
自定義注解:
自定義注解的實(shí)例如下.
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;//此處并沒有報(bào)錯(cuò).Annotation是一個(gè)接口.info是一個(gè)注解.這是因?yàn)榫幾g器會(huì)將注解編譯成接口,并且繼承了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編譯后就是一個(gè)接口,并且繼承了Annotation,這里得到了證實(shí).
}
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{
}
控制臺(tái)輸出:
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
代碼分析:從控制臺(tái)可以看到.@AnimalInfo注解其實(shí)編譯后就是接口,并且它繼承了Annnotation.而通過反射獲得的注解實(shí)例,名字為$Proxy1,是一個(gè)類的對象.可見,該注解實(shí)例是JVM通過動(dòng)態(tài)代理技術(shù)生成的.這也揭示了注解特性的底層實(shí)現(xiàn)原理.關(guān)于注解的具體底層技術(shù)原理,這里不再詳談.
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java中Timer的schedule()方法參數(shù)詳解
今天小編就為大家分享一篇關(guān)于Java中Timer的schedule()方法參數(shù)詳解,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-03-03
詳解Vue響應(yīng)式的部分實(shí)現(xiàn)
響應(yīng)式,簡單來說當(dāng)數(shù)據(jù)發(fā)生變化時(shí),對數(shù)據(jù)有依賴的代碼會(huì)重新執(zhí)行。這篇文章主要為大家介紹了Vue中響應(yīng)式的部分實(shí)現(xiàn),感興趣的可以了解一下2022-12-12
mybatisplus?selectOne查詢,有數(shù)據(jù),但返回為null問題
這篇文章主要介紹了mybatisplus?selectOne查詢,有數(shù)據(jù),但返回為null問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11
使用@TableField(updateStrategy=FieldStrategy.IGNORED)遇到的坑記錄
這篇文章主要介紹了使用@TableField(updateStrategy=FieldStrategy.IGNORED)遇到的坑及解決方案,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11
Spring-data-redis操作redis知識(shí)總結(jié)
這篇文章主要介紹了Spring-data-redis操作redis知識(shí)總結(jié),spring-data-redis是spring-data模塊的一部分,專門用來支持在spring管理項(xiàng)目對redis的操作。2017-04-04
java.lang.UnsatisfiedLinkError: %1 不是有效的Win32應(yīng)用程序錯(cuò)誤解決
這篇文章主要給大家介紹了關(guān)于java.lang.UnsatisfiedLinkError: %1 不是有效的Win32應(yīng)用程序錯(cuò)誤的解決方法,文中介紹的非常詳細(xì),需要的朋友們可以參考學(xué)習(xí),下面來一起看看吧。2017-03-03
Java中注解@Async實(shí)現(xiàn)異步及導(dǎo)致失效原因分析
Async注解用于聲明一個(gè)方法是異步的,當(dāng)在方法上加上這個(gè)注解時(shí)將會(huì)在一個(gè)新的線程中執(zhí)行該方法,而不會(huì)阻塞原始線程,這篇文章主要給大家介紹了關(guān)于Java中注解@Async實(shí)現(xiàn)異步及導(dǎo)致失效原因分析的相關(guān)資料,需要的朋友可以參考下2024-07-07
springboot中項(xiàng)目啟動(dòng)時(shí)實(shí)現(xiàn)初始化方法加載參數(shù)
這篇文章主要介紹了springboot中項(xiàng)目啟動(dòng)時(shí)實(shí)現(xiàn)初始化方法加載參數(shù),具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12

