1

My question is an exact duplicate of this question : Spring application context external properties?

This is how I injected the props file before :

<util:properties id="smrProps" location="classpath:/spring/SomeProps.properties" />  

But for the life of me, I cant figure out what to use in location when I want to inject a properties file which will be in the same directory as a runnable jar. I know where the classpath points and my props is just one level up, so I even tried classpath:/../SomeProps.properties assuming it will look in the parent folder, but no luck.

For ex if jar is in : C:\temp\some.jarand properties file is in C:\temp\SomeProps.properties

If some.jar is in temp, then SomeProps.properties will also be in temp. Of course, I cannot be using C:\temp\SomeProps.properties in location

Can someone please guide me on how I could use this props file ?

Community
  • 1
  • 1
happybuddha
  • 1,271
  • 2
  • 20
  • 40
  • If you are using Spring Boot you can check http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html – shobull Sep 29 '16 at 11:39

2 Answers2

0

I don't think it is possible to use a relative path to access something outside of the .jar with util:properties.

But, if you can, you should be able to do it by using a system property.

Also, you shoud use file: instead of classpath:. Something like this :

<util:properties id="smrProps" location="file:{my.path}/SomeProps.properties" />

And then execute the .jar with something like this :

java -Dmy.path=/YourFolderPath -jar application.jar

An other solution would be to extend the PropertyPlaceholderConfigurer and then get the path of the .jar with Java. Take a look at this question : Loading Properties with Spring (via System Properties)

Community
  • 1
  • 1
Jean-Philippe Bond
  • 10,089
  • 3
  • 34
  • 60
0

Another solution would be to figure out the path of running jar

return new File(MyClass.class.getProtectionDomain().getCodeSource().getLocation().getPath());

Use it find your prop file in parent folder and inject it into context using SPEL

Here is how it looks (you might want to tidy it a bit, but i was able to emulate your case and verified this solution):

<util:properties id="smrProps" location="'file:' + new java.io.File(T(com.yourpackage.YourClass).getProtectionDomain().getCodeSource().getLocation().getPath()).getParent() + '/'+ settings.prop'" />
diy
  • 3,590
  • 3
  • 19
  • 16