2

团队,

我已将 cron 表达式放在属性文件中。然后我尝试从 java 文件中引用该 cron 表达式,如图所示。

    @Scheduled(cron=  "${cron.expression}" )
    public void test(){
    ...
    }

它向我抛出了以下错误:

Cron expression must consist of 6 fields (found 1 in "${cron.expression}"

我是这个调度程序的新手。请就如何使这个表达式可配置提出建议。

谢谢

4

1 回答 1

3

我在我的 AppConfig java 文件中添加了以下内容。

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

如果没有 PropertySourcesPlaceholderConfigurer,我们只能使用 Autowired 环境变量访问属性文件。但是使用 PropertySourcesPlaceholderConfigurer,我们可以使用 ${..} 来使用属性文件变量。

有了这个逻辑,我的代码开始像魅力一样工作。

感谢您的回复@SB 和@RaphaelRoth。

于 2016-04-05T07:33:57.640 回答