Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spring @Scheduled with cron does not resolve property

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! :)

like image 491
Dodge Avatar asked Oct 07 '15 14:10

Dodge


1 Answers

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();
   }
}
like image 165
Ruben Avatar answered Sep 27 '22 22:09

Ruben