Spring定时器的使用详解
睡前没什么事儿,来祸害一下园子~~虽然我菜,但是我不会承认啊,哈哈哈
明天详细补充点儿吧,很晚了,不睡觉的程序员不是好程序员,我总能给自己找借口~~~
1 //spring开启task注解扫描就OK了
2 //<context:component-scan base-package="com.cityhero.dao,com.cityhero.task"/>
3 //<task:annotation-driven/>
4
5 //搞一个BaseTask,常用的都扔进去
6 package com.cityhero.task;
7
8 import java.text.DateFormat;
9 import java.text.SimpleDateFormat;
10
11 import org.apache.log4j.Logger;
12 import org.springframework.stereotype.Component;
13
14 @Component
15 public class BaseTask {
17 protected static final DateFormat date_format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
18 }
19
20 //嗯....再搞一个测试类,继承BaseTask,配合一下.
21 package com.cityhero.task;
22
23 import java.util.Date;
24
25 import org.springframework.scheduling.annotation.Scheduled;
26 import org.springframework.stereotype.Component;
27
28 @Component
29 public class TaskTest extends BaseTask {
30
31 @Scheduled(cron="0/5 * * * * ?") //五秒执行一次
32 public void sayHellow() {
33 Date now = new Date();
34 logger.info("Hello.My English is pool . . ." + date_format.format(now));
35 }
36 }
//启动起来就可以了,怎么样,是不是超级简单呢~对于我这种菜B来说,可以摆脱quartz啦!开心~
//INFO [pool-1-thread-1] - Hello.My English is pool . . .2017-05-10 00:31:05
//INFO [pool-1-thread-1] - Hello.My English is pool . . .2017-05-10 00:31:10
//INFO [pool-1-thread-1] - Hello.My English is pool . . .2017-05-10 00:31:15