SpringBoot 注解相关工具类(AnnotationUtils、AnnotatedElementUtils...)

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Scope("singleton")
@Component
// 表明注解会被子类继承
@Inherited
public @interface SingletonComponent {
    @AliasFor(annotation = Component.class, attribute = "value")
    String value() default "";

    String name() default "";
}
@SingletonComponent(value = "parent/controller", name = "parent")
public class ParentController {

}

@SingletonComponent(value = "child/controller")
public class ChildController extends ParentController {

}
public class AnswerApp {
    public static void main(String[] args) throws Exception {
        // 父类拥有注解 SingletonComponent, 子类没有
        System.out.println("ParentController getAnnotation @SingletonComponent: " + AnnotationUtils.getAnnotation(ParentController.class, SingletonComponent.class));
        System.out.println("ChildController getAnnotation @SingletonComponent: " + AnnotationUtils.getAnnotation(ChildController.class, SingletonComponent.class));
        System.out.println();

        System.out.println("ParentController findAnnotation @SingletonComponent: " + AnnotationUtils.findAnnotation(ParentController.class, SingletonComponent.class));
        System.out.println("ParentController findAnnotation @SingletonComponent: " + AnnotationUtils.findAnnotation(ChildController.class, SingletonComponent.class));
        System.out.println();

        System.out.println("ParentController isAnnotated @SingletonComponent: " + AnnotatedElementUtils.isAnnotated(ParentController.class, SingletonComponent.class));
        System.out.println("ParentController getMergedAnnotation @SingletonComponent: " + AnnotatedElementUtils.getMergedAnnotation(ParentController.class, SingletonComponent.class));
        System.out.println("ChildController isAnnotated @SingletonComponent: " + AnnotatedElementUtils.isAnnotated(ChildController.class, SingletonComponent.class));
        System.out.println("ChildController getMergedAnnotation @SingletonComponent: " + AnnotatedElementUtils.getMergedAnnotation(ChildController.class, SingletonComponent.class));
        System.out.println();

        System.out.println("ParentController hasAnnotation @SingletonComponent: " + AnnotatedElementUtils.hasAnnotation(ParentController.class, SingletonComponent.class));
        System.out.println("ParentController findMergedAnnotation @SingletonComponent: " + AnnotatedElementUtils.findMergedAnnotation(ParentController.class, SingletonComponent.class));
        System.out.println("ChildController hasAnnotation @SingletonComponent: " + AnnotatedElementUtils.hasAnnotation(ChildController.class, SingletonComponent.class));
        System.out.println("ChildController findMergedAnnotation @SingletonComponent: " + AnnotatedElementUtils.findMergedAnnotation(ChildController.class, SingletonComponent.class));
	}
}
ParentController getAnnotation @SingletonComponent: @com.jaemon.aal.define.SingletonComponent(name=parent, value=parent/controller)
ChildController getAnnotation @SingletonComponent: @com.jaemon.aal.define.SingletonComponent(name=, value=child/controller)

ParentController findAnnotation @SingletonComponent: @com.jaemon.aal.define.SingletonComponent(name=parent, value=parent/controller)
ParentController findAnnotation @SingletonComponent: @com.jaemon.aal.define.SingletonComponent(name=, value=child/controller)

ParentController isAnnotated @SingletonComponent: true
ParentController getMergedAnnotation @SingletonComponent: @com.jaemon.aal.define.SingletonComponent(name=parent, value=parent/controller)
ChildController isAnnotated @SingletonComponent: true
ChildController getMergedAnnotation @SingletonComponent: @com.jaemon.aal.define.SingletonComponent(name=, value=child/controller)

ParentController hasAnnotation @SingletonComponent: true
ParentController findMergedAnnotation @SingletonComponent: @com.jaemon.aal.define.SingletonComponent(name=parent, value=parent/controller)
ChildController hasAnnotation @SingletonComponent: true
ChildController findMergedAnnotation @SingletonComponent: @com.jaemon.aal.define.SingletonComponent(name=, value=child/controller)
发布了152 篇原创文章 · 获赞 27 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/u010979642/article/details/103667788