gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:04:39 24 4
gpt4 key购买 nike

我正在使用 @ConfigurationProperties 在 Spring boot 中配置后台任务的延迟,我正尝试在另一个上使用来自 @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;
}
}

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

@Component
class BackgroundTasks {

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

是否可以在不必使用 bean 的全名的情况下引用该值? This answer表明这是可能的,但我没能让它发挥作用。

最佳答案

在属性类上使用 @Component 允许以 "#{@someProperties.persistence.delay} 访问属性。

更多信息在 spring boot documentation .

关于java - 通过引用 bean 名称在 @Scheduled 注释中使用 @ConfigurationProperties,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49034588/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com