来自Docker环境variables的Spring属性
我试图在我的Spring 3.2.16应用程序中使用环境variables在Docker容器中的configuration文件之间切换。
在我的应用程序上下文中,我有两个configuration文件加载属性文件,我开发的一个(它工作100%)如下所示:
<beans profile="dev"> <context:property-placeholder location="classpath*:META-INF/spring/dev.properties" /> </beans>
将应用程序打包到一个容器中时,没有多less内容,这是我的Docker文件:
FROM jetty MAINTAINER Jan Vladimir Mostert "me@example.com" ADD ./target/ROOT.war /var/lib/jetty/webapps/ROOT.war EXPOSE 8080
运行时,注意-e
标志来设置环境variables:
docker run --name='_______' -d -p 8000:8080 --link rabbitmq:rabbitmq -e ENV=test _________________
在test
运行它时, prod
, ...
默认configuration文件将在其中设置为cloud
。 这也适用于:
<beans profile="cloud"> <context:property-placeholder location="classpath*:META-INF/spring/test.properties" /> </beans>
现在我想换出test.properties
来从环境variables中获取configuration文件。
我试过使用systemProperties
,但它不工作:
<beans profile="cloud"> <context:property-placeholder location="classpath*:META-INF/spring/#{systemProperties['ENV']}.properties" /> </beans>
我已经尝试使用systemEnvironment
不起作用:
<beans profile="cloud"> <context:property-placeholder location="classpath*:META-INF/spring/#{systemEnvironment['ENV']}.properties" /> </beans>
input容器:
docker exec -ti _______ bash
并运行
echo $ENV
版画
testing
环境variables设置正确,将该variables放入我的applicationContext.xml
的正确方法是什么?
切换到
<beans profile="cloud"> <context:property-placeholder location="classpath*:META-INF/spring/${ENV}.properties" /> </beans>
神奇地使它工作。