This is a memo when I reviewed how to access resource files using the Resource interface and implementation class of Spring Framework.
environment
reference
Since 28.12.2003
Interface for a resource descriptor that abstracts from the actual type of underlying resource, such as a file or class path resource.
How to get an instance of Resource interface
ResourceLoader
@Value
annotationAnd so on.
How to use the appropriate Resource interface implementation class depending on the location of the resource file.
For example, there are ClassPathResource
to access resources in the classpath, ʻUrlResource to access resources on the network, and
FileUrlResource` to access resources on the file system.
Since 28.12.2003
Resource implementation for class path resources. Uses either a given ClassLoader or a given Class for loading resources.
constructor |
---|
ClassPathResource(String path) |
ClassPathResource(String path, Class<?> clazz) |
ClassPathResource(String path, ClassLoader classLoader) |
code
Resource resource = new ClassPathResource("config/app.properties");
Since the Resource interface inherits the ʻInputStreamSource interface, you can get the ʻInputStream
of the resource file with the getInputStream method.
String read(Resource resource) {
try (InputStream in = resource.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8))) {
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
return sb.toString();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
Since the Resource interface also has a getFile method, it can be described as follows, but since an exception is thrown when the resource is in the classpath or on the network, the resource file is usually on the file system. I think you should use it only in certain cases.
String read(Resource resource) {
try {
return Files.readString(resource.getFile().toPath());
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
Since 28.12.2003
Resource implementation for java.net.URL locators. Supports resolution as a URL and also as a File in case of the "file:" protocol.
constructor |
---|
UrlResource(String path) |
UrlResource(String protocol, String location) |
UrlResource(String protocol, String location, String fragment) |
UrlResource(java.net.URI uri) |
UrlResource(java.net.URL url) |
code
Resource urlResource = new UrlResource("http://localhost:8887/config/app.properties");
Since 5.0.2
Subclass of UrlResource which assumes file resolution, to the degree of implementing the WritableResource interface for it. This resource variant also caches resolved File handles from getFile().
constructor |
---|
FileUrlResource(String location) |
FileUrlResource(java.net.URL url) |
code
Resource fileUrlResource = new FileUrlResource("c://var//config/app.properties");
Since 10.03.2004
Strategy interface for loading resources (e.. class path or file system resources). An ApplicationContext is required to provide this functionality, plus extended ResourcePatternResolver support. DefaultResourceLoader is a standalone implementation that is usable outside an ApplicationContext, also used by ResourceEditor.
ResourceLoader Instance of Resource using implementation class of interface If you get, there is an advantage that you do not need to be aware of the implementation class of the Resource interface.
In Spring Boot, ResourceLoader is injected with an instance of the ʻAnnotationConfigServletWebServerApplicationContext` class. (This class is in the Spring Boot project)
@Autowired
private ResourceLoader resourceLoader;
// org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext
classpath
Writing starting with classpath:
will inject an instance of the ClassPathResource
class.
Resource resource = resourceLoader.getResource("classpath:config/app.properties");
// org.springframework.core.io.ClassPathResource
url
When written in the http scheme, an instance of the ʻUrlResource` class is injected.
Resource resource = resourceLoader.getResource("http://localhost:8887/config/app.properties");
// org.springframework.core.io.UrlResource
file
An instance of the FileUrlResource
class will be injected as described in the file scheme.
Resource resource = resourceLoader.getResource("file:///c://config/app.properties");
// org.springframework.core.io.FileUrlResource
@Value
annotationDepending on the value of the Value annotation, the Spring Framework will inject an instance of the appropriate implementation class. The format of the value described in the annotation is the same as ResourceLoader.
classpath
@Value("classpath:config/app.properties")
private Resource classpathResource;
// org.springframework.core.io.ClassPathResource
url
@Value("http://localhost:8887/config/app.properties")
private Resource urlResource;
// org.springframework.core.io.UrlResource
file
@Value("file:///c://config/app.properties")
private Resource fileUrlResource;
// org.springframework.core.io.FileUrlResource
Since 1.1.5
Utility methods for resolving resource locations to files in the file system. Mainly for internal use within the framework.
As described in the API documentation, it is a utility class mainly for use inside the Spring Framework. There is no statement that it should not be used on the application side, but it should be used with caution as it is intended for use within the framework.
classpath
File file = ResourceUtils.getFile("classpath:config/app.properties");
url
URL url = ResourceUtils.getURL("http://localhost:8887/config/app.properties");
file
File file = ResourceUtils.getFile("file:///c://var//config/app.properties");
Since 3.2.2
Simple utility methods for dealing with streams. The copy methods of this class are similar to those defined in FileCopyUtils except that all affected streams are left open when done. All copy methods use a block size of 4096 bytes. Mainly for use within the framework, but also useful for application code.
The Spring Framework also has utility classes that handle streams. You can use the copyToString method of this class to simplify your code as follows:
String data = StreamUtils.copyToString(resource.getInputStream(), StandardCharsets.UTF_8);
Recommended Posts