[英]How to use Environment variables setted on cloudhub in java class
In local environment , we can configure some connection Environment properties in a property file, and then use them by context:property-placeholder. 在本地环境中,我们可以在属性文件中配置一些连接环境属性,然后通过context:property-placeholder来使用它们。 for example:
例如:
<context:property-placeholder location="classpath:resources-local.properties"/>
<smtp:endpoint host="${smtp.host}" port="${smtp.port}" user="${smtp.user}" password="${smtp.password}" name="NotificationEmail" doc:name="SMTP" to="${smtp.to}" from="${smtp.from}" subject="error" />
But when I deploy the app to cloudhub,I can set the connection info as Environment variables.We don't need to import the resources-local.properties file.We can still use the properties as 但是当我将应用程序部署到cloudhub时,我可以将连接信息设置为Environment变量。我们不需要导入resources-local.properties文件。我们仍然可以将属性用作
<smtp:endpoint host="${smtp.host}" port="${smtp.port}" user="${smtp.user}" password="${smtp.password}" name="NotificationEmail" doc:name="SMTP" to="${smtp.to}" from="${smtp.from}" subject="error" />
here is the question,how can I use Environment variables setted on cloudhub in java class.How can I get the smtp.host value in java class??? 这是一个问题,如何在java类中使用cloudhub上设置的环境变量。如何在java类中获取smtp.host值?
David told me that I can use them as they available as system properties. David告诉我,我可以使用它们作为系统属性。 But How to use the system properties in java class..
但是如何在java类中使用系统属性。
Any advise?? 有什么建议吗?? Thanks a lot!
非常感谢!
The best option is to inject them into your class via Spring. 最好的选择是通过Spring将它们注入到您的类中。 Eg:
例如:
<bean class="my.java.Object">
<property name="smtp" value="${smtp.host}">
</bean>
However, getting it as a system property via System.getProperty("smtp.host") will also work. 但是,也可以通过System.getProperty(“ smtp.host”)作为系统属性来获取它。
从Java类中,只需使用System.getProperty(“ smtp.host”);即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.