Spring Boot Profiles
Spring profiles let one Orders binary behave differently in local, CI, staging, and production without rebuilding.
Introduction
Spring profiles let one Orders binary behave differently in local, CI, staging, and production without rebuilding.
On Boot 3, activate with SPRING_PROFILES_ACTIVE=prod, --spring.profiles.active=prod, or spring.config.activate.on-profile in YAML. Shared defaults live in application.yml; overrides live in application-dev.yml / application-prod.yml.
Beans can be profile-scoped with @Profile — mock email in dev, SES in prod. Never activate a debug profile in production by accident.
This lesson wires TechLearningPRO Orders so ddl-auto, logging, and integrations flip safely per environment.
Purpose of this lesson
Structure Orders config and beans so one Boot 3 artifact serves every environment via profiles.
Understanding the topic
Profiles select property files and optional beans for an environment.
- activation: env var, CLI flag, or default profile in YAML.
- layered YAML: base + profile-specific documents.
- @Profile: include or exclude beans (
prod,!prod). - groups: compose
prodfromprod-db+prod-cachewhen useful. - fail-safe: prod forbids dangerous defaults like
ddl-auto: update.
Step-by-step explanation
- Create
application.ymlwith shared server and app name settings. - Add
application-dev.yml(verbose logging, H2 or local Postgres) andapplication-prod.yml(validate schema, INFO logs). - Mark mock integrations with
@Profile("!prod")and real ones with@Profile("prod"). - Start with
SPRING_PROFILES_ACTIVE=devlocally and verify prod never enables ddl update.
Informative example
Orders profile YAML and profile-scoped email beans.
# application.ymlspring:application:name: orders-service---spring:config:activate:on-profile: devjpa:hibernate:ddl-auto: updatelogging:level:com.techlearningpro.orders: DEBUG---spring:config:activate:on-profile: prodjpa:hibernate:ddl-auto: validatelogging:level:com.techlearningpro.orders: INFO@Service@Profile("!prod")public class LoggingEmailSender implements EmailSender {public void send(OrderPlacedMail mail) {log.info("DEV mail to={} orderId={}", mail.to(), mail.orderId());}}@Service@Profile("prod")public class SmtpEmailSender implements EmailSender {private final JavaMailSender mailer;public SmtpEmailSender(JavaMailSender mailer) { this.mailer = mailer; }public void send(OrderPlacedMail mail) { /* MimeMessageHelper send */ }}
Real-world use
A TechLearningPRO staging deploy once reused the default profile and ran ddl-auto: update against a shared database, altering columns mid-demo. Splitting profile documents and requiring SPRING_PROFILES_ACTIVE in the deployment manifest stopped silent schema mutations.
Security implications
Wrong profile activation can expose debug endpoints or weak security.
- Never ship a profile that permits all requests or prints JWTs.
- Prod profile should disable verbose actuator exposure.
- Review profile-scoped beans in every release checklist.
Best practices
- Treat missing
SPRING_PROFILES_ACTIVEin prod as a startup failure. - Keep secrets out of profile YAML — reference env placeholders.
- Use
@Profilefor alternate implementations, not for business rules.
Common mistakes
- Committing machine-specific profile files with real hostnames and credentials.
- Leaving
@Profile("dev")security bypass beans that somehow activate in prod. - Overlapping property keys across profiles without documenting which wins.
Advanced interview questions
Interview Prep
Practice concise answers, then expand each card for the explanation.
1BeginnerQuestionHow do you activate a Spring Boot profile?+
Answer
spring.profiles.active via env (SPRING_PROFILES_ACTIVE), CLI, or config; Boot loads matching application-{profile}.yml documents.2IntermediateQuestionWhat does @Profile("!prod") mean?+
Answer
prod is active.3IntermediateQuestionWhy avoid ddl-auto=update in prod?+
Answer
Hands-on exercise
Lab. Add dev and prod documents for Orders. Under dev, bind a mock EmailSender; under prod, bind SMTP. Start twice with different active profiles and assert the correct bean via a simple endpoint or log.
Summary
Spring Boot profiles make TechLearningPRO Orders environment-aware: YAML layers and @Profile beans keep local speed without compromising production.