无法在 @Scheduled 注释中使用 @ConfigurationProperties [英] Unable to use @ConfigurationProperties in @Scheduled annotation

查看:46
本文介绍了无法在 @Scheduled 注释中使用 @ConfigurationProperties的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using a @ConfigurationProperties to define the property my.delay.

@ConfigurationProperties( "my" )
public class MyProperties {

    private long delay = 1000L;

    public long getDelay() {
        return delay;
    }
    public void setDelay(long delay) {
        this.delay = delay;
    }
}

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

In the scheduler method I try to use 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]

推荐答案

我不确定您的方法是否有解决方案.但是为了简化您的代码并有一个默认值,您可以这样做:

Im not sure if there is a solution for your approach. But to simplify your code and also have a default value you can go like that:

根本不需要 MyProperty 文件.你可以删除它.

No need to have MyProperty file at all. You can delete it.

使用此默认值更新您的 @Scheduled 注释:

Update your @Scheduled annotation with this default value:

 @Scheduled( fixedDelayString = "${my.delay:1000}" )

这意味着如果 Spring 没有找到 my.delay 的属性,它将使用 : 之后的默认值.在你的情况下它的 1000.

This means if Spring does not find a property of my.delay it uses the default value after the :. In your case its 1000.

如果你想覆盖默认值,只需在你的 application.properties 文件中添加属性:

And if you like to override the default value just add the property in your application.properties file:

my.delay=5000

这篇关于无法在 @Scheduled 注释中使用 @ConfigurationProperties的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆