Spring 引导:更改 属性 占位符指示符
Spring Boot: Change property placeholder signifier
在 Spring 引导中更改 属性 占位符的前缀和后缀的最简单方法是什么?
默认值为 @Value("${some.property}")
,但这在 Kotlin 中看起来很难看,因为它需要转义 - ${something} 是 Kotlin 中用于字符串模板的语言功能。
他们有一个新功能,使用 java 类 注释 @ConfigurationProperties
。这在 Kotlin 中看起来不错并且正在重构保存。你应该试一试:
根据 dox 提供的答案中的建议,我最终得到了类似的结果:
public interface TokenAuthenticationConfig {
public fun apiKey() : String
}
@Component
@ConfigurationProperties(prefix = "service.api")
public open class TokenAuthenticationConfigImpl : TokenAuthenticationConfig
{
public var apiKey : String
constructor() {
this.apiKey = ""
}
override fun apiKey(): String
{
return this.apiKey
}
}
在 Spring @ConfigurationProperties
中,对象需要遵循 Java Beans 模式,因此是可变的。对我来说,配置似乎在整个应用程序生命周期中通常应该是静态的,因此与其增加关于状态推理的复杂性,不如注入不可变接口。
可以通过在配置中声明以下 bean 来自定义使用的前缀:
@Bean
fun propertyConfigurer() = PropertySourcesPlaceholderConfigurer().apply {
setPlaceholderPrefix("%{")
}
如果您有任何使用 ${...}
语法的现有代码(如 Spring 引导执行器或 @LocalServerPort
),您应该声明:
@Bean
fun kotlinPropertyConfigurer() = PropertySourcesPlaceholderConfigurer().apply {
setPlaceholderPrefix("%{")
setIgnoreUnresolvablePlaceholders(true)
}
@Bean
fun defaultPropertyConfigurer() = PropertySourcesPlaceholderConfigurer()
像 @Value("${some.property}")
中那样转义美元是另一种不需要 @Bean
声明的可能选项。
对于使用 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
配置的 Spring 引导测试,您可以使用 @LocalServerPort
而不是 @Value("${local.server.port}")
。
@ConfigurationProperties
would be a better alternative, especially with Kotlin data classes, but currently you have to use Kotlin classes with nullable var
properties since only getter/setter are supported. You can vote for this issue 或发表评论以表明您有兴趣在 Spring Boot 2.x.
中获得支持
在 Spring 引导中更改 属性 占位符的前缀和后缀的最简单方法是什么?
默认值为 @Value("${some.property}")
,但这在 Kotlin 中看起来很难看,因为它需要转义 - ${something} 是 Kotlin 中用于字符串模板的语言功能。
他们有一个新功能,使用 java 类 注释 @ConfigurationProperties
。这在 Kotlin 中看起来不错并且正在重构保存。你应该试一试:
根据 dox 提供的答案中的建议,我最终得到了类似的结果:
public interface TokenAuthenticationConfig {
public fun apiKey() : String
}
@Component
@ConfigurationProperties(prefix = "service.api")
public open class TokenAuthenticationConfigImpl : TokenAuthenticationConfig
{
public var apiKey : String
constructor() {
this.apiKey = ""
}
override fun apiKey(): String
{
return this.apiKey
}
}
在 Spring @ConfigurationProperties
中,对象需要遵循 Java Beans 模式,因此是可变的。对我来说,配置似乎在整个应用程序生命周期中通常应该是静态的,因此与其增加关于状态推理的复杂性,不如注入不可变接口。
可以通过在配置中声明以下 bean 来自定义使用的前缀:
@Bean
fun propertyConfigurer() = PropertySourcesPlaceholderConfigurer().apply {
setPlaceholderPrefix("%{")
}
如果您有任何使用 ${...}
语法的现有代码(如 Spring 引导执行器或 @LocalServerPort
),您应该声明:
@Bean
fun kotlinPropertyConfigurer() = PropertySourcesPlaceholderConfigurer().apply {
setPlaceholderPrefix("%{")
setIgnoreUnresolvablePlaceholders(true)
}
@Bean
fun defaultPropertyConfigurer() = PropertySourcesPlaceholderConfigurer()
像 @Value("${some.property}")
中那样转义美元是另一种不需要 @Bean
声明的可能选项。
对于使用 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
配置的 Spring 引导测试,您可以使用 @LocalServerPort
而不是 @Value("${local.server.port}")
。
@ConfigurationProperties
would be a better alternative, especially with Kotlin data classes, but currently you have to use Kotlin classes with nullable var
properties since only getter/setter are supported. You can vote for this issue 或发表评论以表明您有兴趣在 Spring Boot 2.x.