Is there a way to call a getter (or even a variable) from a propertyClass in Spring's @Scheduled
cron
configuration? The following doesn't compile:
@Scheduled(cron = propertyClass.getCronProperty())
or @Scheduled(cron = variable)
I would like to avoid grabbing the property directly:
@Scheduled(cron = "${cron.scheduling}")
Short answer - it's not possible out of the box.
The value passed as the "cron expression" in the @Scheduled
annotation is processed in ScheduledAnnotationBeanPostProcessor
class using an instance of the StringValueResolver
interface.
StringValueResolver
has 3 implementations out of the box - for Placeholder
(eg ${}), for Embedded
values and for Static
Strings - none of which can achieve what you're looking for.
If you have to avoid at all costs using the properties placeholder in the annotation, get rid of the annotation and construct everything programmatically. You can register tasks using ScheduledTaskRegistrar
, which is what the @Scheduled
annotation actually does.
I will suggest to use whatever is the simplest solution that works and passes the tests.
If you don't want to retrieve the cron expression from a property file you can do it programatically as it follows:
// Constructor
public YourClass(){
Properties props = System.getProperties();
props.put("cron.scheduling", "0 30 9 * * ?");
}
That allows you to use your code whitout any changes:
@Scheduled(cron = "${cron.scheduling}")
@Component
public class MyReminder {
@Autowired
private SomeService someService;
@Scheduled(cron = "${my.cron.expression}")
public void excecute(){
someService.someMethod();
}
}
in /src/main/resources/application.properties
my.cron.expression = 0 30 9 * * ?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.