How do you configure the timing of spring scheduled tasks?

There are two ways to configure scheduled tasks in Spring: annotation-based and XML-based configurations.

  1. Commentary method:
  2. Add the @Scheduled annotation to the method in the scheduled task.
  3. Specify the time expression for task execution using the cron attribute. For example, @Scheduled(cron = “0 0 0 * * ?”) signifies the task will be executed at midnight every day.
  4. XML configuration method:
  5. Add the following code to the Spring configuration file:




  6. Specify the time expression for task execution using the cron attribute.

The meanings of each field in the cron expression in the above two ways are as follows:

秒(0-59) 分钟(0-59) 小时(0-23) 日期(1-31) 月份(1-12) 星期(1-7或SUN-SAT) 年份(可选,留空表示任意年份)

For example, the cron expression “0 0 0 * * ?” means that the task will run every day at midnight. For more information on using cron expressions, please refer to the relevant documentation.

bannerAds