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()));
}
}
浙公网安备 33010602011771号