如何在 spring-boot 2.2.* 中将 @ConfigurationProperties bean 自动装配到 SpEL 表达式中?

Pra*_*tic 4 java spring spring-boot

我正在使用 spring boot 2.2.6,正如文档所述:

@ConfigurationProperties使用配置属性扫描或通过 注册 bean时@EnableConfigurationProperties,该 bean 具有常规名称:<prefix>-<fqn>,其中<prefix>是注释中指定的环境键前缀@ConfigurationProperties<fqn>是 bean 的完全限定名称。如果注释不提供任何前缀,则仅使用 bean 的完全限定名称。

上例中的 bean 名称是 acme-com.example.AcmeProperties。

但是,当我使用此 SpEL 时:@Scheduled(fixedDelayString = "#{@(mr-my.config.properties.ApplicationProperties).task.get(T(my.config.Constants).CONST).delay}")我收到错误:

Parameter 0 of constructor in my.controllers.Controller required a bean named 'mr' that could not be found.


Action:

Consider defining a bean named 'mr' in your configuration.
Run Code Online (Sandbox Code Playgroud)

whi*_*mot 9

提供给注释的前缀ConfigurationProperties是 bean 的名称。

例如:

package com.my.company;

...

@ConfigurationProperties("alpha.beta.gamma")
public class TheTimes {

    private Duration theInterval = Duration.ofSeconds(30);

    // getter and setter
}
Run Code Online (Sandbox Code Playgroud)

进而

@Scheduled(fixedDelayString = "#{@'alpha.beta.gamma-com.my.company.TheTimes'.theInterval}")
Run Code Online (Sandbox Code Playgroud)