我正在使用最新版本的Spring。当新年的年份发生变化时,我需要执行一些数据库操作。有什么方法可以启动基于 Spring 的事件来检测年份是否已更改?
谢谢。
最佳答案
正如我在评论中所说,这可以通过使用 @Scheduled 来实现注解。 计划注释允许我们定义一个 cron 表达式,该表达式将确定何时调用使用此注释注释的方法。来自文档:
cron
A cron-like expression, extending the usual UN*X definition to include triggers on the second as well as minute, hour, day of month, month and day of week.
例如,方法
@Scheduled(cron = "0 0 0 1 1 *")
public void doStuffOnFirstOfJanuary() {
System.err.println("This is called");
}
将于 1 月第一天的 0 小时 0 分 0 秒调用。
为了使此注释起作用,您必须在应用程序中启用调度,可以使用 @EnableScheduling
注释或添加 <task:annotation-driven/>
元素(如果您使用 xml 配置)。
关于java - Spring Activity 将于元旦起火,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50749441/