@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Component {
String value() default "";
}
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Configuration {
String value() default "";
}
我有一个对象被@Configuration注解,那我怎么知道它还是被Component注解的?@Interface 和接口Annotation是什么关系?
有一个注解了@Configuration的对象, obj.getClass().getAnnotation(Configuration.class).getClass()==Configuration.class 结果是false, obj.getClass().getAnnotation(Configuration.class) instanceof Configuration为true, obj.getClass().getAnnotation(Configuration.class) instanceof Component为false
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
Annotation也可以获取自身的注解。@interface是声明注解的关键字(与class/interface一样)obj.getClass().getAnnotation(Configuration.class).getClass() == Configuration.class引用不同结果为false是正常情况。可以使用这种判断obj.getClass().getAnnotation(Configuration.class).getClass().getName().equals(Configuration.class.getName())@Component只是@Configuration的注解声明,并不表示@Configuration就是@Component类型另外上面这种用法叫
Meta-Annotations在 spring-4.1.x 之后的的版本都支持这个功能,可以自定义Annotation,声明Meta-Annotations即可拥有相对的功能。beans-meta-annotations
Spring Annotation Programming Model
注:
Meta-Annotations并不是Annotation的原生功能,只是在spring中实现了这种机制。如果单纯的使用原生的Annotation则需要自己解析Meta-Annotations实现相应的功能才行。我有一个对象被@Configuration注解,那我怎么知道它还是被Component注解的?
@Interface 和接口Annotation是什么关系?
没听懂
讲道理getAnnotation(xx)以后直接去判断应该就可以了吧