spring注解scheduled实现定时任务

只想说,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 }

三、运行查看结果

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

是不是很简单的呀

原文地址:https://www.cnblogs.com/July74/p/5672797.html