Kotlin Spring@自定义数据类的值



我希望能够做以下事情:

#application.yml
servers:
foo:
name: "Foo"
url: "http://localhost:8080/foo"
description: "Foo foo"
bar:
name: "Bar"
url: "http://localhost:8080/bar"
description: "Bar bar"
data class Server (
val name : String,
val url : String,
val description : String
)

然后在代码的某个地方

@Service
class LookupService (
@Value ("${servers.foo}")
val fooServer : Server,
@Value ("${servers.bar}")
val barServer : Server
) {
// do stuff
}

当我目前尝试时,我在启动应用程序时会得到java.lang.IllegalArgumentException: Could not resolve placeholder 'servers.bar' in value "${servers.bar}"

有没有一种简单的方法可以做到这一点,而无需对每个属性专门执行@Value

我认为@Value只能处理一个;叶子;属性,即具有单个值的属性,而不是具有子级的属性。

这是我对类型安全配置属性文档的理解。

在您的案例中,您可以做的是准备一个Servers结构,该结构将包含将整个配置树映射到某个点的映射。在您的情况下,可以使用Server类型的foobar属性创建它。

要使其完全工作,您需要在代码中添加3个注释:

  1. @EnableConfigurationProperties(Servers::class)在配置类上激活对服务器类型安全配置的支持
  2. @ConfigurationProperties("servers") on服务器class, to tell Spring that Servers should be filled with data extracted from properties with服务器前缀
  3. Servers类上的@ConstructorBinding,以告诉Spring它是不可变的,并且必须使用构造函数注入值

您将在下面找到一个最小的工作示例:

@SpringBootApplication
@EnableConfigurationProperties(Servers::class)
class DemoApplication
fun main(args: Array<String>) {
runApplication<DemoApplication>(*args)
}
data class Server(val name: String, val url: URL, val description: String)
@ConfigurationProperties("servers")
@ConstructorBinding
data class Servers(val foo: Server, val bar: Server)
@Service
class LookupService(servers : Servers) {
val foo = servers.foo
val bar = servers.bar
init {
println(foo)
println(bar)
}
}

启动时,示例应用程序打印注入的配置:

Server(name=Foo, url=http://localhost:8080/foo, description=Foo foo)
Server(name=Bar, url=http://localhost:8080/bar, description=Bar bar)

最新更新