I have checked your code.If you would like to load a file from classpath in a Spring Boot JAR, then you have to use the resource.getInputStream() rather than resource.getFile().If you try to use resource.getFile() you will receive an error, because Spring tries to access a file system path, but it can not access a path in your JAR.

detail as below:

https://smarterco.de/java-load-file-classpath-spring-boot/

Answer from Seamas on Stack Overflow
🌐
Simplesolution
simplesolution.dev › java-spring-boot-resourceloader
Spring Boot Read File from resources using ResourceLoader
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.4.3) 2021-03-03 01:11:53.341 INFO 4700 --- [ main] d.s.SpringBootResourceloaderApplication : Starting SpringBootResourceloaderApplication using Java 1.8.0_231 on SS with PID 4700 (D:\SimpleSolution\spring-boot-resourceloader\bin\main started by SS in D:\SimpleSolution\spring-boot-resourceloader) 2021-03-03 01:11:53.342 INFO 4700 --- [ main] d.s.SpringBootResou
🌐
Spring
docs.spring.io › spring-framework › docs › current › javadoc-api › org › springframework › core › io › ResourceLoader.html
ResourceLoader (Spring Framework 7.0.7 API)
Expose the ClassLoader used by this ResourceLoader. ... Return a Resource handle for the specified resource location. ... Pseudo URL prefix for loading from the class path: "classpath:". ... Return a Resource handle for the specified resource location. The handle should always be a reusable resource descriptor, allowing for multiple InputStreamSource.getInputStream() calls. Must support fully qualified URLs, for example, "file:C:/test.dat".
🌐
GeeksforGeeks
geeksforgeeks.org › java › spring-resourceloaderaware-with-example
Spring - ResourceLoaderAware with Example - GeeksforGeeks
July 23, 2025 - Since spring bean does not have access to the spring application context, So how can a bean load/access resources or files (e.g. text files, XML files, properties files, etc.)? To solve this issue, the workaround is to implement the ResourceLoaderAware interface and create a setter method for the ResourceLoader object.
🌐
Medium
medium.com › codex › accessing-resource-files-in-spring-boot-a-comprehensive-guide-eab958eb8f9c
Accessing Resource Files in Spring Boot: A Comprehensive Guide | by Igor Venturelli | CodeX | Medium
November 28, 2024 - In this example, the ResourceLoader loads the resource from the classpath, and we use a BufferedReader to read its contents. The key point here is the use of the classpath: prefix to indicate that the file is located in the classpath.
🌐
Mkyong
mkyong.com › home › spring › spring resource loader with getresource() example
Spring Resource loader with getResource() example - Mkyong.com
June 23, 2011 - ResourceLoader rl = new DefaultResourceLoader(); Resource r1 = rl.getResource(“classpath:someClasspathResource”); Resource r2 = rl.getResource(“file:/some/file/resource”); No need for the code to run in an ApplicationContext as Spring Bean!
🌐
HowToDoInJava
howtodoinjava.com › home › spring core › spring: reading an internal or external resource
Spring: Reading an Internal or External Resource
December 4, 2023 - Spring provides the following 6 implementations for the Resource interface. ... We can specify different prefixes for creating paths to load resources from different locations. The ResourceLoader is used for loading resources from classpath or file system resources.
🌐
Spring Framework Guru
springframework.guru › home › working with resources in spring
Working with Resources in Spring - Spring Framework Guru
May 29, 2019 - In this post, I’ll explain how to work with resources in Spring using ResourceLoader. We’ll begin with a brief introduction about resources. Next, we’ll look at the Resource interface and some of its important methods. Finally, we’ll go through its implementations. Frequently, we need to read external resources into our Spring application. Examples of external resources are text files, XML files, properties files, and image files.
🌐
Baeldung
baeldung.com › home › spring › access a file from the classpath in a spring application
Access a File from the Classpath using Spring | Baeldung
March 26, 2025 - Note too that ResourceLoader is implemented by all concrete ApplicationContexts, which means that we can also simply depend on ApplicationContext if that suits our situation better: ApplicationContext context; public Resource loadEmployeesWithApplicationContext() { return context.getResource("classpath:data/employees.dat"); } As a caveat, there is another way to retrieve resources in Spring, but the ResourceUtils Javadoc is clear that the class is mainly for internal use.
Find elsewhere
🌐
LogicBig
logicbig.com › tutorials › spring-framework › spring-core › injecting-resource-loader.html
Spring - Autowiring ResourceLoader
package com.logicbig.example; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import java.io.IOException; @Configuration @ComponentScan public class InjectResourceLoaderExample { public static void main(String[] args) throws IOException { new AnnotationConfigApplicationContext(InjectResourceLoaderExample.class); } }
🌐
Medium
medium.com › @anders.swanson.93 › implement-a-custom-spring-resource-loader-7de75262ef61
Implement a custom Spring Resource Loader | by Anders Swanson | Medium
January 31, 2025 - package com.example; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.ResourceLoaderAware; import org.springframework.core.io.DefaultResourceLoader; import org.springframework.core.io.ProtocolResolver; import org.springframework.core.io.Resource; import org.springframework.core.io.ResourceLoader; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Component; @Component public class DatabaseResourceResolver implements ResourceLoaderAware, ProtocolResolver { private final JdbcTemplate jdbcTemplate; private final St
🌐
Spring
docs.spring.io › spring-boot › api › java › org › springframework › boot › io › ApplicationResourceLoader.html
ApplicationResourceLoader (Spring Boot 3.5.7 API)
Return a ResourceLoader supporting additional ProtocolResolvers registered in spring.factories. The factories file will be resolved using the default class loader at the time this call is made.
🌐
ZetCode
zetcode.com › springboot › loadresource
Spring Boot loading resources
We used @Value and ResourceLoader to load a resource file. My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books.
🌐
Jaylinh-com
jaylinh-com.github.io › spring-framework-chinese › en › core › resources › the-resourceloader.html
2.4. The ResourceLoader | Spring Framework Documentation
All application contexts implement the ResourceLoader interface. Therefore, all application contexts may be used to obtain Resource instances. When you call getResource() on a specific application context, and the location path specified doesn’t have a specific prefix, you get back a Resource type that is appropriate to that particular application context. For example, assume the following snippet of code was executed against a ClassPathXmlApplicationContext instance:
🌐
Baeldung
baeldung.com › home › spring › load a resource as a string in spring
Load a Resource as a String in Spring | Baeldung
February 26, 2026 - We can also inject the ResourceLoader into our bean with @Autowired: ... Once we have access to the Resource we need to be able to read it into a String. Let’s create a ResourceReader utility class with a static method asString to do this for us. ... Our next step is to take this InputStream and convert it to a String. We can use Spring’s own FileCopyUtils#copyToString method:
🌐
DZone
dzone.com › coding › frameworks › working with resources in spring
Working With Resources in Spring
August 29, 2019 - ResourceLoader is a vital component ... into our Spring application. Examples of external resources are text files, XML files, properties files, and image files....
🌐
Blogger
javainspires.blogspot.com › home › org.springframework.core › resourceloader in spring
ResourceLoader in Spring
February 8, 2024 - Here are some common use cases: 1. Loading Configuration Files: Spring applications often require configuration files (e.g., properties files, XML files) for setting up application parameters.
🌐
Blogger
javadiscover.blogspot.com › 2015 › 09 › spring-resource-loader-with-example.html
Spring Resource Loader with example - Java Discover
package com.app.javadiscover; import org.springframework.context.ResourceLoaderAware; import org.springframework.core.io.Resource; import org.springframework.core.io.ResourceLoader; public class ResourceBean implements ResourceLoaderAware { private ResourceLoader resourceLoader; public void setResourceLoader(ResourceLoader resourceLoader) { this.resourceLoader = resourceLoader; } public Resource getResource(String location){ return resourceLoader.getResource(location); } } LoadResource.java
🌐
danvega.dev
danvega.dev › blog › 2025 › 12 › 17 › loading-spring-resources
Loading Resources in Spring Boot
The mental model is straightforward: if you're hardcoding the path in your source code, use @Value. If the path depends on something that happens while your application is running (user input, configuration values, database lookups), use ResourceLoader.
🌐
Spring
docs.spring.io › spring-framework › reference › core › resources.html
Resources :: Spring Framework
All application contexts implement the ResourceLoader interface. Therefore, all application contexts may be used to obtain Resource instances. When you call getResource() on a specific application context, and the location path specified doesn’t have a specific prefix, you get back a Resource type that is appropriate to that particular application context. For example, assume the following snippet of code was run against a ClassPathXmlApplicationContext instance: