小编Pet*_*edy的帖子

无法在@Scheduled批注中使用@ConfigurationProperties

我正在使用@ConfigurationProperties来定义属性my.delay

@ConfigurationProperties( "my" )
public class MyProperties {

    private long delay = 1000L;

    public long getDelay() {
        return delay;
    }
    public void setDelay(long delay) {
        this.delay = delay;
    }
}
Run Code Online (Sandbox Code Playgroud)

在调度程序方法中,我尝试使用my.delay

@SpringBootApplication
@EnableScheduling
@EnableConfigurationProperties( { MyProperties.class } )
public class TestSprPropApplication {

    public static void main(String[] args) {
        SpringApplication.run(TestSprPropApplication.class, args);
    }

    @Scheduled( fixedDelayString = "${my.delay}" )
    public void schedule() {
        System.out.println( "scheduled" );
    }
}
Run Code Online (Sandbox Code Playgroud)

然后出现以下错误:

Caused by: java.lang.IllegalStateException: Encountered invalid @Scheduled method 'schedule': Could not …
Run Code Online (Sandbox Code Playgroud)

spring spring-boot

6
推荐指数
2
解决办法
2307
查看次数

标签 统计

spring ×1

spring-boot ×1