简体   繁体   English

Spring-是否可以在属性文件中递归解析属性(例如环境变量)?

[英]Spring - Is it possible to resolve properties (such as environment variables) recursively in properties file?

I am using @PropertySource and PropertySourcesPlaceholderConfigurer to load my properties file: 我正在使用@PropertySourcePropertySourcesPlaceholderConfigurer加载我的属性文件:

@Configuration
@PropertySource("classpath:app.properties")
class MyApp {
    @Bean
    public PropertySourcesPlaceholderConfigurer PropertySourcesPlaceholderConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();
    }
}

In app.properties , I would like to have: 我希望在app.properties中具有:

database.dataSource.url=jdbc:postgresql://localhost:${db-port:5432}/mydb

Here, the port of the database is either resolved from the property db-port , or defaulted to 5432. 在这里,数据库的端口从属性db-port解析,或者默认为5432。

This would allow me to spawn my application with the -Ddb-port=9876 flag if necessary. 如果需要的话,这将允许我使用-Ddb-port=9876标志生成应用程序。 If this flag is not set, the default port as written in app.properties should be taken. 如果未设置此标志,则应采用app.properties编写的默认端口。

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

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