简体   繁体   English

Spring从我的系统环境变量中读取

[英]Spring to read from my system environment variables

I want to use my system environment variables (ie use variable FOO that I have defined in my .bash_profile export FOO=BAR ) for values in Spring's applicationContext.xml; 我想使用Spring的applicationContext.xml中的值使用系统环境变量(即,使用在.bash_profile export FOO=BAR定义的变量FOO );

For example: 例如:

<bean id="dataSource" ...>
   <property name="jdbcUrl" value="${FOO}" />
...
</bean>

I use both the Maven-tomcat-plugin and Tomcat inside IntelliJ to run my server. 我在IntelliJ中同时使用Maven-tomcat-plugin和Tomcat来运行服务器。

Right now when my webapp starts up, it can't find the system variables. 现在,当我的webapp启动时,它找不到系统变量。

What do I have to do to get Spring to read from my system environment variables? 我该怎么做才能使Spring从我的系统环境变量中读取?

PS I have already checked out how to read System environment variable in Spring applicationContext , but doesn't seem to work for me PS我已经检查了如何在Spring applicationContext中读取系统环境变量 ,但似乎对我不起作用

System environment variables are different to the JVM system properties - which the spring PropertyPlaceholderConfigurer gives you access to. 系统环境变量与JVM系统属性不同-春天的PropertyPlaceholderConfigurer可让您访问这些PropertyPlaceholderConfigurer So you'd need to add to the JVM command: 因此,您需要添加到JVM命令:

java ... -DFOO=${FOO}

(where -DFOO is defining "FOO" as a JVM system property and ${FOO} is inserting the "BAR" value from your "FOO" environment variable) (其中-DFOO将“ FOO”定义为JVM系统属性,而${FOO}从“ FOO”环境变量中插入“ BAR”值)

See also the ServletContextPropertyPlaceholderConfigurer - with the SYSTEM_PROPERTIES_MODE_OVERRIDE property and a few other options. 另请参见ServletContextPropertyPlaceholderConfigurer-具有SYSTEM_PROPERTIES_MODE_OVERRIDE属性和一些其他选项。

try this (Spring Expression Language) 试试这个(春季表达语言)

<bean id="dataSource" ...>
   <property name="jdbcUrl" value="#{systemEnvironment.FOO}" />
...
</bean>

or add this line 或添加此行

<context:property-placeholder/>

to application context 到应用程序上下文

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

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