註解使用的場景:
做過web開發的都知道,很多開發框架都使用註解,比如Spring中如下代碼:
@RequestMapping(value="/inputPerson")
public String inputPerson(){
System.out.println("..........inputPerson");
return "inputPerson";
}
@RequestMapping(value="/updatePerson")
public String updatePerson(){
System.out.println("..........updatePerson");
return "updatePerson";
}
這段代碼中 RequestMapping就是一個註解,註解前面用@符號修飾,這個註解作用於方法上,裏面的value用於註解賦值。
註解類型:
1:標準註解:
Override作用:保證編譯時候Override函數的申明正確性。
Deprecated作用:保證編譯時候Override函數的申明正確性。
SuppressWarnings 關閉特定警告,他的主要參數有:
1):deprecation:使用了過時的類或者方法時的警告
2):unchecked 執行了未檢查的轉換時的警告
3):path 在類路徑,源文件路徑等有不存在的路徑是的警告
4):serial 當在可序列話的類缺少
5):serialVersionUID定義時的警告
6):finally 任何finally子句不能正常完成時的警告
7):all 關於以上所有情況的警告
元註解:(負責註解其他註解)
@Retention (主要參數:SOURCE CLASS RUNTIME)
@Target (主要參數:CONSTRUCTOR,FIELD,LOCAL_VARIABLE,METHOD,PACKAGE,PARAMETER,TYPE)
@Documented (jiang 將註釋包含在Javadoc中)
@Inhertied(允許子類繼承父類中的註釋)
看以下例子:
我們定義一個註解,這個註解作用於字段所有Target的value值是ElementType.FIELD
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface FiledAnnotation {
String userName() default "zhangming";
String passWord() default "123456";
String unit() default "android";
}
定義Person使用註解,並且通過註解給Person對象的字段賦值:
public class Person {
@FiledAnnotation(userName="小明")
public String userName;
@FiledAnnotation(passWord="abc123456")
public String passWord;
@FiledAnnotation(unit="java")
public String unit;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassWord() {
return passWord;
}
public void setPassWord(String passWord) {
this.passWord = passWord;
}
public String getUnit() {
return unit;
}
public void setUnit(String unit) {
this.unit = unit;
}
@Override
public String toString() {
return "Person [userName=" + userName + ", passWord=" + passWord + ", unit=" + unit + "]";
}
}
測試結果查看註解是否運行正確:
public static void main(String[] args) {
//創建一個Person對象
Person person = new Person();
//獲取person對象對應的字段數組
Field[] fields = person.getClass().getFields();
for(int i = 0;i < fields.length;i++){
//判斷當前是否是userName字段,並且userName字段中的的註解是否是FiledAnnotation
if(fields[i].isAnnotationPresent(FiledAnnotation.class) && fields[i].getName().equals("userName")){
FiledAnnotation filedAnnotation = fields[i].getAnnotation(FiledAnnotation.class);
System.out.println(filedAnnotation.userName());
//判斷當前是否是passWord字段,並且userName字段中的的註解是否是FiledAnnotation
}else if(fields[i].isAnnotationPresent(FiledAnnotation.class) && fields[i].getName().equals("passWord")){
FiledAnnotation filedAnnotation = fields[i].getAnnotation(FiledAnnotation.class);
System.out.println(filedAnnotation.passWord());
//判斷當前是否是unit字段,並且userName字段中的的註解是否是FiledAnnotation
}else if(fields[i].isAnnotationPresent(FiledAnnotation.class) && fields[i].getName().equals("unit")){
FiledAnnotation filedAnnotation = fields[i].getAnnotation(FiledAnnotation.class);
System.out.println(filedAnnotation.unit());
}
}
}
通過以上例子控制檯打印結果如下:
小明
abc123456
java
這裏我們在Person對象中用註解賦值當前對象字段,通過反射方法得到當前對象的字段名,然後調用isAnnotationPresent判斷當前字段是否包含FiledAnnotation註解,如果是,最後通過getAnnotation()方法獲取FiledAnnotation實例,最後輸出當前person對象註解賦予的值。