Different property variable for Local and prod Environment (Spring)

后端 未结 5 1056
梦如初夏
梦如初夏 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:43

    One solution that does not involve Spring Profiles at all is to use something like the following:

    
    

    What this does is tell Spring to look for properties using two files, the file in your jar/war, and one that can be anywhere in the file system. The ignore-resource-not-found means that if one of the files is not found, Spring won't complain.

    Using this setup the second file can be controlled by DevOps people and can contain anything of their choosing, thus overriding any properties in your classpath properties file file.

    UPDATE:

    You could do the same with Java Config using the following bean in your configuration class

    @Bean
    public static PropertySourcesPlaceholderConfigurer properties() {
        final PropertySourcesPlaceholderConfigurer pspc = new PropertySourcesPlaceholderConfigurer();
    
        Resource[] resources = new Resource[ ] {
                new ClassPathResource( "config.properties" ),
                new FileSystemResource("/path/config/config.properties")
        };
    
        pspc.setLocations( resources );
        pspc.setIgnoreResourceNotFound(true);
        pspc.setIgnoreUnresolvablePlaceholders(false);
        return pspc;
    }
    

提交回复
热议问题