amg*_*han 9 java spring spring-annotations spring-el
我想在我的属性文件中以秒为单位配置fixedDelay,然后我想在@Scheduled Annotation中将其转换为millis.
我希望这可行:
@Scheduled(fixedDelayString = "#{${my.scheduler.fixed.delay} * 1000}")
Run Code Online (Sandbox Code Playgroud)
但它抛出了这个例外
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]
Run Code Online (Sandbox Code Playgroud)
$ {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;
}
}
Run Code Online (Sandbox Code Playgroud)
但我找不到注入自定义CustomValueResolver的方法.
我是对的还是错的?有另一种简单的方法
小智 1
尝试这个:
#{new Integer(${my.scheduler.fixed.delay}) * 1000}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1811 次 |
| 最近记录: |