简体   繁体   English

如何使用Java类中Cloudhub上设置的环境变量

[英]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.

相关问题 如何在Java中使用Jenkins中的环境变量? - How to use environment variables in Jenkins by the Java? 如何使用 Java 在 CMD 中使用环境变量? - How to use environment variables in CMD using Java? 为什么Tomcat8.exe以不同于Windows环境变量中设置的JAVA_HOME的JAVA_HOME开头? - Why Tomcat8.exe starts with a JAVA_HOME that is different from JAVA_HOME setted in Windows environment variables? Java和Eclipse如何使用环境变量? (MAC OS X) - How to use environment variables in Java with Eclipse? (MAC OS X) 检测到使用 Java 选项环境变量。 如何删除它? - The use of Java options environment variables detected. How to delete it? 如何为Java设置环境变量 - How to set environment variables for Java Mac环境变量不适用于Java运行时类 - Mac environment variables not available to java runtime class 初始化时继承的类变量会覆盖父级设置的自己的值 - Inherited class variables when initialized are overriding own value setted by parent 如何在 Java 中的另一个 class 中使用来自 class 的变量 - How can I use variables from a class in another class in Java 如何强制Java重新加载环境变量 - How to force Java to reload environment variables
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM