Spring boot Spring`@Scheduled`在第一次HTTP命中之前不会启动吗?
我用两种方法定义了控制器:Spring boot Spring`@Scheduled`在第一次HTTP命中之前不会启动吗?,spring-boot,spring-mvc,spring-scheduled,Spring Boot,Spring Mvc,Spring Scheduled,我用两种方法定义了控制器: @Scheduled(cron = "* * * * * *") private void heartBeat() { logger.info("here"); } @RequestMapping(value = "/now", method = RequestMethod.GET) @ResponseBody public String getDate() { return Instant.now().toString(); } 我的配置如下所示
@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);
}
当我第一次启动应用程序时,什么都没有发生
然后,我一碰到卷发http://localhost/now,heartBeat方法开始定期执行
为什么不立即启动?看起来我只是缺少了@ComponentScan注释。将@Scheduledcron=*****更改为 @ScheduledExedRate=**。
一旦应用程序启动,这将触发计划的方法。是否添加了@EnableScheduling?是的,在我的配置中,请查看带有@Component注释的新类中的updatewrite计划程序代码,然后重试。@Component已经存在,但我缺少@ComponentScan