首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在spring中,主方法中是否有调用@调度注释的方法?

在spring中,主方法中是否有调用@调度注释的方法?
EN

Stack Overflow用户
提问于 2018-05-27 15:10:27
回答 1查看 6.5K关注 0票数 2

我只想通过使用@调度器注释定期运行我的spring方法。我已经指定了一些额外的代码,它将在启用REST服务之前执行一些预操作。

代码语言:javascript
复制
@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注释,如下所示:

代码语言:javascript
复制
@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方法可以使用@调度注释

还有其他方法来完成这一任务吗?我想定期运行主方法中提到的所有内容。

有什么线索吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-05-27 15:17:36

带有@Scheduled注释的计划方法必须没有参数,因为注释没有提供任何输入。@Scheduled sais的Spring文档:

带注释的方法必须不需要参数。它通常有一个空返回类型;如果不是,则在通过调度程序调用时将忽略返回的值。

您注释了方法public static void main(String args[]),它有一个数组作为参数。您只需将main(String args[])中的内容包装成不同的方法即可。请注意,您根本不使用args[]

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50553548

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档