1). @Retentionjava
表示須要在什麼級別保存該註釋信息,用於描述註解的生命週期,也是一個枚舉RetentionPoicy來決定的,這個枚舉我不列出來了,包括這個註解的具體怎麼決定註解的生命週期我也很少講,由於根據小弟這麼多年使用的經驗,都是填的RetentionPoicy.RUNTIME,填這個值註解處理器才能經過反色拿到註解信息,實現本身的語義,因此你們都填RetentionPoicy.RUNTIME就能夠了,想擴展瞭解的自行google..google
2). @Documentedspa
若是用javadoc生成文檔時,想把註解也生成文檔,就帶這個。(話說老鐵們都是怎麼寫文檔的,小弟表示從不寫文檔... ^_^)繼承
3). @Inherited接口
@Inherited 元註解是一個標記註解,@Inherited闡述了某個被標註的類型是被繼承的。若是一個使用了@Inherited修飾的annotation類型被用於一個class,則這個annotation將被用於該class的子類。注意,@Inherited annotation類型是被標註過的class的子類所繼承。類並不從它所實現的接口繼承annotation,方法並不從它所重載的方法繼承annotation。生命週期
其實這幾個註解只有一個有點用,就是@Target,你們稍微注意下就好了。註解不是你想加哪就加哪的。ip
public class Test {
public static void main(String[] args) {
TestVo testVo = new TestVo();
testVo.setId(11);
reflect(testVo,TestVo.class);
}
private static void reflect(TestVo testVo, Class<?> testVoClass) {
Method[] declaredMethods =
testVoClass.getDeclaredMethods();
for (Method declaredMethod : declaredMethods) {
TestAnnotation annotation = declaredMethod.getAnnotation(TestAnnotation.class);
if (annotation != null) {
System.out.println("Found Use Case:" + annotation.value() + " "
+ annotation.description());
}
}
}
}
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface TestAnnotation {
public String value() default "1";
public String description() default "no description";
}
public class TestVo { private String masg; private Integer id; @TestAnnotation(description = "aaaaaaa") public Integer getId() { return id; }}