如何在java类中使用cloudhub上设置的环境变量

如何在java类中使用cloudhub上设置的环境变量,java,mule,cloudhub,Java,Mule,Cloudhub,在本地环境中,我们可以在属性文件中配置一些连接环境属性,然后按上下文使用它们:property placeholder。例如: <context:property-placeholder location="classpath:resources-local.properties"/> <smtp:endpoint host="${smtp.host}" port="${smtp.port}" user="${smtp.user}" password="$

在本地环境中,我们可以在属性文件中配置一些连接环境属性,然后按上下文使用它们:property placeholder。例如:

<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" />

但当我将应用程序部署到cloudhub时,我可以将连接信息设置为环境变量。我们不需要导入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" />

问题是,如何在java类中使用cloudhub上设置的环境变量。如何在java类中获取smtp.host值

David告诉我,我可以使用它们作为系统属性。 但是如何在java类中使用系统属性


有什么建议吗??非常感谢

在java类中,只需使用System.getProperty(“smtp.host”)

最好的选择是通过Spring将它们注入到类中。例如:

<bean class="my.java.Object">
  <property name="smtp" value="${smtp.host}">
</bean>

但是,通过system.getProperty(“smtp.host”)将其作为系统属性也可以工作