Using Spring Boot @ConfigurationProperties and @Profile

In this tutorial, you will learn how to use @ConfigurationProperties with Spring Boot Profiles.

This tutorial assumes you have a basic knowledge of how to use @ConfigurationProperties in your Spring Boot application. If you are very new to it, please read this tutorial: Spring Boot @ConfigurationProperties Tutorial.

You should also have basic knowledge of Spring Boot Profiles. If you are very new to it, please consider reading this short tutorial showing you how to use basic Spring Boot Profiles in your RESTful Web Service application.

Configuration properties for specific Spring Boot profile

Let’s assume we have the following two property files: file for a Default Profile = photo_app
database.url = jdbc:mysql://localhost:3306/${}
database.username = "developer"
database.password = "12345678" file for a Production Profile = photo_app_production
database.url = jdbc:mysql://localhost:3306/${}
database.username = "production"
database.password = "1fhFy4Yr73H4Y9f"

and our goal is to use the file for a Default profile and the file for a Production profile.

Let’s create Java classes to make it work.

Java Classes To Map a Properties File

Base class

public abstract class DatabaseConfiguration {

    private String name;
    private String url;
    private String username;
    private String password;
    public String getName() {
        return name;
    public void setName(String name) { = name;
    public String getUrl() {
        return url;
    public void setUrl(String url) {
        this.url = url;
    public String getUsername() {
        return username;
    public void setUsername(String username) {
        this.username = username;
    public String getPassword() {
        return password;
    public void setPassword(String password) {
        this.password = password;

Java Class for a Default or a Developer Profile


public class AppConfigurationDev extends DatabaseConfiguration {

Java Class for a Production Profile


public class AppConfigurationProd extends DatabaseConfiguration {   

Using @Profile Annotation

When our application starts, we want to load a Profile specific properties file. We want to load a default file if no profile was specified. But when a production profile was specified, we want to load properties from an file. Here is how we can use @Profile annotation to achieve it.

public class ProfilesTutorialApplication {

    public static void main(String[] args) {, args);

    public DatabaseConfiguration getProdDatabaseConfiguration() {
        return new AppConfigurationProd();

    public DatabaseConfiguration getDevDatabaseConfiguration() {
        return new AppConfigurationDev();


Read below to learn how to start Spring Boot application for a specific Profile.

Starting up Spring Boot Application for a Specific Profile

You can use the following commands to start your Spring Boot application for a specific profile.

Starting up an application for a @Profile(“production”)

The below command will trigger @Profile(“production”) annotation to be used and an file to be loaded.

mvn spring-boot:run

Starting up an application for a @Profile(“developer”)

The below command will trigger @Profile(“developer”) annotation to be used and an or a default file to be loaded.

mvn spring-boot:run

I hope this Spring Boot tutorial was helpful to you. If you enjoy learning by watching step-by-step video lessons, then have a look at the below list of video courses that teach Spring Boot. Each has a few free video lessons to watch.

1 Comment on "Using Spring Boot @ConfigurationProperties and @Profile"

Leave a Reply

Your email address will not be published.