spring注解scheduled实现定时任务

来源:互联网 发布:淘宝评论违禁词有哪些 编辑:程序博客网 时间:2024/06/11 03:03

只想说,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 }
复制代码

三、运行查看结果

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

是不是很简单的呀

 

0 0
原创粉丝点击