通过在配置中声明以下bean,可以自定义使用的前缀:
@Bean
fun propertyConfigurer() = PropertySourcesPlaceholderConfigurer().apply {
setPlaceholderPrefix("%{")
}
如果您有任何使用${...}
语法的现有代码(如Spring Boot actuators或@LocalServerPort
),您应该声明:
@Bean
fun kotlinPropertyConfigurer() = PropertySourcesPlaceholderConfigurer().apply {
setPlaceholderPrefix("%{")
setIgnoreUnresolvablePlaceholders(true)
}
@Bean
fun defaultPropertyConfigurer() = PropertySourcesPlaceholderConfigurer()
Escaping the dollar like in @Value("\${some.property}")
is another possible option that require no @Bean
declaration.
对于配置为@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
的Spring Boot测试,可以使用@LocalServerPort
而不是@Value("\${local.server.port}")
.
@ConfigurationProperties
将是一个更好的 Select ,尤其是对于Kotlin数据类,但目前必须使用具有可为null的var
属性的Kotlin类,因为只支持getter/setter.你可以投票支持this issue或发表 comments ,以表明你有兴趣在Spring Boot 2中获得支持.十、