弹簧注释不支持 Spel @Scheduled.fixedDelayString

2022-09-04 23:38:57

我想在我的属性文件中配置秒,然后我想在注释中将其转换为millis。fixedDelay@Scheduled

我期望这能起作用:

@Scheduled(fixedDelayString = "#{${my.scheduler.fixed.delay} * 1000}")

但它会引发以下异常:

由以下原因导致: java.lang.IllegalStateException: 遇到无效@Scheduled方法 'myMethod': Invalid fixedDelayString 值 “#{5 * 1000}” - 无法在 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

我走的路是对还是错?


答案 1

试试这个:

#{new Integer(${my.scheduler.fixed.delay}) * 1000}

答案 2

推荐