@Value(classpath)
When using@Value("classpath:...")
- External Resources: If resources can be found on the external file system,
@Valueloads them as file paths.- Internal JAR Resources: Resources embedded within a JAR cannot be directly accessed as file paths and must be read through
getInputStream(). Spring automatically provides these resources via aResourceobject in this case. Resources can be read using anInputStream.
When deploying as a WAR
- Tomcat adds
WEB-INF/classes/(a path on the actual file system) of the directory where the WAR is extracted to the classpath. - Resources can be accessed directly.
1@Value("classpath:menu.xml")
2private Resource menuFile;
3
4...
5
6menuFile.getFile()
When deploying as a JAR
getFile()is expected to return aFileobject, but files inside a JAR are compressed and cannot be directly converted to aFile.- The resource is read directly from within the compressed JAR using
getInputStream().
1@Value("classpath:menu.xml")
2private Resource menuFile;
3
4...
5
6try (InputStream inputStream = menuFile.getInputStream())...