我只想通过使用@调度器注释定期运行我的spring方法。我已经指定了一些额外的代码,它将在启用REST服务之前执行一些预操作。
@EnableScheduling
@SpringBootApplication
public class SpringBootJDBCApp {
@Autowired
ITest testService;
public static void main(String args[]) throws Exception {
PersistenceValidation.cloneGit();
PersistenceValidation.dataPersistance();
PersistenceValidation.cleanUp();
ApplicationContext context = SpringApplication
.run(SpringBootJDBCApp.class);
ITest testService = context.getBean(ITestService.class);
testService.getAllData();
}
}我想每10秒运行一次上面的main方法。并在main方法中添加了@Schedule注释。但它抛出了一个例外:
按照doc @Scheduler的预期行为应该称为没有args[]的方法
我想在主方法中使用@Scheduler注释,如下所示:
@Scheduled(initialDelay = 1000, fixedRate = 10000)
public static void main(String args[]) throws Exception {
PersistenceValidation.cloneGit();
PersistenceValidation.dataPersistance();
PersistenceValidation.cleanUp();
ApplicationContext context = SpringApplication.run(SpringBootJDBCApp.class);
ITest testService = context.getBean(ITestService.class);
testService.getAllData();
}误差
创建名为‘springBootJDBCApp’的org.springframework.beans.factory.BeanCreationException:错误: bean初始化失败;嵌套异常是java.lang.IllegalStateException:遇到无效的@调度方法'main':只有no-arg方法可以使用@调度注释
还有其他方法来完成这一任务吗?我想定期运行主方法中提到的所有内容。
有什么线索吗?
发布于 2018-05-27 15:17:36
带有@Scheduled注释的计划方法必须没有参数,因为注释没有提供任何输入。@Scheduled sais的Spring文档:
带注释的方法必须不需要参数。它通常有一个空返回类型;如果不是,则在通过调度程序调用时将忽略返回的值。
您注释了方法public static void main(String args[]),它有一个数组作为参数。您只需将main(String args[])中的内容包装成不同的方法即可。请注意,您根本不使用args[]。
https://stackoverflow.com/questions/50553548
复制相似问题