6

我正在使用 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

我试图调试这个问题,发现应该从这里的属性占位符中解析 cron 表达式。跟随呼叫进入resolveStringValue将我带到这个位置进入AbstractBeanFactory。据我所知,这就是问题所在。该this.embeddedValueResolvers列表是空的......因此它不能解析我传递给的属性@Scheduled(cron)

任何人都知道我是否做错了什么或错过了什么?

提前致谢!:)

4

1 回答 1

7

你注册了PropertySourcesPlaceholderConfigurer吗?

PlaceholderConfigurerSupport 的特殊化,它针对当前 Spring 环境及其一组 PropertySource 解析 bean 定义属性值和 @Value 注释中的 ${...} 占位符。

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

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

   @Bean
   public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
      return new PropertySourcesPlaceholderConfigurer();
   }
}
于 2015-10-07T17:57:32.913 回答