/** * 注意使用Retention,只有指定Retention为RetentionPolicy.RUNTIME * 时在运行时才能获得注解信息 */ @Retention(value = RetentionPolicy.RUNTIME) public @interface Test { String name() default "tian"; int age() default 12; } //使用自定义注解 @Test(name = "song", age = 27) public class Person { } //利用反射获取注解信息 public static void main(String[] args) { Annotation[] annotations = Person.class.getAnnotations(); for (Annotation annotation : annotations) { if (annotation instanceof Test) { log(((Test) annotation).name()); } } }