gpt4 book ai didi

java - 如何在 spring-boot 2.2.* 中将 @ConfigurationProperties bean Autowiring 到 SpEL 表达式中?

转载 作者:行者123 更新时间:2023-12-01 16:14:07 25 4
gpt4 key购买 nike

我正在使用 Spring Boot 2.2.6 和 documentation说:

When the @ConfigurationProperties bean is registered using configuration property scanning or via @EnableConfigurationProperties, the bean has a conventional name: <prefix>-<fqn>, where <prefix> is the environment key prefix specified in the @ConfigurationProperties annotation and <fqn> is the fully qualified name of the bean. If the annotation does not provide any prefix, only the fully qualified name of the bean is used.

The bean name in the example above is 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.

最佳答案

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

例如:

package com.my.company;

...

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

private Duration theInterval = Duration.ofSeconds(30);

// getter and setter
}

然后

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

关于java - 如何在 spring-boot 2.2.* 中将 @ConfigurationProperties bean Autowiring 到 SpEL 表达式中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62449641/

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