How to load Profile Specific Properties files in Spring Boot

Posted on

In this articles, we will see how to load Profile specific properties files from resource directory in Spring Boot application.

Here, we set up a sample Spring Boot Project and maintains set of configuration files specific to the Profile which would be loaded when the Application runs.

Total four configuration files are created in this sample Project:- – Which is a parent property that always loaded first before Profile specific properties file.

application.${}.properties – This is a Profile specific properties file where ${} is a placeholder of any Env i.e DEV/UAT/PROD. This property file will be loaded right after parent properties. We can keep all Profile related configuration here additionally we can overwrite derived properties of parent property file. – Spring provides this property to enabled active Profile which you have to provide in VM argument with Environment name.





Here, we see Project setup hierarchy and configuration files created under resource folder ( /src/main/resources ).






 file is parent properties file that contains all common properties which will be derived by Profile specifies properties file.

file.upload.size.limit =  1024

remote.https.enabled = false
remote.connection.auth=false  ( When application runs on DEV Profile then load all derived properties of file as well as it own properties ) ( When application runs on UAT Profile then Load and Overwrite properties value if it exists in parent properties file  )

file.upload.size.limit =  102400

remote.https.enabled = true

file.upload.size.limit =  102400

remote.https.enabled = true

PropertiesUtils.Java:- This Utilities Class load all properties file in specified order means first it loads then load Profile specifies properties ( application.${}.properties ).

 * Created by MYPC on 8/4/2017.

package com.spring.example.utils;


        public class PropertiesUtils {

            public static final String SPRING_PROFILES_ACTIVE = "";

            public static void initProperties() {
                String activeProfile = System.getProperty(SPRING_PROFILES_ACTIVE);
                if (activeProfile == null) {
                    activeProfile = "dev";
                PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer
                        = new PropertySourcesPlaceholderConfigurer();
                Resource[] resources = new ClassPathResource[]
                        {new ClassPathResource(""),
                                new ClassPathResource("application-" + activeProfile + ".properties")};


Finally, Main class which read all properties file and print on the Console.

package com.spring.example;

import com.spring.example.utils.PropertiesUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

 * list
 * Created by AJAY KUMAR on 1/9/2017.
public class MainApplication {

    private long size;

    private String remoteConnectionUrl;

    private int remoteConnectionPort;

    private String remoteConnectionUsername;

    private String remoteConnectionPassword;

    private String remoteHttpsEnabled;

    private String envName;

    private String remoteConnectionAuth;

    public static void main(String[] args) {
        ConfigurableApplicationContext context =, args);
        MainApplication mainApplication = context.getBean(MainApplication.class);

    private static void printEnvironmentsProperties(MainApplication MainApplication) {
        StringBuilder stringBuilder = new StringBuilder("Properties Files ......\n");

        stringBuilder.append("").append(" : ").append(MainApplication.envName).append("\n")
                .append("file.upload.size.limit").append(" : ").append(MainApplication.size).append("\n")
                .append("remote.connection.url").append(" : ").append(MainApplication.remoteConnectionUrl).append("\n")
                .append("remote.connection.port").append(" : ").append(MainApplication.remoteConnectionPort).append("\n")
                .append("remote.connection.username").append(" : ").append(MainApplication.size).append("\n")
                .append("remote.connection.password").append(" : ").append(MainApplication.remoteConnectionPassword).append("\n")
                .append("remote.connection.auth").append(" : ").append(MainApplication.remoteConnectionAuth).append("\n")
                .append("remote.https.enabled").append(" : ").append(MainApplication.remoteHttpsEnabled).append("\n");

By default, spring-profiles-active is dev Profile either you set active profile to dev or not, it loads and give below output-

2017-08-05 17:37:35.068 INFO 11548 — [ main] com.spring.example.MainApplication : Started MainApplication in 4.457 seconds (JVM running for 5.036)
Properties Files …… : dev
file.upload.size.limit : 1024
remote.connection.url : localhost
remote.connection.port : 8080
remote.connection.username : 1024
remote.connection.password : Pa$$wOrd
remote.connection.auth : false
remote.https.enabled : false

When you set spring-profiles-active is PROD  in VM options then you will get below output.

2017-08-05 17:41:06.539 INFO 10184 — [ main] com.spring.example.MainApplication : Started MainApplication in 4.095 seconds (JVM running for 4.783)
Properties Files …… : PROD
file.upload.size.limit : 102400
remote.connection.url :
remote.connection.port : 9090
remote.connection.username : 102400
remote.connection.password : Pa$$wOrd@233$
remote.connection.auth : true
remote.https.enabled : true

I hope this article would be helpful!! Thanks.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s