0

When I was working on a Spring Boot application that used scheduling and acted as a config client, I was unable to pull the crontab expression from the application-cloud.yml file.

This:

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

did not work.

Putting the cron.expression in the jar's internal application.yml was not an option. As a workaround, I changed the source of that property to the appropriate environment variable, CRON_EXPRESSION.



Does anyone know why I had to do that? Why was Spring Scheduled unable to resolve an externalized configuration property?

Edit:

Here is the bootstrap.yml that the project was setup with:

spring:  
  profiles: cloud
  application:
    name: application-name
  cloud:
    config:
      uri: https://config-server.com
      fail-fast: false
Matthew K.
  • 56
  • 4
  • Are you sure that the .yml file was actually loaded and taking precedence over other values? – kasoban Jun 16 '21 at 15:22
  • @kasoban Yes. The same external, cloud yaml was used to populate the ConfigurationProperties classes. From examining logs, it appeared that the cloud configuration was not loaded until after the Scheduled's initialization. The Scheduled's Bean Exception was thrown before the cloud props were brought into scope. – Matthew K. Jun 16 '21 at 18:12
  • I'm using @Scheduled with externalized configuration in my project and it works fine. Did you put `bootstrap.yml` file correctly to boot the project? – Lebecca Jun 17 '21 at 04:21
  • @Lebecca We did use a bootstrap.yml file. I'm not sure if we used it correctly. I will update the question. – Matthew K. Jun 17 '21 at 16:11
  • For the usage of `bootstrap.yml`, you may have a look at my two cents [here](https://stackoverflow.com/a/56414990/9304616) – Lebecca Jun 17 '21 at 16:21
  • Which version of spring boot you are using? In my project we use `spring-profiles-active` three-layer structure to target the active profile. – Lebecca Jun 17 '21 at 16:27
  • @Lebecca 2.3.3.RELEASE – Matthew K. Jun 17 '21 at 16:35

0 Answers0