我爱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();
    }
}

 

posted on 2019-12-20 15:29  少年攻城狮  阅读(329)  评论(0编辑  收藏  举报

导航