如何在Spring Boot中设置默认环境 [英] How to set default environment in Spring Boot
问题描述
如何在Spring Boot中设置默认环境?
How can I set the default environment in Spring Boot?
我放入 application.properties
:
spring.profiles.include=prod,dev
spring.profiles.active=prod
以及用户变量中:
SPRING_PROFILES_ACTIVE = dev
因此,当我在comp中进行开发时,我的环境就是dev,但是当我生成 .war
并部署在tomcat中,他仍然在环境中使用 dev
(我部署在没有其他配置的另一个组件中)
So when I dev in my comp my environment are dev but when I generate my .war
and deploy in tomcat he still using dev
with enviroment (I deploy in another comp with no other conf)
如何设置默认环境(如果他在用户变量中找不到任何内容或命令行使用生产方式)?
How can I set my default environment (if he don't find any in user variables or commmand line uses production)?
推荐答案
您需要的是:
1)定义所需的活动配置文件,例如:
1) Define the active profile you need, something like:
spring.profiles.active=dev
在此示例中,加载的文件将为 application-dev.properties
In this example the loaded file will be application-dev.properties
现在,如果您需要根据安装war文件的位置切换到其他环境,则可以在每个所需的环境中将此变量设置为系统变量,这样,每个环境对于相同的键都有不同的值,例如:
Now, if you need to switch to a different environment depending where are you installing your war file what you can do is to set this variable as a system variable in each environment you want, that way each environment has a different value for the same key, something like:
-Dspring.profiles.active=dev
我使用的是Tomcat,并且在文件中声明了此键/值setenv.sh
,您需要根据所使用的服务器分配该值。
In my case I am using Tomcat and I declare this key/value in the file setenv.sh
, you need to assign that value depending on the server you're using.
这篇关于如何在Spring Boot中设置默认环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!