Using Spring Boot @ConfigurationProperties and @Profile

In this tutorial you will learn how to use @ConfigurationProperties together with Spring Boot Profiles and @Profile annotation.

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, then 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 very short tutorial that will show you how to use basic Spring Boot Profiles in your RESTful Web Service application.

Let’s assume we have the following two property files:

application.properties file for a Default Profile

database.name = photo_app
database.url = jdbc:mysql://localhost:3306/${database.name}
database.username = "developer"
database.password = "12345678"

application-production.property file for a Production Profile 

database.name = photo_app_production
database.url = jdbc:mysql://localhost:3306/${database.name}
database.username = "production"
database.password = "1fhFy4Yr73H4Y9f"

and our goal is to use application.properties file for a Default profile and to use application-production.properties 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) {
        this.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 

import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties("database")
public class AppConfigurationDev extends DatabaseConfiguration {
}

Java Class for a Production Profile

import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties("database")
public class AppConfigurationProd extends DatabaseConfiguration {   
}

Using @Profile Annotation

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

@SpringBootApplication
public class ProfilesTutorialApplication {

    public static void main(String[] args) {
        SpringApplication.run(ProfilesTutorialApplication.class, args);
    }

    @Profile("production")
    @Bean
    public DatabaseConfiguration getProdDatabaseConfiguration() {
        return new AppConfigurationProd();
    }

    @Profile("developer")
    @Bean
    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

To start your Spring Boot application for a specific profile you can use the following commands.

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

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

mvn spring-boot:run -Dspring-boot.run.arguments=--spring.profiles.active=production

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

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

mvn spring-boot:run -Dspring-boot.run.arguments=--spring.profiles.active=developer

I hope this Spring Boot tutorial was helpful for 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.