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:

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 the application.properties file for a Default profile and the 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. We want to load a default application.properties file if no profile was specified. 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

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 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 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. Required fields are marked *