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

Dan*_*kov 4 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;
    }
}
Run Code Online (Sandbox Code Playgroud)

Roh*_*ain 8

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

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