java - Docker 环境变量中的 Spring 属性

标签 java spring docker

我正在尝试使用 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/

相关文章:

docker - 如何从命令行重置 docker-for-mac v2.1.0.0 kubernetes?

django - Docker:从Web容器内部执行将django迁移到db容器

java - Android长文本分页

java - 如何在值类型和键值对数量未知的情况下 HashMap JSON 的键值对

分布式环境中的 Springquartz/cron 作业

java - SQL Server JDBC 的驱动程序类名称是什么

java - 从 SAMLAuthenticationToken 创建 JWT 访问 token

docker - nginx反向代理简单配置不重定向

java - 如何删除列表和迭代器中的警告[不是由 @SuppressWarnings ] [在两行代码上应用什么泛型类型]

java - Spring 上下文作为运行时依赖