简体   繁体   English

Spring PropertyPlaceHolder Java Config外部属性文件

[英]Spring PropertyPlaceHolder Java Config external properties file

So this has to be some silly mistake, which I've not been able to pass through. 所以这必须是一些愚蠢的错误,我无法通过。 I'm trying to externalize my properties file, currently placed in my user home. 我正在尝试外部化我的属性文件,该文件目前放在我的用户主目录中。 I'm loading the properties file using @PropertySource like this: 我正在使用@PropertySource加载属性文件,如下所示:

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

    @Bean
    public static PropertySourcesPlaceholderConfigurer propertiesPlaceHolderConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();
    }
}

But unfortunately, that is not loading the properties file. 但不幸的是,这不是加载属性文件。 Throws FileNotFoundException . 抛出FileNotFoundException But if I change the path to: 但是,如果我改变路径:

@PropertySources(value = { @PropertySource("file:/home/rohit/.invoice/config.properties") })

it works properly. 它工作正常。 And that is the path which the earlier path resolves to. 这就是早期路径解决的路径。 I've logged it to verify. 我已经记录下来进行验证。 So it seems to me that SpEL is not getting evaluated in the @PropertySource annotation. 所以在我看来,SpEL没有在@PropertySource注释中得到评估。 Is it supposed to work that way? 它应该以这种方式工作吗?

If yes, then is there any other way to read the external properties file, which sits in /home/rohit ? 如果是,那么有没有其他方法来读取外部属性文件,它位于/home/rohit I don't want to give absolute path, for obvious reasons. 出于显而易见的原因,我不想给出绝对的道路。 And I would like to avoid extending PropertyPlaceHolderConfigurer class. 我想避免扩展PropertyPlaceHolderConfigurer类。

One other option I tried was adding the /home/rohit/.invoice folder to tomcat classpath. 我尝试的另一个选项是将/home/rohit/.invoice文件夹添加到tomcat类路径中。 But seems like Spring doesn't use System Classpath to resolve classpath: suffix. 但似乎Spring不使用System Classpath来解析classpath: suffix。 Any pointers on this? 有关于此的任何指示?

In a @PropertySoure annotation EL expressions won't work. @PropertySoure注释中EL表达式不起作用。 You are allowed to use placeholders ${...} however that is also limited to system or environment variables. 您可以使用占位符${...}但这也仅限于系统或环境变量。 However as you want to resolve the home directory of the user you can use the ${user.home} placeholder. 但是,如果要解析用户的主目录,可以使用${user.home}占位符。

@PropertySource("file:${user.home}/.invoice/config.properties")

This should work as desired. 这应该按照需要工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM