问题
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
- application.properties (if no profile is set)
- application-dev.properties (for dev profile)
- 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