xml 配置
xmlns:task="http://www.springframework.org/schema/task"
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd
<task:annotation-driven />
代码配置
import java.text.SimpleDateFormat; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; @Component public class Scheduling { private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss"); public Scheduling(){ System.out.println(dateFormat); } //@Scheduled(fixedDelay=1000) //第一种方式 //fixedDelay延时多少毫秒,多少毫秒执行一次 //@Scheduled(cron="0 * * * * *") //第二种方式 //1 Seconds (0-59) //2 Minutes (0-59) //3 Hours (0-23) //4 Day of month (1-31) //5 Month (1-12 or JAN-DEC) //6 Day of week (1-7 or SUN-SAT) //7 Year (1970-2099) //取值:可以是单个值,如6; // 也可以是个范围,如9-12; // 也可以是个列表,如9,11,13 // 也可以是任意取值,使用* //0 * * * * * 代表每分钟执行一次 //•"0 0 * * * *" = the top of every hour of every day. //•"*/10 * * * * *" = every ten seconds. //•"0 0 8-10 * * *" = 8, 9 and 10 o'clock of every day. //•"0 0/30 8-10 * * *" = 8:00, 8:30, 9:00, 9:30 and 10 o'clock every day. //•"0 0 9-17 * * MON-FRI" = on the hour nine-to-five weekdays //•"0 0 0 25 12 ?" = every Christmas Day at midnight @Scheduled(fixedDelay=1000) public void SendToUser(){ System.out.println("给用户发送信息"); } }