Spring5核心原理-ResourceLoaderAware从Spring中读取文件

Spring5核心原理-ResourceLoaderAware从Spring中读取文件

getResource()获取外部资源提供了统一的方法。

1.资源接口代表一个资源

Resource是 Spring 中用于表示外部资源的通用接口。

Spring 为该
Resource接口提供了以下 6 种实现。

  1. 网址资源
  2. 类路径资源
  3. 文件系统资源
  4. ServletContextResource
  5. 输入流资源
  6. 字节数组资源

我们可以指定不同的前缀来创建从不同位置加载资源的路径。

字首 例子 解释
classpath: classpath:com/myapp/config.xml 从类路径加载。
file: file:///data/config.xml URL从文件系统加载为 a 。
http: https://myserver/logo.png 加载为URL.
(没有) /data/config.xml 取决于底层ApplicationContext

2.资源加载器

它用于加载资源(例如类路径或文件系统资源)。它有两种方法:

//Expose the ClassLoader used by this ResourceLoader.
ClassLoader getClassLoader()
 
//Return a Resource handle for the specified resource location.
Resource getResource(String location)


getResource()方法将
Resource根据资源路径决定要实例化哪个实现。

要获取ResourceLoader的引用,请实现ResourceLoaderAware接口。

Resource banner = resourceLoader.getResource("file:c:/temp/filesystemdata.txt");

3. 使用ApplicationContext加载资源

在 Spring 中,所有应用程序上下文都实现了ResourceLoader接口。因此,所有应用程序上下文都可以用于获取资源实例。

要获取ApplicationContext的引用,请实现ApplicationContextAware接口。

Resource banner = ctx.getResource("file:c:/temp/filesystemdata.txt");

4. 使用 ResourceLoaderAware 加载资源

为了演示下面的各种示例,我在不同位置放置了一个名称匹配的文件,我将展示如何加载它们中的每一个。


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该文件的文件条目如下:

<bean id="customResourceLoader" class="com.note234.demo.CustomResourceLoader"></bean>

为了测试
CustomResourceLoaderbean 并调用该
showResourceData()方法,使用了以下代码:

@SuppressWarnings("resource")
public static void main(String[] args) throws Exception 
{
  ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
 
  CustomResourceLoader customResourceLoader = (CustomResourceLoader) context.getBean("customResourceLoader");
 
  customResourceLoader.showResourceData();
}



 

因为我们是通过spring的资源加载器来访问资源的,所以自定义的资源加载器要么实现ApplicationContextAware接口要么实现ResourceLoaderAware接口。

5.加载外部资源

5.1。从应用程序根文件夹加载资源

要从应用程序文件夹加载文件,请使用以下模板:

Resource banner = resourceLoader.getResource("file:data.txt");

5.2. 从类路径加载资源

要从类路径加载文件,请使用以下模板:

Resource banner = resourceLoader.getResource("classpath:classpathdata.txt");

5.3. 从文件系统加载资源

要从应用程序文件夹外的文件系统加载文件,请使用以下模板:

Resource banner = resourceLoader.getResource("file:c:/temp/filesystemdata.txt");

5.4. 从 URL 加载资源

要从任何 URL 加载文件,请使用以下模板:

Resource banner = resourceLoader.getResource("//note234.com/readme.txt");

以上所有示例都将从它们的位置加载资源文件,您可以按照自己的方式使用它们。

6.如何注入外部文件

在上面的例子中,我们硬编码了
CustomResourceLoader很多人可能不喜欢的资源名称,并希望通过上下文文件使其可配置。使用下面的代码模板使外部资源名称可配置。

<bean id="customResourceLoader" class="com.note234.demo.CustomResourceLoader">
 
  <property name="resource">
    <value>classpath:classpathdata.txt</value>
    <!-- or -->
    <value>file:data.txt</value> 
  </property>
 
</bean>

CustomResourceLoader如下所示:

public class CustomResourceLoader {
 
  private Resource resource;
 
  public Resource getResource() {
    return resource;
  }
 
  public void setResource(Resource resource) {
    this.resource = resource;
  }
}

在上下文初始化时,资源将被注入到 ‘ 
resource‘ 的属性中
CustomResourceLoader。在spring boot 资源加载器示例中可以使用相同的代码。

 

Spring5核心原理-ResourceLoaderAware从Spring中读取文件

相关文章:

你感兴趣的文章:

标签云: