Switching Environments in Spring
Published on 7/12/2013
You can switch environment profiles easily in Spring.
In Windows:
- In Control Panel > System > Advanced System Settings > Advanced > Environment Variables
- Create a new System Variable, named
spring.profiles.activewith a value ofdevelopment - Create a new System Variable, named
JAVA_OPTSwith a value of-Dspring.profiles.active=%spring.profiles.active% - (if
JAVA_OPTSalready exists, edit it and add that value) - Do the same thing on your testing, staging, and production servers, but use different values for
spring.profiles.active, such astesting,staging, orproduction - This way, you can use the same codebase, but use different profiles on different machines
- You can read the
${spring.profiles.active}variable, or use<beans profile="development">in Spring to use different database connections or dependency injections for testing - You can usually change environment variables easily in your IDE, such as here. You’ll probably have to restart your application server however.