spring 定时计划.

<!-- 通知spring容器采用自动扫描机制查找注解的bean 日期规则从小到大-->
<context:component-scan base-package="com.*" />
<task:annotation-driven /> <!-- 定时器开关 -->
<bean id="email" class="com.ant.form.sendmessage.Email" />
<!-- 配置执行时间,不配置则按照@Scheduled定义的时间. -->
<task:scheduled-tasks>
<task:scheduled ref="email" method="print"
cron="*/5 * * * * ?" /> <!-- 此处参见Cron表达式 -->
</task:scheduled-tasks>

 

package com.ant.form.sendmessage;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class Email {
/**
* 每隔20秒执行一次
*/
@Scheduled(fixedRate = 1000*20)
public void print(){
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd H:m:s");
System.out.println("timer : "+format.format(new Date()));
}
}

posted on 2016-10-10 09:55  愿心如止水  阅读(71)  评论(0)    收藏  举报

导航