Profiles & Environments

Profiles let one build behave differently per environment. Tag beans with @Profile , layer application-<profile>.properties , and flip everything with spring.profiles.active .

Learn Profiles & Environments in our free Java course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.

Part of the free Java course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

You should understand configuration and properties and how dependency injection picks beans. Profiles build directly on both.

💡 Analogy: Think of your app as a theater stage and profiles as scene sets . The same actors (your code) perform on one stage, but a stagehand rolls in the "dev" scenery (a cardboard prop mailer, bright debug lights) or the "prod" scenery (the real backdrop, dimmed lights) depending on the show. spring.profiles.active is the cue that tells the stagehand which set to use; @Profile labels which props belong to which scene.

Build the play once; change the scenery per performance.

application.properties is the base; application-dev.properties and application-prod.properties layer overrides on top when their profile is active. Activate one with spring.profiles.active .

Annotate competing implementations with @Profile . A service depends on the interface ; Spring injects whichever bean matches the active profile — no environment if/else in your logic.

Under the hood, Spring just picks the bean whose profile matches spring.profiles.active . Here is that selection logic in plain, runnable Java.

Answer: spring.profiles.active=prod (settable via file, CLI arg, or env var).

Answer: application-dev.properties , layered on top of the base file.

Answer: the default profile — base config and any non-profiled beans.

🎯 YOUR TURN — Sandbox vs live gateway

Provide two PaymentGateway implementations chosen by @Profile("dev") and @Profile("prod") .

🧩 MINI-CHALLENGE — Layered property files

Design base and per-profile property files, then activate prod with an environment variable.

You can now define profiles, restrict beans and @Bean methods with @Profile , layer application-<profile>.properties over the base, and select the active environment with spring.profiles.active via files, CLI args, or environment variables.

Next up: Spring Security Basics — protecting your endpoints with authentication and authorization.

Practice quiz

What does a Spring profile represent?

  • A user account
  • A named set of beans and config active in a given environment
  • A database index
  • A logging level only

Answer: A named set of beans and config active in a given environment. A profile is a named grouping of beans and configuration activated for a particular environment (dev, test, prod).

What does @Profile("dev") on a bean mean?

  • The bean is always created
  • The bean is deleted in dev
  • The bean is created only when the dev profile is active
  • The bean runs twice

Answer: The bean is created only when the dev profile is active. @Profile("dev") makes the bean eligible for creation only when the dev profile is active.

Which property selects the active profile(s)?

  • spring.active.profile
  • spring.profiles.active
  • profile.spring.use
  • server.profile

Answer: spring.profiles.active. spring.profiles.active lists the profile(s) Spring should activate.

What file naming convention provides per-profile properties?

  • application-<profile>.properties
  • profile.<name>.properties
  • <profile>.application.properties
  • app.<profile>.yml only

Answer: application-<profile>.properties. application-dev.properties (etc.) supplies profile-specific overrides loaded on top of application.properties.

If no profile is explicitly active, what runs?

  • Nothing
  • Every profile at once
  • Only prod
  • The default profile / non-profiled beans and base config

Answer: The default profile / non-profiled beans and base config. With no active profile, Spring uses the default profile: base config and beans not restricted to a profile.

Can more than one profile be active at the same time?

  • No, only one
  • Yes, you can activate multiple profiles together
  • Only two maximum
  • Only in tests

Answer: Yes, you can activate multiple profiles together. spring.profiles.active accepts a comma-separated list, so several profiles can be active together.

How can you activate a profile at launch without editing files?

  • You cannot
  • Set spring.profiles.active via an env var or --spring.profiles.active arg
  • Rebuild the JAR
  • Rename the main class

Answer: Set spring.profiles.active via an env var or --spring.profiles.active arg. An environment variable or command-line argument can set spring.profiles.active at startup.

A good use of @Profile is to...

  • Provide a fake email sender in dev and a real one in prod
  • Define the primary key
  • Replace validation
  • Speed up the JVM

Answer: Provide a fake email sender in dev and a real one in prod. Profiles let you swap implementations per environment, e.g. a no-op/fake sender in dev versus a real one in prod.

Which is true about per-profile property files?

  • They replace application.properties entirely
  • They override matching keys on top of the base application.properties
  • They are ignored
  • They must be YAML

Answer: They override matching keys on top of the base application.properties. Profile-specific files override only the keys they define, layered on top of the base properties.

Where can @Profile be applied besides a @Component bean?

  • Only on fields
  • On a @Bean method or a whole @Configuration class
  • Only on the main method
  • Nowhere else

Answer: On a @Bean method or a whole @Configuration class. @Profile can also restrict individual @Bean methods or an entire @Configuration class to an active profile.