1.註解的定義
@Documented:一個簡單的Annotations標記註解,表示是否將註解信息添加在java文檔中。
@Retention(RetentionPolicy.RUNTIME):
RetentionPolicy.SOURCE – 在編譯階段丟棄。這些註解在編譯結束之後就不再有任何意義,所以它們不會寫入字節碼。@Override, @SuppressWarnings都屬於這類註解。
RetentionPolicy.CLASS – 在類加載的時候丟棄。在字節碼文件的處理中有用。註解默認使用這種方式。
RetentionPolicy.RUNTIME– 始終不會丟棄,運行期也保留該註解,因此可以使用反射機制讀取該註解的信息。我們自定義的註解通常使用這種方式。
@Target(ElementType.FIELD):單個參數
@Target({ElementType.FIELD,ElementType.METHOD}):多個參數用大括號
ElementType.TYPE:用於描述類、接口或enum聲明
ElementType.FIELD:用於描述實例變量
ElementType.METHOD
ElementType.PARAMETER
ElementType.CONSTRUCTOR
ElementType.LOCAL_VARIABLE
ElementType.ANNOTATION_TYPE 另一個註釋
ElementType.PACKAGE 用於記錄java文件的package信息
@Inherited:
Inherited作用是,使用此註解聲明出來的自定義註解,在使用此自定義註解時,如果註解在類上面時,子類會自動繼承此註解,
否則的話,子類不會繼承此註解。這裏一定要記住,使用Inherited聲明出來的註解,只有在類上使用時纔會有效,對方法,屬性等其他無效。
package com.order.orderSystem.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**一個簡單的Annotations標記註解,表示是否將註解信息添加在java文檔中。*/
@Documented
/**
* RetentionPolicy.SOURCE – 在編譯階段丟棄。這些註解在編譯結束之後就不再有任何意義,所以它們不會寫入字節碼。@Override, @SuppressWarnings都屬於這類註解。
* RetentionPolicy.CLASS – 在類加載的時候丟棄。在字節碼文件的處理中有用。註解默認使用這種方式。
* RetentionPolicy.RUNTIME– 始終不會丟棄,運行期也保留該註解,因此可以使用反射機制讀取該註解的信息。我們自定義的註解通常使用這種方式。
*/
@Retention(RetentionPolicy.RUNTIME)
/**
* ElementType.TYPE:用於描述類、接口或enum聲明
* ElementType.FIELD:用於描述實例變量
* ElementType.METHOD
* ElementType.PARAMETER
* ElementType.CONSTRUCTOR
* ElementType.LOCAL_VARIABLE
* ElementType.ANNOTATION_TYPE 另一個註釋
* ElementType.PACKAGE 用於記錄java文件的package信息
*/
@Target({ElementType.FIELD,ElementType.METHOD})
/**
* Inherited作用是,使用此註解聲明出來的自定義註解,在使用此自定義註解時,如果註解在類上面時,子類會自動繼承此註解,
* 否則的話,子類不會繼承此註解。這裏一定要記住,使用Inherited聲明出來的註解,只有在類上使用時纔會有效,對方法,屬性等其他無效。
*/
@Inherited
public @interface FieldAnnotation {
public enum PASSWORD {LOW, MEDIUM, HIGH}
PASSWORD value() default PASSWORD.LOW;
String type();
}
2.註解的應用
註解方法有default則可以不傳值,多個值方法@FieldAnnotation(value=PASSWORD.MEDIUM,type="password")
package com.order.orderSystem.bean;
import com.order.orderSystem.annotation.FieldAnnotation;
import com.order.orderSystem.annotation.FieldAnnotation.PASSWORD;
public class UserInfo {
@FieldAnnotation(value=PASSWORD.MEDIUM,type="password")
private String password;
@FieldAnnotation(type="用戶信息")
private String getUser(String str){
return str;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
測試用例,用反射獲取註解內容
package com.order.test;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import org.junit.Test;
import com.order.orderSystem.annotation.FieldAnnotation;
import com.order.orderSystem.bean.UserInfo;
public class AnnotationTest {
@Test
public void fieldTest() throws Exception, SecurityException{
Class<UserInfo> clazz = UserInfo.class;
Field field = clazz.getDeclaredField("password");
FieldAnnotation fieldAnnotation = field.getAnnotation(FieldAnnotation.class);
System.out.println("field註解:value:"+fieldAnnotation.value()+",type:"+fieldAnnotation.type());
Method method = clazz.getDeclaredMethod("getUser", String.class);
fieldAnnotation = method.getAnnotation(FieldAnnotation.class);
System.out.println("method註解:value:"+fieldAnnotation.value()+",type:"+fieldAnnotation.type());
}
}