我正在使用@ConfigurationProperties来定义属性my.delay
。无法在@Scheduled注释中使用@ConfigurationProperties
@ConfigurationProperties("my")
public class MyProperties {
private long delay = 1000L;
public long getDelay() {
return delay;
}
public void setDelay(long delay) {
this.delay = delay;
}
}
在调度方法,我尝试使用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");
}
}
然后下面的错误出现了:如果
Caused by: java.lang.IllegalStateException: Encountered invalid @Scheduled method 'schedule': Could not resolve placeholder 'my.delay' in string value "${my.delay}"
at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.processScheduled(ScheduledAnnotationBeanPostProcessor.java:454) ~[spring-context-4.3.6.RELEASE.jar:4.3.6.RELEASE]
at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.postProcessAfterInitialization(ScheduledAnnotationBeanPostProcessor.java:324) ~[spring-context-4.3.6.RELEASE.jar:4.3.6.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:423) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1633) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
你可以显示你的'application.properties'文件。您也可以将该属性直接注入到计划的注释中。 – Patrick
@Patrick我的'application.properties'是空的。它应该使用MyProperties中指定的值,并且只有在application.properties中设置了my.delay时才会覆盖它。至少这是我的意图。 –
在属性文件中创建'my.delay = 1000',它应该可以工作。不确定这是另一种方式。 – Patrick