加载属性文件取决于弹簧轮廓

时间:2017-04-26 21:43:06

标签: spring-profiles

我想使用PropertyPlaceholderConfiguration加载不同的属性文件取决于在启动Web应用程序时传递的spring.profiles.active。我有不同的阶段,分为两组。当spring profile是' prod'时读取application.properties,否则读取application-dev.properties文件。

当我启动非prod阶段时,developerPropertyPlaceholderConfigurer()调用,"开发属性读取"打印出来,我想应该加载application-dev。但是当我使用@Value(" $ {aws.key}")来读取值时,它的application.properties'值。

我不知道错误

忘记提及我使用spring-boot。

我做了很少测试,让我们说两个文件中都有两个相同的属性名称。 aws.key = dev在默认文件中的dev文件aws.key = prod中。即使我主动开发阶段,也始终读入application.properties中的aws.key = prod。但是,如果我删除' aws.key'在application.properties中,然后读入了aws.key = dev。我认为appliaction-dev.properties文件已被读入,然后spring boot read application.properties再次覆盖相同的属性,即使我不想让spring boot读取application.property在非prod阶段。怎么解决?

@Configuration 
public class PropertyPlaceholderConfiguration {
    @Bean
    @Profile({"test","qa","demo","dev","AWS","localhost"})
    public static PropertySourcesPlaceholderConfigurer developmentPropertyPlaceholderConfigurer() {
        System.out.println("Development properties read");
        PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
        configurer.setIgnoreUnresolvablePlaceholders(Boolean.TRUE);
        configurer.setLocation(new ClassPathResource("application-dev.properties"));
        return configurer;
    }

    @Bean
    @Profile("prod") // The default
    public static PropertySourcesPlaceholderConfigurer propertyPlaceholderConfigurer() {
        System.out.println("Production properties read");
        PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
        configurer.setIgnoreUnresolvablePlaceholders(Boolean.TRUE);
        configurer.setLocation(new ClassPathResource("application.properties"));
        return configurer;
    } }

0 个答案:

没有答案
相关问题