java / intermediate
Snippet
Unified Resource Access with ResourceLoader
ResourceLoader provides a unified way to load resources (files, URLs, classpath items) regardless of the underlying storage mechanism.
snippet.java
java
1
2
3
4
5
6
7
8
9
10
@Servicepublic class TemplateService {@Autowiredprivate ResourceLoader resourceLoader;public void loadTemplate() {Resource resource = resourceLoader.getResource("classpath:templates/mail.txt");// Access file content via resource.getInputStream()}}
spring
Breakdown
1
ResourceLoader
A strategy interface for loading resources such as class path or file system resources.
2
getResource("classpath:...")
Returns a Resource handle for the specified location string.