publicclassHello { @MyAnnotation(name = "qrrk") publicvoidsayHello(){ System.out.println("hello"); } } @Documented//添加到javadoc中 @Target(ElementType.METHOD)//指定MyAnnotation注解可以修饰什么 /*{ TYPE:类, 接口 (包括注解类型), 或者枚举 FIELD:字段声明(包括枚举常量) METHOD:方法声明 PARAMETER:参数声明 CONSTRUCTOR:构造器声明 LOCAL_VARIABLE:局部变量声明 ANNOTATION_TYPE: PACKAGE:注释类型声明 TYPE_PARAMETER:类型参数声明 TYPE_USE:类型使用 }*/ @Retention(RetentionPolicy.RUNTIME)//指定策略属性 /*{ SOURCE annotations are to be discarded by the compiler CLASS Annotations are to be recorded in the class file by the compiler but need not be retained by the VM at run time. (This is the default behavior). RUNTIME Annotations are to be recorded in the class file by the compiler and retained by the VM at run time, so they may be read reflectively. }*/ @interface MyAnnotation { String name()default""; }