spring @Scheduled with cron does not resolve property
Asked Answered
Z

1

6

I'm using spring framework v4.1.7 and have issues scheduling a cron task where i want to define the cron parameter in a property file.

My java code:

@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 on my main configuration class.

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

anyone has an idea if i'm doing something wrong or miss something here?

Thanks in advance! :)

Zacharyzacherie answered 7/10, 2015 at 14:46 Comment(0)
J
7

Have you registered a 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.

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

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

   @Bean
   public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
      return new PropertySourcesPlaceholderConfigurer();
   }
}
Juggle answered 7/10, 2015 at 17:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.