Java 如何在Spring配置中导入用于生产的可选属性文件?

Java 如何在Spring配置中导入用于生产的可选属性文件?,java,spring,Java,Spring,我想将Spring配置为在生产过程中覆盖某些属性,但仅当找到production.properties文件且仅针对其定义的属性时(!)。但是这些限制应该只适用于这个生产文件。所有其他属性文件都是必需的 但是我不能在spring配置中导入两个不同的属性资源。我需要改变什么 @Configuration @PropertySource({"classpath:default.properties"}) @PropertySource({"file:production.properties"}, i

我想将
Spring
配置为在生产过程中覆盖某些属性,但仅当找到
production.properties
文件且仅针对其定义的属性时(!)。但是这些限制应该只适用于这个生产文件。所有其他属性文件都是必需的

但是我不能在spring配置中导入两个不同的属性资源。我需要改变什么

@Configuration
@PropertySource({"classpath:default.properties"})
@PropertySource({"file:production.properties"}, ignoreResourceNotFound = true) //Error: Duplicate annotation

尝试
PropertySources
注释,它是一个容器注释,可聚合多个
PropertySources
注释。

您可以在配置文件中执行以下操作:

@Bean
public static PropertySourcesPlaceholderConfigurer properties() {
    final PropertySourcesPlaceholderConfigurer pspc = new PropertySourcesPlaceholderConfigurer();

    Resource[] resources = new Resource[ ] {
            new ClassPathResource( "default.properties" ),
            new FileSystemResource("production.properties")
    };

    pspc.setLocations( resources );
    pspc.setIgnoreResourceNotFound(true);
    pspc.setIgnoreUnresolvablePlaceholders(false);
    return pspc;
}

这不会将
ignoreSourceNotFound
属性设置为我的所有属性文件吗?我只想将它应用于一个特定的.ignoreSourceNotFound。在本例中,这意味着如果Spring遇到类似@Value(${prop.name})的东西,并且在任何属性文件中都找不到prop.name,那么应用程序将不会启动。这并不意味着所有属性都需要出现在所有文件中