小编San*_*ich的帖子

通过引用bean名称在@Scheduled注释中使用@ConfigurationProperties

我正在使用@ConfigurationPropertiesSpring引导配置后台任务的延迟,我正在尝试使用@Scheduled另一个组件上的注释中的这个值.但是,为了使它工作,我必须使用Spring给bean的全名.

配置属性类如下:

@ConfigurationProperties("some")
class SomeProperties {
    private int millis; //the property is some.millis

    public int getMillis() {
        return millis;
    }

    public void setMillis(int millis) {
         this.millis = millis;
    }
}
Run Code Online (Sandbox Code Playgroud)

我在预定的方法中使用如下值:

@Component
class BackgroundTasks {

    @Scheduled(fixedDelayString = "#{@'some-com.example.demo.SomeProperties'.millis}") //this works.
    public void sayHello(){
        System.out.println("hello");
    }
}
Run Code Online (Sandbox Code Playgroud)

是否可以引用该值而无需使用bean的全名?这个答案表明这是可能的,但我无法使其发挥作用.

java spring spring-boot

7
推荐指数
1
解决办法
871
查看次数

标签 统计

java ×1

spring ×1

spring-boot ×1