1

I'd like to accomplish this: Environment Specific application.properties file in Spring Boot application

in a Spring non-Boot application. Any idea on how to do that? Now I am setting environment variables to tell the application which properties to use, would prefer to do it the "boot" way.

Help would be appreciated.

Community
  • 1
  • 1
why_vincent
  • 2,172
  • 9
  • 33
  • 49

2 Answers2

0

You can add application-environment.properties as per environment. Spring boot should automatically detect the corresponding properties file based on active environment.

sandeshch
  • 11
  • 5
  • In Spring boot it is always based on the profile, which is also an environment property which you need to set. I don't think of any other way possible. I would like to follow the question, if anything new comes, would be interesting. – sandeshch Apr 12 '17 at 09:08
0

In order to represent the several environments use profiles. If you want to know more browse this site. and I think this is exactly what you are looking for.

Update 1:

Considering you have a fixed suffix of your property files and you have a set of property files for different environment, for example,

development-it_wroks.properties, test-it_wroks.properties etc. etc.

etc.it_wroks is the suffix

Determine the active enviourment from active_env.properties

profiles.active: development
#profiles.active: test
#profiles.active: stage
#profiles.active: production

Write a custom Property resolver

import org.apache.commons.configuration2.Configuration;
import org.apache.commons.configuration2.FileBasedConfiguration;
import org.apache.commons.configuration2.PropertiesConfiguration;
import org.apache.commons.configuration2.builder.FileBasedConfigurationBuilder;
import org.apache.commons.configuration2.builder.fluent.Parameters;
public class MyPropertyUtil {
    public static String getValuesFromPerpertyFile(String filename,String key){
        String value = null;
        Configuration config = getConfiguration(filename);
        value = config.getString(key);

        return value;

    }

    public static Configuration getConfiguration(String file){

        Configuration config = null;

        try{
            Parameters params = new Parameters();
            FileBasedConfigurationBuilder<FileBasedConfiguration> 
                builder =new FileBasedConfigurationBuilder
                <FileBasedConfiguration>(PropertiesConfiguration.class)
                .configure(params.properties().setFileName(file));

            config = builder.getConfiguration();

        }catch(Exception ex){
            ex.printStackTrace();
        }finally{

        }

        return config;
    }

}

Now your calling class

import org.apache.log4j.Logger;
public class MyCallingClass {

    final static Logger logger = Logger.getLogger(this.getClass());

    //Determine the active enviourment,You may determine this from os environment variable if you want
    String activeEnvironment = MyPropertyUtil.
            getValuesFromPerpertyFile("resource/active_env.properties"
                    ,"profiles.active");

    //Set the property file
    String myEnvSpecificValue = MyPropertyUtil.
            getValuesFromPerpertyFile("resource/"+activeEnvironment+"it_wroks.properties",
                    "my.property.string");

    //Do what you want to
    logger.info(myEnvSpecificValue);

}
Arko
  • 902
  • 12
  • 23