Card 03/ 06

ExampleDifficulty: Intermediate1 min

Profiles Example: Switching a Whole Set of Properties at Once

One flag switches every setting a production file defines, not just one of them.

A profile is a named set of properties that only takes effect when you activate it. application-prod.properties sits beside application.properties and contributes nothing at all until the prod profile is active.

The base file and the production file each set the same property:

application.propertiestext
app.greeting=hello from application.properties
application-prod.propertiestext
app.greeting=hello from application-prod.properties

Running the same jar without a profile, and then with --spring.profiles.active=prod:

text
java -jar app.jar
app.greeting = hello from application.properties

java -jar app.jar --spring.profiles.active=prod
app.greeting = hello from application-prod.properties
run in a container: activating the profile is enough, and nothing in the code changed

One flag switched every property application-prod.properties sets, not only this one. In a real application the production file would hold the database address, the logging level and the cache sizes, and all of them change together.

So a profile groups everything one environment needs behind one name. The profile can also be activated with the environment variable SPRING_PROFILES_ACTIVE=prod, which is how most deployment platforms set it. Several profiles can be active at once, separated by commas, and where two profile files disagree the profile listed last wins.