java spring boot 定时器
启动类加个
@EnableScheduling
package com.example.demo2122; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.scheduling.annotation.EnableScheduling; @SpringBootApplication @EnableScheduling public class Demo2122Application { public static void main(String[] args) { SpringApplication.run(Demo2122Application.class, args); } }
然后函数前面加个
@Scheduled(fixedRate = 5000)
package com.example.demo2122; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; import java.util.*; @RestController @Component public class HelloControl { @GetMapping("/hello") public String hello(@RequestParam(value = "name", defaultValue = "World") String name) { return "fwef"; } @Scheduled(fixedRate = 5000) public void scheduledTask1(){ System.out.println("5秒执行一次"); } }
然后。就跑起来了。。。
原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/18955.html