Spring的Task定时器

1.开启注解,定义扫描包
<mvc:annotation-driven/>
<context:annotation-config />
<context:component-scan base-package="com.*" />
2.开启Task注解
<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-3.2.xsd">
<task:annotation-driven/>
3.撰写Task
@Service
public class TestTask {
	Log log = LogFactory.getLog(TestTask.class);
        //每隔五秒中执行一次
	@Scheduled(cron = "0/5 * * * * ? ")
	public void task(){
		String time = DateUtil.getTime();
		log.info("==========now:"+time);
	}
}

Cron表达式可以参考: http://www.blogjava.net/javagrass/archive/2011/07/12/354134.html
4.启动web,查看控制台输出
2016-05-31 10:33:05 -10116 [pool-1-thread-1] INFO    - ==========now:2016-05-31 10:33:05
2016-05-31 10:33:10 -15084 [pool-1-thread-1] INFO    - ==========now:2016-05-31 10:33:10
2016-05-31 10:33:15 -20084 [pool-1-thread-1] INFO    - ==========now:2016-05-31 10:33:15
2016-05-31 10:33:20 -25084 [pool-1-thread-1] INFO    - ==========now:2016-05-31 10:33:20

你可能感兴趣的:(spring,Web,mvc)