Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can the Spring active profiles be set through a property file?

I want to be able to read the active profiles from a property file so that different environments (dev,prod etc) can be configured with different profiles in a Spring MVC based web application. I know that the active profiles can be set through JVM params or system properties. But I would like to do it through a property file instead. The point is that I dont know the active profile statically and instead want to read it from a properties file. It looks like this is not possible. For eg., if I had 'spring.profiles.active=dev' in application.properties, and allow it to be overridden in override.properties like so:

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="ignoreResourceNotFound" value="true" />
    <property name="locations">
        <list>
            <value>classpath:/application.properties</value>
            <value>file:/overrides.properties</value>
        </list>
    </property>
</bean> 

the profile is not being picked up in the environment. I guess this is because the active profiles are being checked before bean initialization, and therefore do not honor the property being set in a properties file. The only other option I see is to implement an ApplicationContextInitializer that will load those property files in order of priority(override.properties first if it exists, else application.properties) and set the value in context.getEnvironment(). Is there a better way to do it from properties files?

like image 411
Navin Viswanath Avatar asked Aug 12 '15 22:08

Navin Viswanath


People also ask

What is the property used to specify a profile while running a Spring Boot application?

The spring. profiles. active property follows the same ordering rules as other properties, the highest PropertySource will win. This means that you can specify active profiles in application.

Which property needs to be added in the application properties file to activate a particular profile?

active properties to activate or include profiles from an application.

Where are Spring profiles stored?

Profiles work in conjunction with Spring Boot properties files. By default, Spring Boot parses a file called application. properties – located in the src/main/resources directory – to identify configuration information.

Which is the property used to set the application name in Spring?

So in a spring boot application, application. properties file is used to write the application-related property into that file. This file contains the different configuration which is required to run the application in a different environment, and each environment will have a different property defined by it.


1 Answers

One solution to do it is to read necessary property file with specified profile "manually" - without spring - and set profile at context initialization:

1) Write simple properties loader:

import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.util.Properties;

public class PropertiesLoader
{
    private static Properties props;

    public static String getActiveProfile()
    {
        if (props == null)
        {
            props = initProperties();
        }
        return props.getProperty("profile");
    }

    private static Properties initProperties()
    {
        String propertiesFile = "app.properties";
        try (Reader in = new FileReader(propertiesFile))
        {
            props = new Properties();
            props.load(in);
        }
        catch (IOException e)
        {
            System.out.println("Error while reading properties file: " + e.getMessage());
            return null;
        }
        return props;
    }
}

2) Read profile from properties file and set it during Spring container initialization (example with Java-based configuration):

public static void main(String[] args)
{
    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
    ctx.getEnvironment().setActiveProfiles(PropertiesLoader.getActiveProfile());
    ctx.register(AppConfig.class);
    ctx.refresh();

    // you application is running ...

    ctx.close();
}
like image 80
Ivan Pronin Avatar answered Sep 22 '22 02:09

Ivan Pronin