问题描述
我正在使用 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 ?
推荐答案
你可以在 SpringBootApplication 主类或任何配置类中添加一个 bean 从数据库中获取 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屋!
更多推荐
[db:关键词]
发布评论