Java 注解 Dependency injection
前言
Linus Benedict Torvalds : RTFSC – Read The Fucking Source Code
現(xiàn)有的開源庫
- RoboGuice
- Android Annotations
- Dragger
- ButterKnife
- Transfuse
注解(Annotation)
從三個(gè)方向了解注解:
- 注解是什么?
- 注解有什么用瞧甩?
- 注解怎么用喧务?
注解是什么
注解(Annotation)蚤蔓,也叫元數(shù)據(jù)所森。一種代碼級別的說明待榔,是一種將程序中的元素與任何信息關(guān)聯(lián)一起的途徑和方法统锤。它是JDK1.5及以后版本引入的一個(gè)特性陆淀,與類尤辱、接口砂豌、枚舉是在同一個(gè)層次。
注解有什么用
- 創(chuàng)建文檔光督。
- 跟蹤代碼中的依賴性阳距。
- 甚至執(zhí)行基本編譯時(shí)檢查。
在Java 5.0 中java.lang包有三個(gè)標(biāo)準(zhǔn)Annotation類型结借。
- 標(biāo)準(zhǔn) Annotation (JDK內(nèi)置系統(tǒng)注解)
標(biāo)準(zhǔn) Annotation 是指 Java 自帶的幾個(gè) Annotation筐摘,例如: @Override, @Deprecated, @SuppressWarnings。
- 元 Annotation (元注解)
元 Annotation 是指用來定義 Annotation 的 Annotation映跟,例如:@Retention, @Target, @Inherited, @Documented蓄拣。
- 自定義 Annotation (自定義注解)
自定義 Annotation 表示自己根據(jù)需要定義的 Annotation,定義時(shí)需要用到元 Annotation努隙。
注解怎么用
注解的語法比較簡單球恤,就是在前面添加
@
符號,后面的語法就更Java語法一致荸镊。
標(biāo)準(zhǔn) Annotation
我們列出三個(gè)例子:
- @Override
- @Deprecated
- @SuppressWarnnings
@Override
用于修飾此方法覆蓋了父類的方法咽斧。
@Override 是一個(gè)標(biāo)記注解類型,它被用作標(biāo)注方法躬存。它說明了被標(biāo)注的方法重載了父類的方法张惹,起到了斷言的作用。如果重載的方法寫錯(cuò)了在編譯期會(huì)有警告出現(xiàn)岭洲。
@Deprecated
用于修飾已經(jīng)過時(shí)的方法宛逗。
@Deprecated是一個(gè)標(biāo)記注解。當(dāng)一個(gè)類型或者類型成員使用@Deprecated修飾的話盾剩,編譯器將不鼓勵(lì)使用這個(gè)被標(biāo)注的程序元素雷激。這個(gè)標(biāo)記注解具有繼承性,可被子類繼承告私。
@SuppressWarnnings
用于通知java編譯器禁止特定的編譯警告屎暇。
@SuppressWarnings 被用于有選擇的關(guān)閉編譯器對類、方法驻粟、成員變量根悼、變量初始化的警告。@SuppressWarnings使用格式有這幾種:
- @SuppressWarnings(value={"deprecation", "unchecked"})
- @SuppressWarnings({"deprecation", "unchecked"})
- @SuppressWarnings("unchecked")
常見的@SuppressWarnings參數(shù)值:
- all to suppress all warnings
- boxing to suppress warnings relative to boxing/unboxing operations
- cast to suppress warnings relative to cast operations
- dep-ann to suppress warnings relative to deprecated annotation
- deprecation to suppress warnings relative to deprecation
- fallthrough to suppress warnings relative to missing breaks in switch statements
- finally to suppress warnings relative to finally block that don’t return
- hiding to suppress warnings relative to locals that hide variable
- incomplete-switch to suppress warnings relative to missing entries in a switch statement (enum case)
- nls to suppress warnings relative to non-nls string literals
- null to suppress warnings relative to null analysis
- rawtypes to suppress warnings relative to un-specific types when using generics on class params
- restriction to suppress warnings relative to usage of discouraged or forbidden references
- serial to suppress warnings relative to missing serialVersionUID field for a serializable class
- static-access to suppress warnings relative to incorrect static access
- synthetic-access to suppress warnings relative to unoptimized access from inner classes
- unchecked to suppress warnings relative to unchecked operations
- unqualified-field-access to suppress warnings relative to field access unqualified
- unused to suppress warnings relative to unused code
元 Annotation
元注解的作用就是負(fù)責(zé)注解其他注解。在Java 5.0 中有四個(gè)標(biāo)準(zhǔn)元注解:
- @Target
- @Retention
- @Documented
- @Inherited
@Target
定義了Annotation所修飾的對象范圍:可被用于 packages挤巡、types(類剩彬、接口、枚舉矿卑、Annotation類型)襟衰、類型成員(方法、構(gòu)造方法粪摘、成員變量瀑晒、枚舉值)、方法參數(shù)和本地變量(如循環(huán)變量徘意、catch參數(shù))苔悦。
取值類型有這么幾個(gè):
- CONSTRUCTOR:用于描述構(gòu)造器
- FIELD:用于描述域
- LOCAL_VARIABLE:用于描述局部變量
- METHOD:用于描述方法
- PACKAGE:用于描述包
- PARAMETER:用于描述參數(shù)
- TYPE:用于描述類、接口(包括注解類型) 或enum聲明
@Retention
定義了該Annotation被保留的時(shí)間長短椎咧。
取值類型有這么幾個(gè):
- SOURCE:在源文件中有效(即源文件保留)
- CLASS:在class文件中有效(即class保留)
- RUNTIME:在運(yùn)行時(shí)有效(即運(yùn)行時(shí)保留)
@Documented
用于描述其它類型的annotation應(yīng)該被作為被標(biāo)注的程序成員的公共API玖详,因此可以被如javadoc此類的工具文檔化。
@Inherited
這是一個(gè)標(biāo)記注解勤讽,闡述了某個(gè)被標(biāo)注的類型是被繼承的蟋座。
自定義 Annotation
通過使用@interface
符號來完成自定義注解,自定義注解不能繼承其它注解或接口脚牍∠蛲危可以通過default來聲明參數(shù)的默認(rèn)值。
注解格式:
public @interface 注解名 {定義體}
支持的數(shù)據(jù)類型:
- 所有基本數(shù)據(jù)類型(int,float,boolean,byte,double,char,long,short)
- String類型
- Class類型
- enum類型
- Annotation類型
- 以上所有類型的數(shù)組
備注:
- 如果只有一個(gè)參數(shù)成員,最好把參數(shù)名稱設(shè)為"value"诸狭,后加小括號券膀。
- 只能用public或默認(rèn)(default)這兩個(gè)訪問權(quán)修飾。
例子:
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface FileName {
String value() default "";
}
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface FileType {
public enum Color{BULE,RED,GREEN};
Color fruitColor() default Color.GREEN;
}
// 使用
@FileName("Safe")
private String FileName;
注解處理器類庫
最后這里介紹下注解處理類庫:
java.lang.reflect.AnnotatedElement
可以通過它處理很多復(fù)雜的注解工作驯遇。