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 }

三、运行查看结果

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

是不是很简单的呀

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM