简体   繁体   English

如何在Spring Boot中设置默认环境

[英]How to set default environment in Spring Boot

How can I set the default environment in Spring Boot? 如何在Spring Boot中设置默认环境?

I put in application.properties : 我放入application.properties

spring.profiles.include=prod,dev
spring.profiles.active=prod

and in user variables: 在用户变量中:

SPRING_PROFILES_ACTIVE = 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) 因此,当我开发我的环境时,我的环境是dev,但是当我生成我的.war并在tomcat中部署时,他仍然使用dev环境(我在另一个comp中部署而没有其他conf)

How can I set my default environment (if he don't find any in user variables or commmand line uses production)? 如何设置我的默认环境(如果他在用户变量或命令行中没有找到任何产品)?

Take a look at the Spring Boot guide on profile specific properties 查看配置文件特定属性的Spring Boot指南

http://docs.spring.io/spring-boot/docs/1.3.0.BUILD-SNAPSHOT/reference/htmlsingle/#boot-features-external-config-profile-specific-properties http://docs.spring.io/spring-boot/docs/1.3.0.BUILD-SNAPSHOT/reference/htmlsingle/#boot-features-external-config-profile-specific-properties

I would remove what you have from your application.properties. 我会从你的application.properties中删除你拥有的东西。 You are telling it to include (not replace) both dev and prod profiles. 您告诉它包括(而不是替换)dev和prod配置文件。 A much easier setup is to have 更简单的设置就是拥有

  1. application.properties (if no profile is set) application.properties(如果没有设置配置文件)
  2. application-dev.properties (for dev profile) application-dev.properties(用于开发配置文件)
  3. application-prod.properties (for prod profile) application-prod.properties(用于prod配置文件)

What you need is: 你需要的是:

1) Define the active profile you need, something like: 1)定义您需要的活动配置文件,例如:

spring.profiles.active=dev

In this example the loaded file will be application-dev.properties 在此示例中,加载的文件将是application-dev.properties

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: 现在,如果您需要切换到不同的环境,具体取决于您在哪里安装war文件,您可以做的是将此变量设置为您想要的每个环境中的系统变量,这样每个环境对于同一个键具有不同的值, 就像是:

-Dspring.profiles.active=dev

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. 在我的情况下,我使用Tomcat并在setenv.sh文件中声明此键/值,您需要根据您使用的服务器分配该值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何为运行测试设置默认的Spring Boot配置文件(不使用环境变量)? - How do I set the default Spring Boot profile for running tests (without using environment variables)? 如何在Tomcat开发环境中更改Spring Boot属性的默认路径 - How to change Spring Boot properties default path in Tomcat development environment Gradle测试任务,如何设置Spring Boot环境进行测试? - Gradle test task, how to set the Spring Boot environment for testing? 如何在spring boot中使用环境变量动态设置tableName? - How to set tableName dynamically using environment variable in spring boot? 如何在 Spring Boot 中根据环境/条件设置@Table 名称? - How to set @Table name based on environment / condition in Spring Boot? 如何在 application.yml 中设置环境变量 - Spring Boot - How to set an environment variable in application.yml - Spring Boot 如何在spring boot中设置环境 - How to Environment setup in spring boot 如何在 Spring Boot 中设置惰性初始化的默认行为? - How can I set the default behavior of lazy init in Spring Boot? 如何在Spring Boot中注册和设置默认消息转换器 - How to register and set a default message converter in Spring boot 如何为 Spring 中的对象设置默认表单值 Boot: Thymeleaf - How to set default form values for objects in Spring Boot : Thymeleaf
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM