Spring @Scheduled cron does not resolve property

I am using spring framework v4.1.7 and have problems scheduling a cron task where I want to define the cron parameter in the properties file.

My Java code is:

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

and in my properties file I have invoice.export.cron: 0 0 7 * * MON-FRI? To enable scheduling, I have @EnableScheduling in my main configuration class.

I tried to debug this problem and found that the cron expression should be resolved from the placeholder property here . after calls to resolveStringValue me to this place in AbstractBeanFactory . And as far as I can see, here is the problem. the list this.embeddedValueResolvers empty ... therefore it does not resolve the property i passed to @Scheduled(cron) .

Does anyone have an idea if I am doing something wrong or have missed something?

Thanks in advance!:)

+6
source share
1 answer

Have you registered PropertySourcesPlaceholderConfigurer ?

A specialization of PlaceholderConfigurerSupport that resolves $ {...} placeholders in the values โ€‹โ€‹of the definition properties of bean and @Value annotations to the current Spring environment and its set of PropertySources.

I'm not sure if it works in @Scheduled , but it's worth a try

 @Configuration @PropertySource("classpath:whatever.properties") public class PropertiesWithJavaConfig { @Bean public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() { return new PropertySourcesPlaceholderConfigurer(); } } 
+7
source

All Articles