spring - Kotlin application.yml 数据类寻找 beans

标签 spring spring-boot kotlin spring-properties configurationproperties

我有一个 Kotlin 数据类,我想从 application.yml 文件中读取属性。这是我的数据类:

@ConstructorBinding
@ConfigurationProperties("meanwhile.in.hell.myapp")
data class MyAppProperties(val serverId: String, val locationId: String)

然后我将其添加到我的配置类中:

@Configuration
@EnableConfigurationProperties(MyAppProperties::class)
open class MyAppConfiguration(private val properties: MyAppProperties) {

我仅使用 properties.serverId 访问值,并将对象传递到正在创建的其他 bean 的构造函数中,例如这个:

open class MyAppClient(
    private val webClient: WebClient,
    private val properties: MyAppProperties
) : IMyAppClient {

但是,当我启动应用程序时,我收到一个错误,它不是尝试从 application.yml 加载属性,而是尝试为构造函数参数查找 bean:

通过构造函数参数 0 表达的依赖关系不满足;嵌套异常是org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为'meanwhile.in.hell.myapp-meanwhile.in.hell.myapp.MyAppProperties'的bean时出错:通过构造函数参数0表达的依赖关系不满足;嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException:没有可用的“java.lang.String”类型的合格 bean:预计至少有 1 个符合 Autowiring 候选资格的 bean。依赖注释:{}

同时.in.hell.myapp.MyAppProperties 中的构造函数参数 0 需要一个类型为“java.lang.String”的 bean,但无法找到。

如何阻止我的应用认为这些参数是 Autowiring 的?我知道在 Kotlin 中,这就是构造函数 Autowired bean 的样子(即不需要注释),但是我在网上看到的有关如何读取 application.yml 属性的所有示例看起来都与我的数据类。

Spring-Boot v2.3.0.RELEASE Kotlin v1.3.72

最佳答案

关于spring - Kotlin application.yml 数据类寻找 beans,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62810702/

相关文章:

plugins - 如何在 Intellij 15 中降级 Kotlin

java - 在 Spring Security 中添加组

android - 如何在我的 Android 项目中使用 Spring 框架?

java - 带有 LIKE 的 Spring JPA @Query

javascript - SpringBoot Angular 应用程序不提供所有 js 文件

java - 使用 ObjectMapper 将 JSON 日期格式从 ZonedDateTime 序列化为自定义日期格式

java - 有没有一种简单的方法可以在 Spring Boot 中使用 Java 注解从测试覆盖率中排除类?

java - 未定义名为 'shiroFilter' 的 bean

java - 自从 @SpringApplicationConfiguration、@WebIntegration 在 Spring Boot 框架中被弃用以来,正确的注释是什么?

java - ProcessBuilder 挂起