AI Hints
Home›Spring Boot Tutorial›YAML, Profiles and Logging
Spring Boot · Lesson 41 of 120

Profiles

Settings per environment.

⏱ 2 min read▶ Video at 54:20🎓 Free
Watch this lesson in the Spring Boot courseWatch on YouTube ↗Full Spring Boot course

Your laptop, a test server, and the real production server, all need slightly different settings. Profiles make that easy.

  • Laptop
  • Test server
  • Production

How profiles work

A profile is a name, like dev, or prod. Settings for it go in a file named application dash dev dot properties. The main file is always loaded. When a profile is active, its file is loaded too, and its values win. You activate a profile with spring profiles active.

keyvalue
Profilea name: dev, prod
Fileapplication-dev.properties
Loadedmain file, then the profile file
Activatespring.profiles.active=dev

The dev settings

Here's application dash dev dot properties. It changes the shop name, and gives a big discount, for testing.

application-dev.propertiesini
shop.name=Corner Books (TEST)
shop.discount=50

Beans per profile

At Profile also works on beans. Fake mailer only exists when dev is active. Real mailer exists when dev is not active. The exclamation mark means not.

Mailer.javajava
public interface Mailer {
    String send(String to);
}

@Component
@Profile("dev")
class FakeMailer implements Mailer {
    public String send(String to) { return "Pretend email to " + to; }
}

@Component
@Profile("!dev")
class RealMailer implements Mailer {
    public String send(String to) { return "Real email sent to " + to; }
}

Dev is active

The main file sets the shop settings, and activates the dev profile. Run it. The dev values won. The name has test in it, and the discount is fifty. And the fake mailer was used, so no real email goes out, while we develop.

application.propertiesini
shop.name=Corner Books
shop.discount=10
spring.profiles.active=dev
Output
Corner Books (TEST), 50% off
Pretend email to sam@example.com

── app log ──
INFO  BookshopApplication : The following 1 profile is active: "dev"
Also uses application-dev.properties
shop.name=Corner Books (TEST)
shop.discount=50
Also uses Mailer.java
public interface Mailer {
    String send(String to);
}

@Component
@Profile("dev")
class FakeMailer implements Mailer {
    public String send(String to) { return "Pretend email to " + to; }
}

@Component
@Profile("!dev")
class RealMailer implements Mailer {
    public String send(String to) { return "Real email sent to " + to; }
}
Also uses ShopController.java
@RestController
public class ShopController {
    private final Mailer mailer;
    private final ShopProperties shop;

    public ShopController(Mailer mailer, ShopProperties shop) {
        this.mailer = mailer;
        this.shop = shop;
    }

    @GetMapping("/info")
    public String info() {
        return shop.name() + ", " + shop.discount() + "% off\n"
            + mailer.send("sam@example.com");
    }
}
Also uses ShopProperties.java
@ConfigurationProperties("shop")
public record ShopProperties(String name, int discount, List<String> categories) {}
Also uses ShopConfig.java
@Configuration
@EnableConfigurationProperties(ShopProperties.class)
public class ShopConfig {}
Also uses _curl.sh
curl localhost:8080/info

Summary

Profiles load extra settings files, like application dash dev dot properties, and can switch beans on and off with at Profile. Next, logging.

Key takeaways
  • application-dev.properties
  • spring.profiles.active
  • @Profile("dev")
Next up: Logging — messages from your app