我在Maven中使用了Spring Boot应用程序,我需要设置Spring属性,但我无法解决这个问题.

问题:

假设有一个Spring应用程序和应用程序.属性应该设置了spring.datasource.url个.

我有两个Maven配置文件,其中包含两个不同的应用程序.properties文件,一个用于开发,另一个用于部署.例如,应用程序容器.properties和应用程序-Dev.Properties:

    <profiles>
        <profile>
            <id>dev</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <activatedProperties>dev</activatedProperties>
                <spring.profiles.active>dev</spring.profiles.active>
            </properties>
        </profile>

        <profile>
            <id>container</id>
            <properties>
                <activatedProperties>container</activatedProperties>
                <spring.profiles.active>container</spring.profiles.active>
            </properties>
        </profile>
    </profiles>

和应用程序.属性:

spring.profiles.active=@spring.profiles.active@
  • 对于开发和测试,它应该类似于

应用程序-Dev.Properties:

spring.datasource.url=jdbc://devdatabase://localhost:12345/database
  • 构建的应用程序应该在容器中运行,假设它是 docker .对于docker应用程序.属性将如下所示

应用程序容器.属性:

spring.datasource.url=${DATASOURCE}

并且在docker中将使用env文件,如(docker-compose.yaml):

    env_file:
      - ./docker.env

其中docker.env类似于:

DATASOURCE=jdbc://proddatabase://prodhost:2345/proddatabase

问题是,当我在本地为docker部署构建应用程序时,我无法运行测试,因为预计将从系统环境中设置spring.datasource.url.我试图找到一个maven插件来从文件中加载环境变量(S),但没有成功.

值得一提的是,我实际上有大约20-30个这样的参数.

我的问题是:我怎么才能解决这个问题呢?我还不熟悉maven,也许除了在容器中构建之外,我还可以使用其他方法?理想情况下,我希望使用从环境中获取值的单个应用程序.properties,并通过使用不同的env文件将环境切换为maven,就像在docker中所做的那样.如果也能看到一些例子,那就太好了.

我们非常感谢您的建议.谢谢.

推荐答案

How about use the spring.profiles.active?
You can define a variable in your different environment.
Make three properties file in your code. One is application.properties, it just set spring.profiles.active = ${Your environment variable name}
The other two is container.properties and dev.properties,define the different environment variable.
Then set the different variable in your develop operate system and your container system.

Java相关问答推荐

当耗时的代码完成时,Circular ProgressIndicator显示得太晚

如何在Spring Security中设置CustomLogin路径?

ittext pdf延迟签名,签名无效

JavaFX Maven Assembly插件一直打包到错误的JDK版本

Spring Boot@Cachebale批注未按预期工作

DTO到实体,反之亦然,控制器和服务之间的哪一层应该处理转换?

获取字符串中带空格的数字和Java中的字符

将响应转换为带值的键

从ActiveMQ Classic迁移到ActiveMQ Artemis需要进行哪些客户端更改?

声明MessageChannel Bean的首选方式

Sack()步骤中的合并运算符未按预期工作

如何在EL处理器中定义带有命名空间的变量?

如何在ImageIO或十二只猴子中旋转TIFF CMYK图像?

当我在Java中有一个Synchronized块来递增int时,必须声明一个变量Volatile吗?

嘲笑黄瓜中的对象

如何在Java中使用正则表达式拆分字符串

将java.util.Date(01.01.0001)转换为java.time.LocalDate将返回29.12.0000

在Java中使用StorageReference将数据从Firebase存储添加到数组列表

如何利用OpenTelemeter将初始值(零)输出到普罗米修斯

如何修复Spring Boot应用程序中的RestDocumentationGenerationException:java.io.FileNotFoundException:/curl-request.adoc(只读文件系统)?