如果存在,我需要从我的属性文件中获取值来参数化一个@Scheduled
方法,否则使用默认值。
我们可以通过以下方式从配置文件属性进行参数化:
@Scheduled(cron = "${my.task.cron-exec-expr}")
public void scheduledTask() {
// do something
}
但是如果该属性不存在,我们将会遇到运行时异常。
我尝试使用带有默认值的 @ConfigurationProperties
bean,但没有成功:
@Component
@ConfigurationProperties(prefix = "my.task")
public class MyTaskProperties {
private String cronExecExpr = "*/5 * * * * *";
// getter and setter
}
如何避免这种情况并传递默认值?