import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * @author: lihui
 * @date: 2020-05-20
 */
public class CompositeAnnotationStudy {
    public static void main(String[] args) {
        AnnotationTwo annotationTwo = ClassInAnnotation.class.getAnnotation(AnnotationTwo.class);
        System.out.println(annotationTwo.basePackages());
        AnnotationOne annotationOne = annotationTwo.annotationType().getAnnotation(AnnotationOne.class);
        System.out.println(annotationOne.value());

        System.out.println(annotationTwo.getClass().getAnnotation(AnnotationOne.class));
    }
}

@AnnotationTwo()
class ClassInAnnotation {
}

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@AnnotationOne 
@interface AnnotationTwo {
    String basePackages() default "basePackages";
}

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented 
@interface AnnotationOne {
    String value() default "value";
}

输出如下:

basePackages
value
null
可以看到:一个注解就可以得到许多种注解的信息。 
注意:
获得一个注解的注解不是通过这种方式:
annotation.getClass().getAnnotation(AnnotationOne.class);
而是通过以下方式:
annotation.annotationType().getAnnotation(AnnotationOne.class);

相关文章:

  • 2021-10-12
  • 2021-08-16
  • 2021-10-27
  • 2021-09-02
  • 2021-08-11
  • 2021-12-03
  • 2021-04-24
猜你喜欢
  • 2021-05-03
  • 2021-10-10
  • 2021-04-27
  • 2022-02-04
  • 2021-12-20
  • 2022-02-09
  • 2021-11-25
相关资源
相似解决方案