简体   繁体   English

带有 Kotlin 的 Spring Boot - @Value 注释未按预期工作

[英]Spring Boot with Kotlin - @Value annotation not working as expected

I'm developing a Spring boot application using Kotlin.我正在使用 Kotlin 开发 Spring 启动应用程序。 Since I need to connect to an external API (cloudinary) I decided to add to my app a configuration class in order to store (and hide from VCS) my sensible data like username, passwords or API keys.由于我需要连接到外部 API(cloudinary),我决定向我的应用程序添加一个配置类,以便存储(并向 VCS 隐藏)我的敏感数据,如用户名、密码或 API 密钥。

So this is what i did:所以这就是我所做的:

I created a Config class:我创建了一个 Config 类:

package demons

import org.springframework.beans.factory.annotation.Value
import org.springframework.context.annotation.Configuration
import org.springframework.context.annotation.PropertySource

@Configuration
@PropertySource("classpath:application.properties")
class AppConfig {
    @Value("\${test.prop}")
    val testProperty: String? = null
}

Then I added a test.prop entry in my application.properties file然后我在 application.properties 文件中添加了一个 test.prop 条目

test.prop=TEST

However, in every test I run, after creating an instance of AppConfig, his testProperty attribute is null instead of being the string TEST .但是,在我运行的每个测试中,在创建 AppConfig 实例后,他的 testProperty 属性为null而不是字符串TEST

For instance this snippet:例如这个片段:

val config = AppConfig()
System.out.println(config.testProperty)

would print out:会打印出来:

null

I've also tried using a separate .properties file instead of the default one like myproperties.properties and declaring the variable as lateinit var .我还尝试使用单独的 .properties 文件而不是像myproperties.properties这样的默认文件,并将变量声明为lateinit var In this last case the variable seems to never be initialized:在最后一种情况下,变量似乎从未被初始化:

kotlin.UninitializedPropertyAccessException: lateinit property testProperty has not been initialized

What am I missing?我错过了什么?

The issue is that you are creating the instance of AppConfig yourself via the constructor:问题是您正在通过构造函数自己创建AppConfig的实例:

 val config = AppConfig()

While this class might have Spring annotations, it is NOT spring managed if you create the instance yourself.虽然此类可能具有 Spring 注释,但如果您自己创建实例,则它不是由 spring 管理的。

I recommend you borrow from the link you mentioned in my other answer.我建议您从我在其他答案中提到链接借用。 There are good examples of using SpringBoot to create a Spring application for you.有使用 SpringBoot 为您创建 Spring 应用程序的好例子。 Below I created a 'merged' example of your test + an example from the link.下面我创建了一个“合并”的测试示例+来自链接的示例。 There is no need to specify a properties file, as application.properties is used as a property source by default.无需指定属性文件,因为application.properties默认用作属性源。

@SpringBootApplication
class AppConfig {
    @Value("\${test.prop}")
    val testProperty: String? = null
}

fun main(args: Array<String>) {
    val appContext = SpringApplication.run(AppConfig::class.java, *args)
    val config = appContext.getBean(AppConfig::class.java)
    System.out.println(config.testProperty)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM