在Spring Boot中将基于YAML的属性值传递给@Scheduled批注

Luc*_*ano 4 spring spring-boot

这个问题可能是这个较旧问题的重复。

我正在使用Spring Boot 1.4应用程序,并且有一种用注释的bean方法@Scheduled。我需要将cron值传递给注释,并且由于我使用的是基于YAML的配置,因此cron值存储在YAML文件(application.yaml)中。

我找不到将属性值传递app.cron给注释的方法。

例如,这不起作用

@Scheduled(cron = ${app.cron})
Run Code Online (Sandbox Code Playgroud)

我也尝试过使用EL表达式,但是没有运气。

将基于YAML的属性值传递给Spring注释的正确方法是什么?

Den*_*Ich 5

尝试先将其放在Javaconfig中,它应该与EL一起使用:

@Configuration
@ConfigurationProperties(prefix = "app")
public class CronConfig() {
    private String cron;

    @Bean
    public String cron() {
        return this.cron;
    } 

    public void setCron(String cron) {
       this.cron = cron;
    }
}
Run Code Online (Sandbox Code Playgroud)

并与 @Scheduled(cron = "#{@cron}")

我没有为计划的Taks尝试过此操作,但是在注解中注入内容时遇到了类似的问题。