Spring Boot:从数据库中获取@Scheduled cron 值

编程入门 行业动态 更新时间:2024-10-26 08:30:04
本文介绍了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 ?

推荐答案

你可以在 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:关键词]

本文发布于:2023-03-27 04:40:31,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/679095.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据库中   Boot   Spring   Scheduled   cron

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!