我想在我的属性文件中以秒为单位配置fixedDelay,然后我想在@Scheduled Annotation中将其转换为millis。
我希望这可行:
@Scheduled(fixedDelayString = "#{${my.scheduler.fixed.delay} * 1000}")
但它抛出此异常
Caused by: java.lang.IllegalStateException: Encountered invalid @Scheduled method 'myMethod': Invalid fixedDelayString value "#{5 * 1000}" - cannot parse into integer
at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.processScheduled(ScheduledAnnotationBeanPostProcessor.java:384) ~[spring-context-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor$1.doWith(ScheduledAnnotationBeanPostProcessor.java:227) ~[spring-context-4.1.6.RELEASE.jar:4.1.6.RELEASE]
$ {my.scheduler.fixed.delay}已被正确恢复,但表达式未被重新开发。
我尝试配置自己的StringValueResolver
private static class CustomValueResolver
implements StringValueResolver {
private final ConfigurableBeanFactory beanFactory;
private final ExpressionParser expressionParser;
public CustomValueResolver(final ConfigurableBeanFactory beanFactory, final ExpressionParser expressionParser) {
this.beanFactory = beanFactory;
this.expressionParser = expressionParser;
}
@Override
public String resolveStringValue(
final String strVal) {
String value = this.beanFactory.resolveEmbeddedValue(strVal);
if (value.startsWith("#{")) {
value = this.expressionParser.parseExpression(value).getValue(String.class);
}
return value;
}
}
但我找不到注入自定义CustomValueResolver的方法。
我是对的还是错的?有另一种简单的方法