了解将外部资源或文件(例如文本文件,XML文件,属性文件或图像文件)加载到Spring应用程序上下文中的不同方法。Spring ResourceLoadergetResource()
为您提供了一种统一的方法,可以通过资源路径检索外部资源。
1. Spring Resource接口代表一种资源
Resource是Spring中用于表示外部资源的通用接口。Spring为Resource
接口提供了几种实现。资源加载器的getResource()
方法将Resource
根据资源路径决定实例化哪个实现。
如何获得资源
Resource banner = resourceLoader.getResource( "file:c:/temp/filesystemdata.txt" ); |
您可以指定不同的前缀来创建从不同位置加载资源的路径。
1)要从文件系统加载资源,请使用file
前缀。
2)要从类路径加载资源,请使用classpath
前缀。
3)您还可以将URL指定为资源路径。
因为您是通过spring的资源加载器访问资源,所以您的自定义资源加载器必须实现ApplicationContextAware
接口或ResourceLoaderAware
接口。
2.如何使用Spring ResourceLoader获取资源
为了演示下面的各种示例,我在不同的位置放置了一个具有匹配名称的文件,我将展示加载它们中的每一个。
CustomResourceLoader.java
编写如下,将加载的资源文件的内容打印到控制台。
CustomResourceLoader.java
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import org.springframework.context.ResourceLoaderAware; import org.springframework.core.io.Resource; import org.springframework.core.io.ResourceLoader; public class CustomResourceLoader implements ResourceLoaderAware { private ResourceLoader resourceLoader; public void setResourceLoader(ResourceLoader resourceLoader) { this .resourceLoader = resourceLoader; } public void showResourceData() throws IOException { //This line will be changed for all versions of other examples Resource banner = resourceLoader.getResource( "file:c:/temp/filesystemdata.txt" ); InputStream in = banner.getInputStream(); BufferedReader reader = new BufferedReader( new InputStreamReader(in)); while ( true ) { String line = reader.readLine(); if (line == null ) break ; System.out.println(line); } reader.close(); } } |
而applicationContext.xml
此文件的文件条目如下:
applicationContext.xml中
< bean id = "customResourceLoader" class = "com.howtodoinjava.demo.CustomResourceLoader" ></ bean > |
要测试CustomResourceLoader
bean并调用该showResourceData()
方法,下面的代码已被使用:
Main.java
@SuppressWarnings ( "resource" ) public static void main(String[] args) throws Exception { ApplicationContext context = new ClassPathXmlApplicationContext( "applicationContext.xml" ); CustomResourceLoader customResourceLoader = (CustomResourceLoader) context.getBean( "customResourceLoader" ); customResourceLoader.showResourceData(); } |

3.如何加载外部资源
3.1。从应用程序根文件夹加载资源
要从应用程序文件夹加载文件,请使用以下模板:
Resource banner = resourceLoader.getResource( "file:data.txt" ); |
3.2。从类路径加载资源
要从类路径加载文件,请使用以下模板:
Resource banner = resourceLoader.getResource( "classpath:classpathdata.txt" ); |
3.3。从文件系统加载资源
要从应用程序文件夹外的文件系统加载文件,请使用以下模板:
Resource banner = resourceLoader.getResource( "file:c:/temp/filesystemdata.txt" ); |
3.4。从URL加载资源
要从任何URL加载文件,请使用以下模板:
Resource banner = resourceLoader.getResource( "//howtodoinjava.com/readme.txt" ); |
以上所有示例都将从其位置加载资源文件,您可以按照自己的方式使用它们。
4.如何注入外部资源名称
在上面的示例中,我们对CustomResourceLoader
许多人可能不喜欢的资源名称进行了硬编码,并希望通过上下文文件对其进行配置。使用下面的代码模板可以配置外部资源名称。
beans.xml中
< bean id = "customResourceLoader" class = "com.howtodoinjava.demo.CustomResourceLoader" > < property name = "resource" > < value >classpath:classpathdata.txt</ value > <!-- or --> < value >file:data.txt</ value > </ property > </ bean > |
而且CustomResourceLoader
会象下面这样:
CustomResourceLoader.java
public class CustomResourceLoader { private Resource resource; public Resource getResource() { return resource; } public void setResource(Resource resource) { this .resource = resource; } } |
在上下文初始化时,资源将被注入' resource
'的属性CustomResourceLoader
。可以在spring boot resourceloader示例中使用相同的代码