Spring External Property Properties File PropertyPlaceHolder Java Config

So, it must be some kind of stupid mistake that I could not get through. I am trying to externalize my properties file, which is now placed in my custom home. I upload a properties file using @PropertySource as follows:

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

But, unfortunately, this is not loading a properties file. Throws FileNotFoundException . But if I change the path to:

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

It works correctly. And this is the path to which the previous path is resolved. I registered it for verification. Therefore, it seems to me that SpEL is not evaluated in the @PropertySource annotation. Should it work this way?

If so, is there another way to read the external properties file, which is located in /home/rohit ? I do not want to give an absolute path for obvious reasons. And I would like to avoid extending the PropertyPlaceHolderConfigurer class.

Another option I tried was to add the /home/rohit/.invoice folder to the classpath for tomcat. But it looks like Spring is not using the System Classpath to resolve the classpath: suffix. Any pointers to this?

+5
source share
1 answer

In @PropertySoure expressions, @PropertySoure annotations will not work. You can use ${...} placeholders, but this is also limited by system or environment variables. However, since you want to allow the user's home directory, you can use ${user.home} placeholder.

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

This should work as desired.

+3
source

Source: https://habr.com/ru/post/1206349/


All Articles