繁体   English   中英

如何确保方法中存在@Scheduled批注

[英]How to make sure the @Scheduled annotation is present on the method

在我们的Spring Boot应用程序中,我们有一个带有@Scheduled批注的方法:

@Scheduled(fixedRate = 1000L * 60 * 10, initialDelay = 1000L * 60 * 5)
   public void doSomethingFromTimeToTime(){....}

在我们的Maven构建测试阶段,我们希望确保:

  1. 注释在那里(例如未被开发人员误删除),并且
  2. 包含这些确切值

有没有办法在单元/集成测试中对此进行测试? 还是在构建阶段用另一种方式?

我们不需要测试Spring的Scheduler本身,只需确保注释在那里。

可以测试的注释存在一个上Class使用对象.getAnnotation(Class<A> annotationType)所述的方法Class ,其中A是一个通用的类型参数,正如T通常是。

用法示例:

Scheduled scheduledAnnotation = MySpringScheduler.class.getAnnotation(Scheduled.class).

有了注释后,就可以像使用普通类一样使用注释提供的方法访问字段:

scheduledAnnotation.fixedDelay();

我想通过这种方式您可以使用反射轻松获得它

Class<?> cls = Class.forName(name);

    cls.getDeclaredAnnotations(); // get all annotation
    (cls.getDeclaredMethods()[0]).getAnnotations(); //get annotation of a method
    Annotation ety = cls.getAnnotation(Annotation.class); // get annotation of particular annotation class

ref 如何在Java中使用Reflection获得注释的值

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM