只想说,spring注解scheduled实现定时任务使用真的非常简单。

一、配置spring.xml文件

  1、在beans加入xmlns:task="http://www.springframework.org/schema/task"以及在xsi:schemaLocation中加入

  http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd

  2、task任务扫描注解<task:annotation-driven />

  3、配置扫描的位置 <context:component-scan base-package="com.test"/>  

二、java代码的实现

1 @Component// 实现定时任务的类必须被@Component注解
2 public class TestScheduled {
3 
4     @Scheduled(cron = "0 1 * * * ? ")
5     public void test() {// 定时器的任务方法不能有返回值
6         System.out.println("每分钟执行一次");
7     }
8 }

三、运行查看结果

   当然是每分钟打印一次喽!

是不是很简单的呀

 

相关文章:

  • 2022-02-17
  • 2021-10-15
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-09-16
  • 2021-05-20
  • 2022-12-23
  • 2021-07-09
  • 2021-10-04
  • 2021-08-11
  • 2021-10-08
相关资源
相似解决方案