一、用到的注解 @Scheduled @EnableScheduling
1-2需要执行的类加上 @Scheduled(cron = "0 0/1 * * * ?")
package com.atzhongruan.demo; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import java.util.Date; @Component public class TestController { @Scheduled(cron = "0 0/1 * * * ?") public void pushDataScheduled(){ System.out.println("当前时间为"+new Date()); System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"); } }
2-2 在启动的类上开启加上 @EnableScheduling
package com.atzhongruan.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.scheduling.annotation.EnableScheduling; @EnableScheduling @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }