Different property variable for Local and prod Environment (Spring)

后端 未结 5 1057
梦如初夏
梦如初夏 2020-12-08 00:44

I am working on an Spring web application in where I need to have variable\'s that will have different value in local environment and other value in production environment.<

5条回答
  •  旧巷少年郎
    2020-12-08 01:27

    In my case above any solutions didn't work so I did some google from my end and finally found some working solution for it.

    I've modified the above class as follows:

    @Configuration
    @PropertySource("classpath:application-${spring.profiles.active}.properties")
    public class MyApplicationConfiguration {
    @Bean
    public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
        PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
        return configurer;
    }
    }
    

    Instead of defining dynamic file name inside the method. I am defining the file name as below which does the magic for me.

    @PropertySource("classpath:application-${spring.profiles.active}.properties")

    at the top of the class. By following Bart's solution I was not getting any errors but I was getting all the properties to value null defined inside property files.

    by using my approach spring is able to recognize properties defined in VM arguments and in environment variable both. Note: I am using a non-spring boot project.

提交回复
热议问题