我爱java系列---【springboot集成定时器】
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(); } }
愿你走出半生,归来仍是少年!