gpt4 book ai didi

java - springboot中如何根据环境中设置的profile加载特定的xml

转载 作者:行者123 更新时间:2023-12-02 03:34:11 25 4
gpt4 key购买 nike

假设我的项目中有 XML 文件,例如 a.xmlb.xmlc.xml

现在我想根据 spring boot 中设置的配置文件加载特定的 XML。
例如 - 如果配置文件是 test1 那么我想加载 a.xml,
____ 如果配置文件是 test2 那么我想加载 b.xml 等等。

如何使用 Spring boot 来实现这一点。任何帮助将非常感激。我是新来的。所以我找不到任何合适的解决方案。

最佳答案

首先,您的配置 xml 应该定义它定义 bean 的配置文件。

第二,所有这些都应该已经加载,spring不会使用特定于未激活的配置文件的定义

file a.xml:
<beans profile="test1">
... your bean definitions that only get activated on profile test1 here
</beans>
file b.xml:
<beans profile="test2">
... your bean definitions that only get activated on profile test2 here
</beans>

现在您的所有 xml 文件都定义了运行的配置,您可以使用所有配置文件启动整个应用程序。

神奇的是:你可以用 Spring Boot 提供的任何方式更改配置文件,无论是环境变量、编程方式、启动时的参数......方式是无穷无尽的

参见profile documentation有关此的信息或baeldung tutorial about profiles

启动后,只有激活的配置文件的bean会在那里,那些具有未激活配置文件的xml的bean将不存在(我猜/认为配置仍将被加载,只是不会为它们创建实例)

关于java - springboot中如何根据环境中设置的profile加载特定的xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56869444/

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