Java 获取 @Scheduled注解
时间: 2023-11-14 14:09:16 浏览: 79
可以通过反射获取到带有 @Scheduled 注解的方法,然后再获取注解的属性值。具体实现可以参考以下代码:
```java
import org.springframework.scheduling.annotation.Scheduled;
import java.lang.reflect.Method;
public class ScheduledAnnotationUtils {
public static void main(String[] args) throws NoSuchMethodException {
Class<?> clazz = ScheduledTask.class;
Method method = clazz.getMethod("task");
Scheduled scheduled = method.getAnnotation(Scheduled.class);
String cron = scheduled.cron();
System.out.println(cron);
}
static class ScheduledTask {
@Scheduled(cron = "0/5 * * * * ?")
public void task() {
System.out.println("task");
}
}
}
```
输出结果为:`0/5 * * * * ?`
阅读全文