1、定义一个注解类:package com.gwolf;import java.lang.annotation.Retentio荏鱿胫协n;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;import static java.lang.annotation.ElementType.*;import static java.lang.annotation.ElementType.CONSTRUCTOR;import static java.lang.annotation.ElementType.LOCAL_VARIABLE;@Repeatable(MyAnnocations.class)@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE})@Retention(RetentionPolicy.RUNTIME)public @interface MyAnnocation { String value() default "gwolf";}

2、定义一个注解容器类package com.gwolf;坡纠课柩import java.lang.annotation.Ret髫潋啜缅ention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;import static java.lang.annotation.ElementType.*;@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE})@Retention(RetentionPolicy.RUNTIME)public @interface MyAnnocations { MyAnnocation[] value();}

3、定义重复注解:package com.gwolf;public class TestAnotation { @MyAnnocation("Hello") @MyAnnocation("World") public void show() { }}

4、类型注解关键字:TYPE_PARAMETER@Repeatable(MyAnnocations.class)@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE,TYPE_PARAMETER})@Retention(RetentionPolicy.RUNTIME)public @interface MyAnnocation { String value() default "gwolf";}

5、使用类型注解:public void test(@MyAnnocation("abc") String str) { Class<TestAnotation> clazz = TestAnotation.class; }

6、整体程序代码如下:package com.gwolf;import java.lang.reflect.Method;public class TestAnotation { @MyAnnocation("Hello") @MyAnnocation("World") public void show() { } public void test(@MyAnnocation("abc") String str) { Class<TestAnotation> clazz = TestAnotation.class; }}
