Skip to lesson

Profiles: dev, test and prod

ShopLite needs a laptop database and a fake SMS sender while you build, and the real database and real SMS on the live server. Keeping two copies of the code would let them drift apart.

Profile files on top of the base file

A profile is a named set of settings and beans, such as dev, test or prod, that Spring Boot switches on at start-up. Its settings live in application-<profile>.properties, read after the base file.

application-dev.properties and application-prod.properties sit next to the base application.properties. Boot reads the base file first, then the active profile's file, whose keys replace the base values.

Example 1 · Base file, dev file and prod file

Runs in: the config-lab practice project (Spring Boot 4.1)

ProfileReport.javaJava
# src/main/resources/application.propertiesspring.main.banner-mode=offlogging.level.root=warnlogging.level.com.shoplite=info
shoplite.db-url=jdbc:mysql://localhost:3306/shopliteshoplite.show-error-details=falseshoplite.support-email=help@shoplite.example
# src/main/resources/application-dev.propertiesshoplite.show-error-details=true
# src/main/resources/application-prod.propertiesshoplite.db-url=jdbc:mysql://db.shoplite.internal:3306/shoplite
// src/main/java/com/shoplite/lab/ProfileReport.javapackage com.shoplite.lab;
import java.util.Arrays;
import org.springframework.boot.CommandLineRunner;import org.springframework.core.env.Environment;import org.springframework.stereotype.Component;
@Componentpublic class ProfileReport implements CommandLineRunner {
    private final Environment env;
    public ProfileReport(Environment env) {        this.env = env;    }
    @Override    public void run(String... args) {        System.out.println("Active profiles: " + Arrays.toString(env.getActiveProfiles()));        System.out.println("Database URL:    " + env.getProperty("shoplite.db-url"));        System.out.println("Error details:   " + env.getProperty("shoplite.show-error-details"));        System.out.println("Support email:   " + env.getProperty("shoplite.support-email"));    }}
Sample output
$ java -jar target/config-lab-0.0.1-SNAPSHOT.jar
2026-10-16T11:47:19.789+05:30  INFO 14720 --- [           main] com.shoplite.lab.ConfigLabApplication    : No active profile set, falling back to 1 default profile: "default"
Active profiles: []
Database URL:    jdbc:mysql://localhost:3306/shoplite
Error details:   false
Support email:   help@shoplite.example
$ java -jar target/config-lab-0.0.1-SNAPSHOT.jar --spring.profiles.active=dev
2026-10-16T11:47:25.736+05:30  INFO 26448 --- [           main] com.shoplite.lab.ConfigLabApplication    : The following 1 profile is active: "dev"
Active profiles: [dev]
Database URL:    jdbc:mysql://localhost:3306/shoplite
Error details:   true
Support email:   help@shoplite.example

What this code does

  • logging.level.com.shoplite=info lets the start-up line about profiles through; the other start-up lines are left out of the output.
  • With no profile, every value comes from the base file. With dev, only show-error-details changes to true; the database URL and email still come from the base file.
  • env.getActiveProfiles() returns the active profile names, and is empty when none is active.

The base file holds everything that is the same everywhere, and each profile file holds only the differences. In Example 1, dev changes one key and prod changes another; every other value comes from the base file. Why not one complete file per environment? A value that is the same everywhere would be copied three times, and sooner or later one copy is forgotten.

The usual profiles:

  • dev: your laptop. Local database, detailed errors and logs.
  • test: automated tests. A test database or none, fake outside services.
  • prod: the live server. Real services, short error messages, values from environment variables.

Activating a profile

spring.profiles.active is the setting that names the active profiles, separated by commas. Like any setting, it can come from a file, an environment variable or the command line.

Activate a profile with spring.profiles.active=prod. The start-up log always shows the result, so you can confirm what Boot really switched on.

Example 2 · Terminal: three ways to activate profiles

Runs in: the config-lab practice project (Spring Boot 4.1)

Terminal
# 1. An environment variable (macOS / Git Bash; PowerShell: $env:SPRING_PROFILES_ACTIVE = "prod")SPRING_PROFILES_ACTIVE=prod java -jar target/config-lab-0.0.1-SNAPSHOT.jar# 2. A system property: two profiles at once, separated by a commajava -Dspring.profiles.active=dev,prod -jar target/config-lab-0.0.1-SNAPSHOT.jar# 3. A command-line argument, with a typo in the namejava -jar target/config-lab-0.0.1-SNAPSHOT.jar --spring.profiles.active=prd
Sample output
$ SPRING_PROFILES_ACTIVE=prod java -jar target/config-lab-0.0.1-SNAPSHOT.jar
2026-10-16T11:47:33.827+05:30  INFO 21232 --- [           main] com.shoplite.lab.ConfigLabApplication    : The following 1 profile is active: "prod"
Active profiles: [prod]
Database URL:    jdbc:mysql://db.shoplite.internal:3306/shoplite
Error details:   false
Support email:   help@shoplite.example
$ java -Dspring.profiles.active=dev,prod -jar target/config-lab-0.0.1-SNAPSHOT.jar
2026-10-16T11:47:38.971+05:30  INFO 12436 --- [           main] com.shoplite.lab.ConfigLabApplication    : The following 2 profiles are active: "dev", "prod"
Active profiles: [dev, prod]
Database URL:    jdbc:mysql://db.shoplite.internal:3306/shoplite
Error details:   true
Support email:   help@shoplite.example
$ java -jar target/config-lab-0.0.1-SNAPSHOT.jar --spring.profiles.active=prd
2026-10-16T11:47:45.539+05:30  INFO 11756 --- [           main] com.shoplite.lab.ConfigLabApplication    : The following 1 profile is active: "prd"
Active profiles: [prd]
Database URL:    jdbc:mysql://localhost:3306/shoplite
Error details:   false
Support email:   help@shoplite.example

What this code does

  • The environment variable is the usual choice on servers: the start command never changes, only the machine's settings do.
  • With dev,prod, the database URL comes from prod and show-error-details=true from dev; a mix like this is almost always a mistake on a real server.
  • prd starts without any warning and silently uses the base values, including the local database URL. Always read the profile line in the log.

The usual ways to set it:

  • SPRING_PROFILES_ACTIVE=prod as an environment variable: the normal choice on a server
  • --spring.profiles.active=dev after the jar name, or -Dspring.profiles.active=dev before -jar
  • the Active profiles field of an IntelliJ IDEA Spring Boot run configuration, or ./mvnw spring-boot:run -Dspring-boot.run.profiles=dev

The log line The following 1 profile is active: "dev" is worth checking after every deployment. A typo such as prd is not an error: Boot activates a profile called prd, finds no file for it and runs with the base values.

Predict the output

Which profile settings apply?

Read the start command and the files, then pick the result.

Round 1 of 3

What values does the app use?

# files in the jar
application.properties
application-prod.properties
# start command
java -jar shoplite.jar --spring.profiles.active=prd
Several profiles are separated by commas. When two active profiles set the same key, the one listed last wins.

@Profile on beans: a fake notifier in dev, the real one in prod

@Profile creates a bean only when the named profile is active, for example @Profile("prod"). A leading ! negates it: @Profile("!prod") means every profile except prod.

@Profile("prod") on a @Component (or a @Bean method) creates that bean only when prod is active. Example 3 gives OrderNotifier a console version for dev and an SMS version for prod.

Example 3 · OrderNotifier with a dev and a prod implementation

Runs in: the config-lab practice project (Spring Boot 4.1)

OrderNotifier.javaJava
package com.shoplite.lab;
public interface OrderNotifier {
    void orderPlaced(int orderId, String customerName);}
package com.shoplite.lab;
import org.springframework.context.annotation.Profile;import org.springframework.stereotype.Component;
@Component@Profile("dev")public class ConsoleOrderNotifier implements OrderNotifier {
    @Override    public void orderPlaced(int orderId, String customerName) {        System.out.println("[console] No SMS sent. Would tell " + customerName + ": order " + orderId + " placed");    }}
package com.shoplite.lab;
import org.springframework.context.annotation.Profile;import org.springframework.stereotype.Component;
@Component@Profile("prod")public class SmsOrderNotifier implements OrderNotifier {
    @Override    public void orderPlaced(int orderId, String customerName) {        System.out.println("[prod] SMS to " + customerName + ": Your ShopLite order " + orderId + " is placed");    }}
package com.shoplite.lab;
import org.springframework.boot.CommandLineRunner;import org.springframework.stereotype.Component;
@Componentpublic class CheckoutRunner implements CommandLineRunner {
    private final OrderNotifier notifier;
    public CheckoutRunner(OrderNotifier notifier) {        this.notifier = notifier;    }
    @Override    public void run(String... args) {        System.out.println("Notifier in use: " + notifier.getClass().getSimpleName());        notifier.orderPlaced(17, "Priya Sharma");    }}
Output
$ java -jar target/config-lab-0.0.1-SNAPSHOT.jar --spring.profiles.active=dev
Notifier in use: ConsoleOrderNotifier
[console] No SMS sent. Would tell Priya Sharma: order 17 placed
$ java -jar target/config-lab-0.0.1-SNAPSHOT.jar --spring.profiles.active=prod
Notifier in use: SmsOrderNotifier
[prod] SMS to Priya Sharma: Your ShopLite order 17 is placed

What this code does

  • CheckoutRunner asks only for an OrderNotifier. Exactly one implementation exists in each profile, so Spring has no choice to make.
  • In dev the console version runs and no SMS is sent. In prod the SMS version is created instead; the console bean does not exist at all.

Example 4 · No profile, no notifier, and the @Profile("!prod") fix

Runs in: the config-lab practice project (Spring Boot 4.1)

ConsoleOrderNotifier.javaJava
// src/main/java/com/shoplite/lab/ConsoleOrderNotifier.java (fixed)package com.shoplite.lab;
import org.springframework.context.annotation.Profile;import org.springframework.stereotype.Component;
@Component@Profile("!prod")public class ConsoleOrderNotifier implements OrderNotifier {
    @Override    public void orderPlaced(int orderId, String customerName) {        System.out.println("[console] No SMS sent. Would tell " + customerName + ": order " + orderId + " placed");    }}
Sample output
$ java -jar target/config-lab-0.0.1-SNAPSHOT.jar   (before the fix: @Profile("dev"))
***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of constructor in com.shoplite.lab.CheckoutRunner required a bean of type 'com.shoplite.lab.OrderNotifier' that could not be found.


Action:

Consider defining a bean of type 'com.shoplite.lab.OrderNotifier' in your configuration.

$ java -jar target/config-lab-0.0.1-SNAPSHOT.jar
Notifier in use: ConsoleOrderNotifier
[console] No SMS sent. Would tell Priya Sharma: order 17 placed
$ java -jar target/config-lab-0.0.1-SNAPSHOT.jar --spring.profiles.active=prod
Notifier in use: SmsOrderNotifier
[prod] SMS to Priya Sharma: Your ShopLite order 17 is placed

What this code does

  • With @Profile("dev") and @Profile("prod"), a run with no profile has no OrderNotifier at all, so CheckoutRunner cannot be created.
  • @Profile("!prod") covers no profile, dev, test and any other name; only prod gets the SMS version. Now every run has exactly one notifier.

Without it, every developer's test order would text a real customer, and every test run would cost money. With it, the service code depends only on OrderNotifier and never knows which one it got.

Make sure every possible profile gets exactly one implementation, or start-up fails with required a bean of type ... that could not be found, as Example 4 shows.

Find the bug

Find the profile bug

Each snippet misbehaves. Tap the wrong line, then check the fix.

A run with no profile fails: no bean of type OrderNotifier.

Round 1 of 3

The test profile with @ActiveProfiles

@ActiveProfiles on a test class activates the named profiles for the Spring context that the test starts, for example @ActiveProfiles("test").

@ActiveProfiles("test") on a test class switches the test profile on for that test. Boot then also reads application-test.properties, usually kept in src/test/resources.

Example 5 · TestProfileTest.java with @ActiveProfiles("test")

Runs in: the config-lab practice project with spring-boot-starter-test (Spring Boot 4.1)

Java
// src/test/java/com/shoplite/lab/TestProfileTest.javapackage com.shoplite.lab;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.jupiter.api.Test;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.core.env.Environment;import org.springframework.test.context.ActiveProfiles;
@SpringBootTest@ActiveProfiles("test")class TestProfileTest {
    @Autowired    private Environment env;
    @Autowired    private OrderNotifier notifier;
    @Test    void usesTheTestDatabaseAndTheFakeNotifier() {        assertThat(env.getActiveProfiles()).containsExactly("test");        assertThat(env.getProperty("shoplite.db-url")).endsWith("/shoplite_test");        assertThat(notifier).isInstanceOf(ConsoleOrderNotifier.class);    }}
# src/test/resources/application-test.propertiesshoplite.db-url=jdbc:mysql://localhost:3306/shoplite_testshoplite.support-email=test@shoplite.example
Sample output
$ ./mvnw test
...
2026-10-16T11:50:36.665+05:30  INFO 16556 --- [           main] com.shoplite.lab.TestProfileTest         : The following 1 profile is active: "test"
Active profiles: [test]
Database URL:    jdbc:mysql://localhost:3306/shoplite_test
Error details:   false
Support email:   test@shoplite.example
Notifier in use: ConsoleOrderNotifier
[console] No SMS sent. Would tell Priya Sharma: order 17 placed
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 10.73 s -- in com.shoplite.lab.TestProfileTest
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] BUILD SUCCESS

What this code does

  • The test needs spring-boot-starter-test with test scope in pom.xml (Spring Initializr adds it). ./mvnw test (.\mvnw test on Windows) runs it.
  • The runners from the earlier examples run inside the test too, which is why their lines appear. The assertions check the active profile, the test database URL and the console notifier.
  • The file lives in src/test/resources, so it is on the classpath only during tests and never inside the jar.

The test profile keeps tests away from real systems: a separate test database, fake notifiers and payment gateways, short timeouts. It also proves your wiring works, because the test fails if the wrong bean or value is loaded. The file in src/test/resources never ends up in the production jar.

In test classes, @Autowired on fields is normal: JUnit, not Spring, creates the test object, so constructor injection is not used there. Your application classes still use constructor injection only.

Profile groups and the default profile

A profile group turns one profile name into several: spring.profiles.group.prod=prod-db,prod-mail. The default profile is separate: it applies only when no profile is active.

With the group above, activating prod also activates prod-db and prod-mail. The default profile applies only when no profile is active, and spring.profiles.default renames it.

Example 6 · A prod group and dev as the default profile

Runs in: the config-lab practice project (Spring Boot 4.1)

Java
# src/main/resources/application.propertiesspring.main.banner-mode=offlogging.level.root=warnlogging.level.com.shoplite=info
spring.profiles.default=devspring.profiles.group.prod=prod-db,prod-mail
shoplite.db-url=jdbc:mysql://localhost:3306/shopliteshoplite.show-error-details=falseshoplite.support-email=help@shoplite.example
# src/main/resources/application-dev.propertiesshoplite.show-error-details=true
# src/main/resources/application-prod-db.propertiesshoplite.db-url=jdbc:mysql://db.shoplite.internal:3306/shoplite
# src/main/resources/application-prod-mail.propertiesshoplite.support-email=orders@shoplite.example
Sample output
$ java -jar target/config-lab-0.0.1-SNAPSHOT.jar
2026-10-16T11:48:45.431+05:30  INFO 19152 --- [           main] com.shoplite.lab.ConfigLabApplication    : No active profile set, falling back to 1 default profile: "dev"
Active profiles: []
Default profiles: [dev]
Database URL:    jdbc:mysql://localhost:3306/shoplite
Error details:   true
Support email:   help@shoplite.example
$ java -jar target/config-lab-0.0.1-SNAPSHOT.jar --spring.profiles.active=prod
2026-10-16T11:48:52.436+05:30  INFO 28300 --- [           main] com.shoplite.lab.ConfigLabApplication    : The following 3 profiles are active: "prod", "prod-db", "prod-mail"
Active profiles: [prod, prod-db, prod-mail]
Default profiles: [dev]
Database URL:    jdbc:mysql://db.shoplite.internal:3306/shoplite
Error details:   false
Support email:   orders@shoplite.example

What this code does

  • A plain run falls back to the default profile dev, so application-dev.properties is read. getActiveProfiles() stays empty; getDefaultProfiles() shows dev.
  • Activating prod also activates its group prod-db and prod-mail, so both small files are read. There is no application-prod.properties at all.
  • The runner is ProfileReport from Example 1 with one extra line for the default profiles.

Big apps use groups to keep each concern in its own small file. Out of the box the default profile is called default, which is why the log says No active profile set, falling back to 1 default profile: "default". Setting spring.profiles.default=dev in the base file makes a plain run on your laptop behave like dev, while any real activation (such as SPRING_PROFILES_ACTIVE=prod) replaces it.

Do not put spring.profiles.active inside a profile-specific file: Boot refuses with InvalidConfigDataPropertyException ... is invalid in a profile specific resource. Profiles are chosen before those files are read.

Next step

Check your understanding

11 quick questions