通过引用bean名称在@Scheduled批注中使用@ConfigurationProperties [英] Using @ConfigurationProperties in a @Scheduled annotation by referencing the bean name

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

问题描述

我正在使用@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 @Componenton the properties class allows to access the property as "#{@someProperties.persistence.delay}.

查看全文

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