1. 开启定时器注解
@SpringBootApplication @EnableScheduling//开启定时器 public class Day01SpringbootIntergrationApplication { public static void main(String[] args) {
SpringApplication.run(Day01SpringbootIntergrationApplication.class, args); } }
2. 配置定时器方法
@Component public class TimerUtil { @Scheduled(initialDelay = 1000,fixedRate = 1000) public void mytask(){ System.out.println(LocalDateTime.now());//写自己要执行的任务
} }
3.测试类
@RunWith(SpringRunner.class) @SpringBootTest @Slf4j public class DbProxyApplicationTests { @Autowired private DbProxyApplication.TimerUtil timerUtil; @Test public void test2() { timerUtil.mytask(); } }