Properties 如何使用Kotlin绑定属性值?
使用java,我可以从application.properties文件中获取“spring.datasource.url”键的值,如下所示:Properties 如何使用Kotlin绑定属性值?,properties,binding,kotlin,classpath,Properties,Binding,Kotlin,Classpath,使用java,我可以从application.properties文件中获取“spring.datasource.url”键的值,如下所示: @PropertySource("classpath:application.properties") public class SpringJdbcConfig { @Autowired Environment environment; private final String URL = "spring.datasource.u
@PropertySource("classpath:application.properties")
public class SpringJdbcConfig {
@Autowired
Environment environment;
private final String URL = "spring.datasource.url";
String dburl = environment.getProperty("spring.datasource.url");
}
@PropertySource("classpath:application.properties")
open class WebController {
@Autowired
var env: Environment ? = null
}
使用kotlin,这是不可能的:
@PropertySource("classpath:application.properties")
public class SpringJdbcConfig {
@Autowired
Environment environment;
private final String URL = "spring.datasource.url";
String dburl = environment.getProperty("spring.datasource.url");
}
@PropertySource("classpath:application.properties")
open class WebController {
@Autowired
var env: Environment ? = null
}
环境将不会引用PropertySource文件。
如何在kotlin中使用它?对于要注入的字段,kotlin提供了lateinit关键字 如果您想从配置中读取值,spring提供
请注意,在Java中,您可以使用
“${my.property.key}”
作为“路径”,但由于${}
在kotlin中有特殊含义,因此您必须使用\
来转义$
,因为kotlin提供了lateinit关键字
如果您想从配置中读取值,spring提供
请注意,在Java中,您可以使用“${my.property.key}”
作为“路径”,但由于${}
在kotlin中有特殊含义,因此必须使用\
来转义$