Spring `@Scheduled` 直到第一次 HTTP 命中才开始?

Spring `@Scheduled` doesn't start until first HTTP hit?

我用两种方法定义了一个控制器:

@Scheduled(cron = "* * * * * *")
private void heartBeat() {
    logger.info("here");
}

@RequestMapping(value = "/now", method = RequestMethod.GET)
@ResponseBody
public String getDate() {
    return Instant.now().toString();
}

我的配置如下所示:

@Configuration
@EnableScheduling
@PropertySource("classpath:/application.properties")
@EnableAutoConfiguration
public class Config extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Config.class);
    }

当我第一次启动应用程序时,没有任何反应。

然后,当我点击 curl http://localhost/now 时,heartBeat() 方法开始定期执行。

为什么不立即开始?

看来我只是遗漏了一个 @ComponentScan 注释。

@Scheduled(cron = "* * * * * *")改为 @Scheduled(fixedRate = "* * * * * *")。 一旦应用程序启动,这将触发计划的方法。