gpt4 book ai didi

java - Spring boot,环境特定属性位置

转载 作者:太空宇宙 更新时间:2023-11-04 12:20:17 24 4
gpt4 key购买 nike

我可以看到很多有关环境特定属性的帖子,但没有一个能够完全解决我的问题。

我正在开发我的 Spring Boot 应用程序,该应用程序有两个配置文件 dev/live。

我希望我的项目具有以下结构:

--deployment
--dev
--myapp.properties
--live
--myapp.properties
--src
--main
--java
--resources
--test
--java

我知道如果我有两个 application.properties在我的资源文件夹中,例如 application-live.propertiesapplication-dev.properties我可以使用以下命令告诉 Maven 在应用程序启动时选择哪一个:

Dspring.profiles.active=profile_name

但我不喜欢这个通用属性文件的名称。因此,我找到了这种方法来告诉应用程序在 /deployment/<profile>/ 中查找配置文件特定属性的位置。 .

我使用 Maven 构建任务配置来做到这一点:

<build>
<resources>
<resource>
<directory>deployment/${environment}</directory>
<includes>
<include>*.properties</include>
<include>*.xml</include>
</includes>
</resource>
</resources>
</build>

这将在 deployment/<profile> 中搜索资源并包括他们。好消息是 Maven 找到了正确的环境属性文件,但不幸的是它将它们包含在 target/classes/myapp.properties 中。这对于 Spring Boot 来说似乎是不可见的?我如何告诉 Maven 将其放在与 application.properties 相同的目录中或者我怎样才能告诉 Spring Boot 找到它目前所在的位置?

非常感谢!

最佳答案

How can I tell Maven to place it in the same directory as application.propertiesor alternately how can I tell Spring Boot to find it where it is at the moment?

1) 您不需要修改 Maven 设置,因为它将配置文件放置在正确的位置:target/classes 是类路径的根目录,因为该目录中的所有文件都将复制到 WAR/JAR 文件的 WEB-INF/classes 目录中。

2) 要更改属性文件的名称,您可以使用 spring.config.name 属性,方法是将其显式传递为 -Dspring.config.name=myapp 或环境变量 SPRING_CONFIG_NAME=myapp。更多信息您可以在 Change the location of external properties of an application 中找到官方文档的章节。

关于java - Spring boot,环境特定属性位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38919090/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com