Starting from spring-boot 2.4.4 and including spring-boot-starter-web, injection on Path objects misinterprets absolute paths which are transformed into relative paths.
For exemple with this pom file :
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.4</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
And This Component :
@Component
public class PathInjectionIssue {
@Value("${myconfigfile}")
private Path configAsPath; // here is the issue
@Value("${myconfigfile}")
private File configAsFile; /// this one is OK
@Value("${myconfigfile}")
private String configFileAsString; // this one is OK this is just a string
@PostConstruct
public void check() {
if (configAsPath.getNameCount() > 1) {
throw new RuntimeException("This absolute file path [" + configAsPath.toAbsolutePath() + "] is not correct. Expected : " + configFileAsString);
}
}
}
This application.properties file :
myconfigfile=/conf.json
The output is :
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'pathInjectionIssue': Invocation of init method failed; nested exception is java.lang.RuntimeException: This absolute file path [C:\Users\shelbert\AppData\Local\Temp\tomcat-docbase.8080.9485259209731591160\conf.json] is not correct. Expected : /conf.json
[...]
There is no issue with spring-boot 2.4.3 nor without the spring-boot-starter-web dependency
Comment From: radarsh
Came to say that I am experiencing the same. In my case, I have a Path
property defined inside a @ConfigurationProperties
bean and we set it using command line using the below syntax:
--<property-name>=/var/folders/t9/ddcqvwls0vb0jz04zjk_ws_80000gq/T/14198791533976608309
But by the time the application reads it, it would have already converted it by prefixing with tomcat-docbase directory to be
/private/var/folders/t9/ddcqvwls0vb0jz04zjk_ws_80000gq/T/tomcat-docbase.8080.14679614108707425001/var/folders/t9/ddcqvwls0vb0jz04zjk_ws_80000gq/T/14198791533976608309
Comment From: wilkinsona
Thanks for the report. This is a duplicate of #25881 and spring-projects/spring-framework#26702.