Beast
Beast

Reputation: 629

Spring Boot Profiles Not Picking Properties Files

We are working on Spring Boot 2.1.6 and we need to implement spring boot profile in our application

We have currently two property files application.properties and bucket.properties(s3 configuration) file in our project.

so we have created the two properties files resources/application-dev.properties and resources/bucket-dev.properties file for Dev environment.

We are passing the VM arguments in the below program -Dspring.profiles.active=dev so that it pickup the files correctly.

We are using XML based configuration and hence we are loading the property file using below definition.

<bean id="applicationProperties"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" >
        <property name="locations" >
            <list>
                <value>classpath:application-${spring.profiles.active:dev}.properties</value>
                <value>classpath:bucket-${spring.profiles.active:dev}.properties</value>
            </list>
        </property>
</bean>

Above configuration is working fine and spring boot is able to pickup the files properly.

But I want to create the below kind of folder structure in resources folder to segregate the files properly.

|
resources
        |dev
            |
            application-dev.properties  
            bucket-dev.properties

As soon as I do that I have made changes in the above PropertyPlaceholderConfigurer like below.

<bean id="applicationProperties"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" >
        <property name="locations" >
            <list>
                <value>classpath:${spring.profiles.active}/application-${spring.profiles.active:dev}.properties</value>
                <value>classpath:${spring.profiles.active}/bucket-${spring.profiles.active:dev}.properties</value>
            </list>
        </property>
</bean>

As soon as I start the application using the above configuration it is failed to locate the properties define inside the above file and failing to start App.

Please let me know what I am missing in the above configuration.

Note: We are not using Annotation based configuration in Spring Boot App but using only XML based configuraton.

Upvotes: 1

Views: 3208

Answers (1)

pvpkiran
pvpkiran

Reputation: 27018

Springboot will not do this out of the box, But you can use PropertySourcesPlaceholderConfigurer to do so.

@Configuration
public class PropertyFileLoaderConfig {

    private static final Logger LOG = LoggerFactory.getLogger(PropertyFileLoaderConfig.class);

    private static final String PROFILE_DEV = "dev";
    private static final String PROFILE_STAGE = "stage";
    private static final String PROFILE_PROD = "prod";

    private static final String PATH_TEMPLATE = "classpath*:%s/*.properties";

    @Bean
    @Profile(PROFILE_DEV)
    public static PropertySourcesPlaceholderConfigurer devPropertyPlaceholderConfigurer() throws IOException {
        LOG.info("Initializing {} properties.", PROFILE_DEV);
        PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
        configurer.setLocations(new PathMatchingResourcePatternResolver().getResources(getResourcesFromPath(PROFILE_DEV)));//Loads all properties files from the path
        configurer.setIgnoreUnresolvablePlaceholders(true);

        return configurer;
    }

    @Bean
    @Profile(PROFILE_STAGE)
    public static PropertySourcesPlaceholderConfigurer stagePropertyPlaceholderConfigurer() throws IOException {
        LOG.info("Initializing {} properties.", PROFILE_STAGE);
        PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
        configurer.setLocations(new PathMatchingResourcePatternResolver().getResources(getResourcesFromPath(PROFILE_STAGE)));

        return configurer;
    }

    @Bean
    @Profile(PROFILE_PROD )
    public static PropertySourcesPlaceholderConfigurer prodPropertyPlaceholderConfigurer() throws IOException {
        LOG.info("Initializing {} properties.", PROFILE_PROD );
        PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
        configurer.setLocations(new PathMatchingResourcePatternResolver().getResources(getResourcesFromPath(PROFILE_PROD )));

        return configurer;
    }

    private static String getResourcesFromPath(String path) {
        return PATH_TEMPLATE.replaceFirst("%s", path);
    }
}

Upvotes: 1

Related Questions