Spring Boot Tutorial 0/110 lessons ~6 min read Lesson 50

    Spring Boot Profiles

    Spring profiles let one Orders binary behave differently in local, CI, staging, and production without rebuilding.

    Course progress0%
    Focus
    12 guided sections
    Practice signal
    Examples included
    Career prep
    Interview Q&A included

    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 prod from prod-db + prod-cache when useful.
    • fail-safe: prod forbids dangerous defaults like ddl-auto: update.

    Step-by-step explanation

    1. Create application.yml with shared server and app name settings.
    2. Add application-dev.yml (verbose logging, H2 or local Postgres) and application-prod.yml (validate schema, INFO logs).
    3. Mark mock integrations with @Profile("!prod") and real ones with @Profile("prod").
    4. Start with SPRING_PROFILES_ACTIVE=dev locally and verify prod never enables ddl update.

    Informative example

    Orders profile YAML and profile-scoped email beans.

    java
    # application.yml
    spring:
    application:
    name: orders-service
    ---
    spring:
    config:
    activate:
    on-profile: dev
    jpa:
    hibernate:
    ddl-auto: update
    logging:
    level:
    com.techlearningpro.orders: DEBUG
    ---
    spring:
    config:
    activate:
    on-profile: prod
    jpa:
    hibernate:
    ddl-auto: validate
    logging:
    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_ACTIVE in prod as a startup failure.
    • Keep secrets out of profile YAML — reference env placeholders.
    • Use @Profile for 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.

    3 questions
    1BeginnerQuestionHow do you activate a Spring Boot profile?+

    Answer

    Set spring.profiles.active via env (SPRING_PROFILES_ACTIVE), CLI, or config; Boot loads matching application-{profile}.yml documents.
    2IntermediateQuestionWhat does @Profile("!prod") mean?+

    Answer

    The bean is registered in every active profile set except when prod is active.
    3IntermediateQuestionWhy avoid ddl-auto=update in prod?+

    Answer

    Hibernate may alter live schema unpredictably; prod should validate schema and apply migrations intentionally (Flyway/Liquibase).

    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.

    Ready to mark this lesson complete?Track your journey across the entire course.