(十二)ResourceLoader接口

本文介绍了Spring框架中资源加载的不同方式,包括文件系统、类路径及网络资源的读取方法,并展示了具体的实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ResourceLoader接口的主要作用是进行org.springframework.core.io.ResourceLoader对象实例化,这个接口的定义如下:

          读取指定的资源信息:public getResource Resource getResource(String location);

           取得类加载器:public getClassLoader ClassLoader getClassLoader()

ResorceLoader是一个接口,于是要使用这个接口必须知道它对应的子类:org.springframework.core.io.DefaultResourceLoader,利用这个子类就可以实现ResourceLoader接口的实例化,但是现在整个资源操作的问题并不在于Resource或者是ResourceLoader接口,以及其一堆的子类,而关键性的问题在于这个定位的字符串,

        文件读取资源:"file:路径";

        CLASSPATH读取:"claspath:路径";

        网络读取:"http://操作路径";

范例:进行文件读取

import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import org.apache.log4j.helpers.Loader;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;

public class FileResourceLoader {
		public static void main(String[] args) throws IOException {
			ResourceLoader rlLoader=new DefaultResourceLoader();
			Resource source=rlLoader.getResource("file:E:"+File.separator+"练习.txt");
			
			System.out.println("数据长度"+source.contentLength());
			Scanner scanner=new Scanner(source.getInputStream());
			if(scanner.hasNext()){
				System.out.println(scanner.next());
			}
		}
}

        只是写了一个字符串,而后就可以进行读取了

范例:读取CLASSPATH的路径

import java.io.IOException;
import java.util.Scanner;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;

public class ClassPathResourceLoader {
		public static void main(String[] args) throws IOException {
			ResourceLoader rlLoader=new DefaultResourceLoader();
			Resource source=rlLoader.getResource("classpath:applicationContext.xml");
			
			System.out.println("数据长度"+source.contentLength());
			Scanner scanner=new Scanner(source.getInputStream());
			while(scanner.hasNext()){
				System.out.println(scanner.next());
			}
		}
}

范例:读取网络资源

import java.io.IOException;
import java.util.Scanner;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;

public class HttpResourceLoader {
		public static void main(String[] args) throws IOException {
			ResourceLoader rlLoader=new DefaultResourceLoader();
			Resource source=rlLoader.getResource("http://test.testzwb.cn/");
			
			System.out.println("数据长度"+source.contentLength());
			Scanner scanner=new Scanner(source.getInputStream());
			while(scanner.hasNext()){
				System.out.println(scanner.next());
			}
		}
}

        所有读取的操作过程之中,可以清楚感受到,都是利用了字符串来进行的资源定位,也就是在整个Spring里面核心的设计思想就是:就是利用合理的字符串格式来进行更加复杂的操作,



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值