1. 自定义注解类 package com.jushin; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) // 这样在反射的时候才能获得到, 默认是class @Target(ElementType.METHOD) public @interface AnnotationTest { String name() default "zhugx"; int age() default 26; } 2.使用和获得注解类的信息 package com.jushin; import java.lang.reflect.Method; public class Test { @AnnotationTest(name="zhangsan", age=30) public void say(){ System.out.println("---hahaa----"); } // 获得注解信息 public static void main(String[] args)throws Exception { Class myClass = Class.forName("com.jushin.Test"); Method method = myClass.getMethod("say", new Class[]{}); //Test test = new Test(); //method.invoke(test,new Object[]{}); boolean isAnnotation = method.isAnnotationPresent(AnnotationTest.class); if(isAnnotation){ AnnotationTest annotationTest = method.getAnnotation(AnnotationTest.class); //System.out.println(annotationTest.annotationType().getName()); System.out.println(annotationTest.name()); System.out.println(annotationTest.age()); } } }