Spring Boot:从数据库获取@Scheduled cron值 [英] Spring Boot : Getting @Scheduled cron value from database

查看:1502
本文介绍了Spring Boot:从数据库获取@Scheduled cron值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Spring Boot ,并且在使用数据库中现有的值计划 cron任务时遇到问题。

I'm using Spring Boot and have issues scheduling a cron task using values existing in database.

目前,我正在从如下属性文件中读取值:

For the time being, I'm reading values from properties file like below :

@Scheduled(cron= "${time.export.cron}")
public void performJob() throws Exception {
   // do something
}

这很好用,但是我不是从属性文件中获取值,而是从数据库表中获取它们。

This works nicely, but instead of getting values from properties file, I want to get them from database table. Is it possible and how ?

推荐答案

您可以添加一个bean,以从SpringBootApplication主类或任何其他类的数据库中获取cron值。配置类。下面是示例代码:

you can add a bean to get cron value from database in the SpringBootApplication main class or in any of the configuration class. Example code is below:

@Autowired
private CronRepository cronRepo;

@Bean
public int getCronValue()
{
    return cronRepo.findOne("cron").getCronValue();
}

您应该创建一个表并在数据库中提供合适的值。之后,您可以在 @Scheduled 中提供bean。示例代码如下:

you should create a table and provide suitable values in the database. After that you can provide the bean inside the @Scheduled. Example code is below:

@Scheduled(cron="#{@getCronValue}")

希望它能解决您的问题。

Hope it works for your issue.

这篇关于Spring Boot:从数据库获取@Scheduled cron值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆