spring @Scheduled with cron 无法解决 属性

spring @Scheduled with cron does not resolve property

我正在使用 spring 框架 v4.1.7,在安排 cron 任务时遇到问题,我想在 属性 文件中定义 cron 参数。

我的java代码:

@Scheduled(cron = "${invoice.export.cron}")
private void scheduledExport() {
    // ... the code to execute ...
}

在我的属性文件中有 invoice.export.cron: 0 0 7 * * MON-FRI?
为了启用计划,我的主要配置 @EnableScheduling class。

我尝试调试这个问题,发现 cron 表达式应该从 属性 占位符 here. following the calls into resolveStringValue brings me to this 位置解析到 AbstractBeanFactory。据我所知,这就是问题所在。 this.embeddedValueResolvers 列表为空...因此它无法解析我传递给 @Scheduled(cron).

的 属性

有人知道我做错了什么或遗漏了什么吗?

提前致谢! :)

你注册了吗PropertySourcesPlaceholderConfigurer

Specialization of PlaceholderConfigurerSupport that resolves ${...} placeholders within bean definition property values and @Value annotations against the current Spring Environment and its set of PropertySources.

我不确定它是否也适用于 @Scheduled,但值得一试

@Configuration
@PropertySource("classpath:whatever.properties")
public class PropertiesWithJavaConfig {

   @Bean
   public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
      return new PropertySourcesPlaceholderConfigurer();
   }
}