gistfile1.txt package com.example.demo;import org.springframework.scheduling.annotation.EnableScheduling;import org.springframework.scheduling.annotation.Scheduled;import org.springframework.stereotype.Component;import java.text.SimpleDateF
package com.example.demo; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import java.text.SimpleDateFormat; import java.util.Date; /** * Created by Administrator on 2017/11/25 0025. */ @Component//将MyTask对象注入spring容器 @EnableScheduling//启动定时任务 public class MyTask { @Scheduled(cron = "0/5 * * * * *")//通过cron表达式定义任务每5秒执行一次 public void task(){ SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); System.out.println("定时任务:"+simpleDateFormat.format(new Date())); } }