如何在PropertySource中使用相对于用户主目录的路径

时间:2015-03-23 16:57:01

标签: java spring annotations applicationcontext

Spring表达式在PropertySource注释中不起作用。

@PropertySource({ "classpath:application.properties",
        "#{systemProperties['user.home']}/.app.properties" })
@Configuration
public class Config {
@Bean
    public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
        PropertySourcesPlaceholderConfigurer result = new PropertySourcesPlaceholderConfigurer();
        result.setOrder(0);
        return result;
    }
}

1 个答案:

答案 0 :(得分:8)

您可以直接使用file:${user.home}加载用户主目录下的文件:

@PropertySource({ "classpath:application.properties", 
                  "file:${user.home}/.app.properties" })
@Configuration
public class Config {
}