原代码
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;@Component
public class TestScheduler {@Scheduled(cron = "0/5 * * * * ?")public void demo1() {System.out.println("==========");}
}
启动后发现定时任务未执行
添加@EnableScheduling注解
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;@Component
@EnableScheduling
public class TestScheduler {@Scheduled(cron = "0/5 * * * * ?")public void demo1() {System.out.println("==========");}
}
@EnableScheduling注解可以让Spring可以进行任务调度,功能类似于Spring的xml命名空间<task:*>参考:https://blog.csdn.net/u011734144/article/details/52849868