Java Spring PropertySource拒绝多次扩展变量

Java Spring PropertySource拒绝多次扩展变量,java,spring,spring-java-config,Java,Spring,Spring Java Config,我试图用以下方式创建一个spring配置类 @Configuration @PropertySource(value={"file:${my.dir}/fileone.properties","file:${my.dir}/filetwo.properties"}) @Lazy(value=true) public class SpringBeans { @Autowired Environment env; @Bean public static Prope

我试图用以下方式创建一个spring配置类

@Configuration
@PropertySource(value={"file:${my.dir}/fileone.properties","file:${my.dir}/filetwo.properties"})
@Lazy(value=true)
public class SpringBeans {

    @Autowired
    Environment env;

    @Bean
    public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
        PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer();
        return ppc;
    }

    @Bean.....
}
问题是变量${my.dir}被展开,以便找到fileone.properties,但它会立即抛出

java.io.FileNotFoundException: ${my.dir}/filetwo.properties (No such file or directory)
我使用的是Spring3.1.1.RELEASE和OracleJDK7

这是实现中的错误/限制吗?有什么办法吗


此外,我也找不到在注释中设置ignore unsolvable=true的方法,就像在xml中一样,这是默认情况下使用注释完成的吗?

您可以这样设置ignoreunsolvable:

@Bean
public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
    PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = PropertyLoadHelper.getPropertySourcesPlaceholderConfigurer();
    propertySourcesPlaceholderConfigurer.setIgnoreUnresolvablePlaceholders(true);
    return propertySourcesPlaceholderConfigurer;

}
作为扩展值的变通方法。。你可以尝试使用

@Value(${my.dir})
private String dir;
然后在bean配置中使用它:

propertySourcesPlaceholderConfigurer.setLocations(new ClassPathResource[]{
                new ClassPathResource("config/"+dir+ "filename1" + ".properties"),
                new ClassPathResource("config/"+dir+ "filename2" + ".properties")
        });

正如我在下面的评论中提到的,这是Spring中的一个bug。升级到最新的3.X解决了这个问题。

遗憾的是,由于PropertySourcesPlaceholderConfigurer bean需要是静态的,它无法访问dir字符串变量,并且尝试使该静态无效,因为env值还不可用。这是Spring中的已知错误吗?尝试在PropertySourcePlaceHolderConfigurer周围使用一些帮助器包装类,然后:@Bean public PropertySourcePlaceHolderConfigurer PropertySourcePlaceHolderConfigurer{return PropertyLadhelper.GetPropertySourcePlaceHolderConfigurerDir;}但是该类仍然是静态的,这意味着它仍然无法访问${my.dir}变量,该变量是一个环境变量,在创建静态类之后加载。我错过了什么吗?看起来这是春季的一个bug,刚刚尝试了一个更新的版本,它可以正常工作s