在应用程序启动后和午夜后运行的 Spring 调度程序

编程入门 行业动态 更新时间:2024-10-24 14:28:24
本文介绍了在应用程序启动后和午夜后运行的 Spring 调度程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何描述在应用程序启动后和 00:00 后运行的 Spring 调度程序?

How to describe Spring scheduler which is run after application is started and after 00:00 ?

推荐答案

我会用两个独立的结构来做到这一点.

I would do this with two separate constructs.

应用程序启动后,使用@PostConstuct,每晚午夜使用@Scheduled 并设置cron 值.两者都应用于一个方法.

For after the application starts, use @PostConstuct, and for every night at midnight use @Scheduled with the cron value set. Both are applied to a method.

public class MyClass {
    @PostConstruct
    public void onStartup() {
        doWork();
    }

    @Scheduled(cron="0 0 0 * * ?")
    public void onSchedule() {
        doWork();
    }

    public void doWork() {
        // your work required on startup and at midnight
    }
}

这篇关于在应用程序启动后和午夜后运行的 Spring 调度程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-03-27 04:28:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/679074.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:应用程序   午夜   程序   Spring

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!