SpringBoot中執行定時任務

一:在SpringBoot中使用定時任務至關的簡單。首先,咱們在啓動類中加入@EnableScheduling來開啓定時任務。html

1 @SpringBootApplication
2 @EnableScheduling//容許定時任務
3 public class DemoApplication {
4 
5     public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); }
6 
7 
8 }

二:建立實現定時任務的Service便可 。java

//定時任務管理器
@Component
public class QuartzService {
    @Autowired
    MemberRepository memberRepository;
    @Autowired
    MemberRepository2 memberRepository2;

    //    每分鐘啓動
    @Scheduled(cron = "0 0/1 * * * ?")
    public void timerToNow() {
        System.out.println("now time:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
    }

}

 

補充知識:spa

cron表達式詳解---https://www.cnblogs.com/javahr/p/8318728.htmlcode

在線生成Cron表達式---http://cron.qqe2.com/orm

相關文章
相關標籤/搜索