Spring PropertyPlaceHolder Java Config外部属性文件

Roh*_*ain 5 java spring properties classpath spring-el

所以这必须是一些愚蠢的错误,我无法通过.我正在尝试外部化我的属性文件,该文件目前放在我的用户主目录中.我正在使用@PropertySource这样加载属性文件:

@Configuration
@PropertySources(value = { @PropertySource("file:#{systemProperties['user.home']}/.invoice/config.properties") })
public class PropertiesConfig {

    @Bean
    public static PropertySourcesPlaceholderConfigurer propertiesPlaceHolderConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();
    }
}
Run Code Online (Sandbox Code Playgroud)

但不幸的是,这不是加载属性文件.投掷FileNotFoundException.但是,如果我改变路径:

@PropertySources(value = { @PropertySource("file:/home/rohit/.invoice/config.properties") })
Run Code Online (Sandbox Code Playgroud)

它工作正常.这就是早期路径解决的路径.我已经记录下来进行验证.所以在我看来,SpEL没有在@PropertySource注释中得到评估.它应该以这种方式工作吗?

如果是,那么还有其他方法可以读取外部属性文件/home/rohit吗?出于显而易见的原因,我不想给出绝对的道路.我想避免延长PropertyPlaceHolderConfigurer课程.

我尝试的另一个选项是将/home/rohit/.invoice文件夹添加到tomcat类路径.但似乎Spring不使用System Classpath来解析classpath:后缀.有关于此的任何指示?

M. *_*num 3

在注释中@PropertySoureEL 表达式将不起作用。您可以使用占位符${...},但这也仅限于系统或环境变量。但是,当您想要解析用户的主目录时,可以使用${user.home}占位符。

@PropertySource("file:${user.home}/.invoice/config.properties")
Run Code Online (Sandbox Code Playgroud)

这应该按预期工作。