java spring - 从 application.properties 文件中读取环境变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39703453/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
spring - read environment variables from inside the application.properties file
提问by Oleg
I specified Spring properties inside the application.propertiesfile. How can i populate those properties from the environment variables?
我在application.properties文件中指定了 Spring 属性。如何从环境变量中填充这些属性?
Here is what I tried, but it doesn't seem to work:
这是我尝试过的,但似乎不起作用:
application.properties
应用程序属性
spring.datasource.url=jdbc:postgresql://#{ systemProperties['DATABASE_HOST']}:5432/dbname
spring.datasource.username = postgres
spring.datasource.password = postgres
回答by Maciej Walkowiak
You can refer to environment properties in the same way you refer to Spring properties using ${...}
syntax.
您可以使用${...}
语法以与引用 Spring 属性相同的方式引用环境属性。
In your case:
在你的情况下:
spring.datasource.url=jdbc:postgresql://${DATABASE_HOST}:5432/dbname
回答by Eric G
Out of the box, as you know, spring-boot expects its Datasource details to be using a specific set of variable names. Being spring of course you can rework this if you need by a few methods:
开箱即用,如您所知,spring-boot 期望其数据源详细信息使用一组特定的变量名称。当然,作为春天,如果您需要,您可以通过以下几种方法对其进行返工:
1/ If the need to use variables from the environment comes from deployment to a cloud service such as Cloud Foundry or Horuku, there is spring-boot-starter-cloud-connector which handles allot of the plumbing out of the box. A good read is the (Binding to Data Services with Spring Boot in Cloud Foundryarticle and the Deploying to the clouddocs which walks you thru this
1/ 如果需要使用环境中的变量来自部署到云服务(例如 Cloud Foundry 或 Horuku),则有 spring-boot-starter-cloud-connector 可以处理开箱即用的管道分配。一个很好的阅读是(在 Cloud Foundry文章中使用 Spring Boot 绑定到数据服务和部署到云文档,它会引导您完成这个
2/ Instead of relying on Spring-Boot's own auto-magical wiring mechanism, you can create a custom configuration bean to override how the DataSource information is populated. A good read explaining the annotations involved can be found here: Spring Java Config Documentation - @BeanConfiguration JavaDOC. Based on your example above, here is what I spat out:
2/ 您可以创建自定义配置 bean 来覆盖 DataSource 信息的填充方式,而不是依赖 Spring-Boot 自己的自动神奇接线机制。可以在这里找到解释所涉及的注释的好读物: Spring Java 配置文档 - @Bean配置 JavaDOC。根据你上面的例子,这是我吐出来的:
@Configuration
public class MyDataSourceConfig {
@Bean
@Primary
public DataSource getDataSource() {
String url = "jdbc:postgresql://" + System.getenv("DATABASE_HOST") + ":5432/dbname";
String username = "postgres";
String password = "postgres";
String driverClassName = "org.postgresql.Driver";
/*
* Create the datasource and return it
*
* You could create the specific DS
* implementation (ie: org.postgresql.ds.PGPoolingDataSource)
* or ask Spring's DataSourceBuilder to autoconfigure it for you,
* whichever works best in your eyes
*/
return DataSourceBuilder
.create()
.url( url )
.username( username )
.password( password )
.driverClassName( driverClassName )
.build();
}
}
Just remember that in spring, you can always override allot of the default behaviours with a little bit of digging!
请记住,在 spring 中,您始终可以通过一点点挖掘来覆盖所有默认行为!
Hope this helps!
希望这可以帮助!
回答by rorschach
You don't have to. When Spring Boot initializes its environment, it pulls stuff from both the application.properties
file and any system-level variables and combines them together. The full list of locations where Spring takes them from is here, specifically points 9)and 10).
你不必。当 Spring Boot 初始化其环境时,它会从application.properties
文件和任何系统级变量中提取内容并将它们组合在一起。Spring 获取它们的位置的完整列表在这里,特别是第9)和10) 点。