Video in progress
What are profiles (spring.profiles.active) for?
With profiles you run the same code in different environments (dev, test, prod) with different settings and beans.
📖 Answer
Settings are split by environment:
application.yml # shared by everyone
application-dev.yml # local: H2, verbose logging
application-prod.yml # production: PostgreSQL, short logs
Turning one on:
java -jar app.jar --spring.profiles.active=prod
# or
SPRING_PROFILES_ACTIVE=prod java -jar app.jar
You can tie a bean to a profile too:
@Bean
@Profile("dev")
public MailSender fakeMailSender() { return new LoggingMailSender(); } // only writes the email to the log
For prod, keep secret values in env variables, not in the profile file. Don't let profiles multiply — each one needs extra upkeep. And don't check the profile name in code (if (profile == "prod")) — keep the difference in configuration or in a bean.
Sign up to mark studied questions and save your progress.