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

教程分享 > Java教程 > Spring (1813) 2024-08-07 11:05:25

了解将资源或文件(例如文本文件、XML 文件、属性文件或图像文件)加载到 Spring 应用程序上下文中的不同方法。Spring ResourceLoader为我们通过资源路径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.leftso.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("//leftso.com/readme.txt");

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

6.如何注入外部文件

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

<bean id="customResourceLoader" class="com.leftso.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 资源加载器示例中可以使用相同的代码。
 

https://www.leftso.com/article/1003.html

相关文章
了解将资源或文件(例如文本文件、XML 文件、属性文件或图像文件)加载到 Spring 应用程序上下文中的不同方法。Spring ResourceLoader为我们通过资源路径getResour...
Spring框架Spring IoC容器的核心原理,前三篇已经从历史的角度和大家一起探讨了为什么会有Spring,Spring的两个核心概念:IoC和AOP的雏形,Spring的历史变迁和如今的...
了解Spring bean 生命周期。我们将了解 bean生命周期阶段、初始化和销毁​​回调方法。我们将学习使用 XML 配置以及 Java 注释配置来自定义 bean 生命周期事件。1. 什么...
Spring AOP 实现原理基础讲解
引言    通过之前spring boot mybatis 整合的讲解: spring boot mybaties整合  (spring boot mybaties 整合 基于Java注解方式写...
FactoryBean 是用作在IoC 容器中创建其他 bean 的工厂bean 。从概念上讲,FactoryBean 与factory method非常相似,但它是 Spring 特定的 be...
spring boot 入门之spring session实现restful apis。通过spring boot或者spring mvc整合spring session的方式来实现sessio...
spring boot 入门之整合spring session实现session共享。一直以来Java编程中web项目中的session共享问题都是一个很难解决的问题。接下来将讲解通过sprin...
在 Spring 框架中,我们可以在 6 个内置的spring bean 作用域内创建 bean ,您也可以定义自定义 bean 作用域。在这六个作用域中,只有在您使用 Web 感知的Appli...
Spring Boot 2.0,Spring框架的Spring Boot 中的Spring Boot Actuator变化讲解。并且了解如何在Spring Boot 2.0中使用Actuator...
在 Spring 框架中,按类型自动装配 bean 允许自动装配属性 -如果容器中只有一个属性类型的 bean。如果有多个,则会抛出一个致命异常,这表明您可能不会byType对该 bean 使用...
在 Spring 框架中,通过构造函数自动装配 bean类似于byType,但适用于构造函数参数。在启用自动装配的 bean 中,它查找构造函数参数的类类型,然后按类型对所有构造函数参数执行自动...
spring boot 2.0 security 5.0 整合,实现自定义表单登录。spring boot 2.0框架使用。
Spring AOP来由,为何会出现Spring AOP这样的框架? 上一篇从Web开发演进过程的一个侧面简述了一下为什么会有Spring框架?事实上只介绍了为什么会有Spring IOC(控制...
spring boot 1.5整合redis实现spring的缓存框架,spring boot,redis