从Spring Boot application.yml文件中注入@Scheduled fixedRate值 [英] Inject @Scheduled fixedRate value from Spring Boot application.yml file

查看:616
本文介绍了从Spring Boot application.yml文件中注入@Scheduled fixedRate值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我可以使用以下语法从属性文件中注入值:

I know I can inject the value from properties file with the following syntax:

@Scheduled(fixedRate=${myRate})
public void getSchedule(){
    System.out.println("in scheduled job");
}

但是,如果配置位于YAML文件中,我无法猜测如何完成相同的工作.

However I can't guess how to accomplish the same if the configuration is in YAML file.

预先感谢

推荐答案

在我的application.properties(YAML)中,我将其放置

In my application.properties (YAML) I put this

console:
    fetchMetrics: 5000

然后在我的简单Task类中推送定义:

Then in my simple Task class I push the definition :

@Scheduled(fixedRateString ="${console.fetchMetrics}", initialDelay=1000)
public void fetchMetrics() {
    logger.info("What's up ?");
}

请注意,fixedRate需要一个long,并且要注入占位符,您将需要fixedRateString

Please notice that fixedRate expects a long and you want to inject a placeholder, you will need fixedRateString

这篇关于从Spring Boot application.yml文件中注入@Scheduled fixedRate值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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