6.3.2 Activating profiles
Setting profile-specific properties will do no good unless those profiles are active. But how can you make a profile active? All it takes to make a profile active is to include it in the list of profile names given to the spring.profiles.active property. For example, you could set it in application.yml like this:
spring:
profiles:
active:
- prodBut that’s perhaps the worst possible way to set an active profile. If you set the active profile in application.yml, then that profile becomes the default profile, and you achieve none of the benefits of using profiles to separate the production-specific properties from development properties. Instead, I recommend that you set the active profile(s) with environment variables. On the production environment, you would set SPRING_PROFILES_ACTIVE like this:
% export SPRING_PROFILES_ACTIVE=prodFrom then on, any applications deployed to that machine will have the prod profile active, and the corresponding configuration properties would take precedence over the properties in the default profile.
If you’re running the application as an executable JAR file, you might also set the active profile with a command-line argument like this:
% java -jar taco-cloud.jar --spring.profiles.active=prodNote that the spring.profiles.active property name contains the plural word profiles. This means you can specify more than one active profile. Often, this is with a comma-separated list as when setting it with an environment variable, as shown here:
% export SPRING_PROFILES_ACTIVE=prod,audit,haBut in YAML, you’d specify it as a list like this:
spring:
profiles:
active:
- prod
- audit
- haIt’s also worth noting that if you deploy a Spring application to Cloud Foundry, a profile named cloud is automatically activated for you. If Cloud Foundry is your production environment, you’ll want to be sure to specify production-specific properties under the cloud profile.
As it turns out, profiles aren’t useful only for conditionally setting configuration properties in a Spring application. Let’s see how to declare beans specific to an active profile.
