How to set default environment in Spring Boot

为君一笑 提交于 2020-08-22 11:56:49

问题


How can I set the default environment in Spring Boot?

I put in 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)

How can I set my default environment (if he don't find any in user variables or commmand line uses production)?


回答1:


Take a look at the Spring Boot guide on 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. You are telling it to include (not replace) both dev and prod profiles. A much easier setup is to have

  1. application.properties (if no profile is set)
  2. application-dev.properties (for dev profile)
  3. application-prod.properties (for prod profile)



回答2:


What you need is:

1) Define the active profile you need, something like:

spring.profiles.active=dev

In this example the loaded file will be 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:

-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.



来源:https://stackoverflow.com/questions/31865442/how-to-set-default-environment-in-spring-boot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!