繁体   English   中英

Spring Scheduled注解设置fixedDelay值时编译报错属性必须是常量表达式

[英]Compile error attribute must be a constant expression when set fixedDelay value in Spring Scheduled annotation

我想从属性文件中读取 fixedDelay,如下所示。

@Bean
public Runnable method(){  
static final int delay= properties.getConnectionConfig().getConnectMonitorDelay();
        return new Runnable() {
            @Override
            @Scheduled(fixedDelay = delay)
            public void run() {}
       };
}

properties.getConnectionConfig().getConnectMonitorDelay() 已经返回一个原始 int,但我遇到以下编译错误。

注释属性 Scheduled.fixedDelay 的值必须是常量表达式

如何摆脱编译错误?

Java 注释采用编译时常量,这些常量被定义为最终基元或字符串。

在 Spring Boot 中,您可以直接使用具有fixedDelayStringfixedRateStringinitialDelayString属性的应用程序属性。

@Bean
public Runnable method(){
    return new Runnable() {
        @Override
        @Scheduled(fixedDelayString = "${connection-config.connect-monitor-delay}")
        public void run() {
            // ..
        }
    };
}

查看此链接以了解更多不同的方法:

以编程方式安排 Spring 的工作(动态设置 fixedRate)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM