0
votes

Question

How do I define Spring Expression Language (SpEL) in groovy spring boot project? (Per spring scheduler crontab @scheduled annotation)

Spring Boot Groovy and Spring EL Scheduler

Per documentation from the web, I'm dinking around with spring scheduler in my groovy spring boot application (2.2.x)

Per this post: Task scheduling using cron expression from properties file

Tried this

I've tried defining my cron expression both as follows:

@Scheduled(cron = "${appConfig.defaultCron}")

and

@Scheduled(cron = "#{appConfig.defaultCron}")

but get this error:

Attribute 'cron' should have type 'java.lang.String'; but found type 'java.lang.Object' in @org.springframework.scheduling.annotation.Scheduled

Analysis

It seems that the spring 'cron cruncher' sees the "${xxx}" as a Gstring and not as a string

Question

How do I get SpringEL work in cron definition in groovy spring boot project?

References https://www.baeldung.com/spring-scheduled-tasks

https://www.baeldung.com/spring-expression-language

Inject @Scheduled fixedRate value from Spring Boot application.yml file

1
You can't have GStrings in annotation values - only the base types are allowed (String, Class, arrays of those, ...). Have you tried '${...}' or "\${...}" ?cfrick

1 Answers

2
votes

Right. Try to escape that Groovy $ operator (like this \$). So, it comes to Java after compilation as regular symbol. Therefore Spring in the end will be able to resolve that property:

@Scheduled(cron = "\${appConfig.defaultCron}")