本文主要是介绍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)
这篇关于SpringBoot 注解相关工具类(AnnotationUtils、AnnotatedElementUtils...)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!