2015-10-07 50 views
6

春天的cron @Scheduled不能解決財產

我的Java代碼:

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

,並在我的屬性文件,我有invoice.export.cron: 0 0 7 * * MON-FRI?
,使調度我在我的主要配置類有@EnableScheduling

我試圖調試到這個問題,並發現cron表達式應該從財產佔位here解決。以下呼叫到resolveStringValue使我this位置爲AbstractBeanFactory。就我所知,這是問題所在。該this.embeddedValueResolvers列表爲空...因此它不能解決我傳遞給@Scheduled(cron)財產。

任何人有一個想法,如果我做錯了什麼或在這裏錯過了什麼?

在此先感謝! :)

回答

7

你有沒有註冊的PropertySourcesPlaceholderConfigurer? PlaceholderConfigurerSupport的

專業解析$ {...} bean定義的屬性值和@Value 註釋中 佔位符針對當前春季環境和一套 PropertySources。

我不知道,如果它的工作原理也@Scheduled,但它是值得一試

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

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

謝謝@Ruben這個工程。 :) – Dodge