Spring include relative path in properties

Viewed 3455

I'm trying to make it so that in config/app.properties, I can have:

myfile.location=./myfile

where . is relative to said properties file. Is this possible ? I tried:

resourceLoader.getResource(appConfig.getMyFileLocation());

where resourceLoader and appConfig are autowired, but it won't work.

2 Answers

Java properties are key-value pairs. So when you specify myfile.location=./myfile, this means appConfig.getMyFileLocation() will return './myfile' which is not the correct location.

As a workaround, you can get the location of the properties file and then use it along with the relative location to find the absolute path.

File propertyFileDirectory = .... // get the property file directory
String myfilePath = appConfig.getMyFileLocation();
File file = new File(propertyFileDirectory, myfilePath);

I usually reference my files like this:

myfile.location=classpath:myfile

where myfile is at the same location as the properties file.

Related