通过引用bean名称在@Scheduled批注中使用@ConfigurationProperties [英] Using @ConfigurationProperties in a @Scheduled annotation by referencing the bean name
问题描述
我正在使用@ConfigurationProperties
来配置Spring引导中后台任务的延迟,并且试图在另一个组件上使用@Scheduled
批注中的该值.但是,为了使其正常工作,我必须使用Spring赋予Bean的全名.
I'm using @ConfigurationProperties
to configure the delay of a background task in Spring boot and I'm trying to use this value from a @Scheduled
annotation on another component. However, in order to make it work I must use the full name given to the bean by Spring.
配置属性类如下:
@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;
}
}
我在计划的方法中使用以下值:
And I'm using the value as follows in the scheduled method:
@Component
class BackgroundTasks {
@Scheduled(fixedDelayString = "#{@'some-com.example.demo.SomeProperties'.millis}") //this works.
public void sayHello(){
System.out.println("hello");
}
}
是否可以引用该值而不必使用bean的全名? 此答案表示有可能,但我无法使其正常工作.
Is it possible to reference the value without having to use the full name of the bean? This answer suggests it is possible but I haven't been able to make it work.
推荐答案
在properties类上使用@Component
允许以"#{@someProperties.persistence.delay}
的身份访问属性.
Using @Component
on the properties class allows to access the property as "#{@someProperties.persistence.delay}
.