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)
發佈了159 篇原創文章 · 獲贊 28 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章