我正在尝试使用 Spring 3.2.16 应用程序的环境变量在 docker 容器内的配置文件之间进行切换。
在我的应用程序上下文中,我有两个加载属性文件的配置文件,我开发的配置文件(100%工作)如下所示:
<beans profile="dev">
<context:property-placeholder location="classpath*:META-INF/spring/dev.properties" />
</beans>
将应用程序打包到容器中时,没有太多内容,这是我的 Docker 文件:
FROM jetty
MAINTAINER Jan Vladimir Mostert "me@example.com"
ADD ./target/ROOT.war /var/lib/jetty/webapps/ROOT.war
EXPOSE 8080
运行时,请注意 -e
设置环境变量的标志:
docker run --name='_______' -d -p 8000:8080 --link rabbitmq:rabbitmq -e ENV=test _________________
在 test
中运行时, prod
, ...
,我设置的默认配置文件为 cloud
。这也有效:
<beans profile="cloud">
<context:property-placeholder location="classpath*:META-INF/spring/test.properties" />
</beans>
现在我想换掉 test.properties
从环境变量中获取配置文件。
我尝试过使用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>
进入容器:
docker exec -ti _______ bash
并运行
echo $ENV
打印
test
环境变量设置正确,将该变量放入我的applicationContext.xml
的正确方法是什么? ?
最佳答案
切换至
<beans profile="cloud">
<context:property-placeholder
location="classpath*:META-INF/spring/${ENV}.properties" />
</beans>
神奇地让它发挥作用。
关于java - Docker 环境变量中的 Spring 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36673653/