spring常用的4种加载资源的前缀

  1. ‘classpath:’ 类路径中加载资源,可以加载所有classpath目录所包含的资源

		Resource fileClasspath = applicationContext.getResource("classpath:test.txt");
		this.outResource(fileClasspath);
  1. ‘file:’ 文件系统中加载资源,可以加载所有文件系统中有权限访问的资源
		Resource fileResource = applicationContext.getResource("file:C:/tmp/test.txt");
		this.outResource(fileResource);
  1. ‘http:’/‘ftp:’ http/ftp加载资源,可以加载互联网上的资源
		Resource fileUrl = applicationContext.getResource("http://git.oschina.net/notifications/count");
		this.outResource(fileUrl);
  1. 不加前缀,此时实际加载分为两种情况
    (1),在web项目中,可以加载应用上下文的所有资源

(2),非web项目中,等同于‘classpath:’,但是类加载器和‘classpath:’不完全相同

		Resource fileProject = applicationContext.getResource("test.txt");
		this.outResource(fileProject);

完整示例

 
	ApplicationContext applicationContext;
	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		this.applicationContext=applicationContext;
		this.outResource("file:C:/tmp/test.txt");
		this.outResource("classpath:classpath.txt");
		this.outResource("webapp.txt");
		this.outResource("http://127.0.0.1:80/test.txt");
	}
 
	private void outResource(String resStr) {
		Resource resource = applicationContext.getResource(resStr);
		InputStream stream = null;
		try {
			System.err.println("");
			System.err.println("加载的资源:"+resStr);
			System.err.println("包装资源的类:" + resource.getClass().getName());
			System.err.println("包装资源的访问地址:" + resource.getURI());
			stream = resource.getInputStream();
			System.err.println("读取到的内容:" + StreamUtils.copyToString(stream, Charset.defaultCharset()));
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (stream != null)
				try {
					stream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
		}
	}

非web项目下输出

加载的资源:file:C:/tmp/test.txt
包装资源的类:org.springframework.core.io.UrlResource
包装资源的访问地址:file:C:/tmp/test.txt
读取到的内容:加载FileSystem文件
 
加载的资源:classpath:classpath.txt
包装资源的类:org.springframework.core.io.ClassPathResource
包装资源的访问地址:file:/C:/git-repo/tm/target/classes/classpath.txt
读取到的内容:加载classpath资源
 
加载的资源:webapp.txt
包装资源的类:org.springframework.core.io.DefaultResourceLoader$ClassPathContextResource
包装资源的访问地址:file:/C:/git-repo/tm/target/classes/webapp.txt
读取到的内容:加载ServletContext资源
 
加载的资源:http://127.0.0.1:80/test.txt
包装资源的类:org.springframework.core.io.UrlResource
包装资源的访问地址:http://127.0.0.1:80/test.txt
读取到的内容:加载nginx服务中的资源
 

web项目下输出

加载的资源:file:C:/tmp/test.txt
包装资源的类:org.springframework.core.io.UrlResource
包装资源的访问地址:file:C:/tmp/test.txt
读取到的内容:加载FileSystem文件
 
加载的资源:classpath:classpath.txt
包装资源的类:org.springframework.core.io.ClassPathResource
包装资源的访问地址:file:/C:/git-repo/tm/target/classes/classpath.txt
读取到的内容:加载classpath资源
 
加载的资源:webapp.txt
包装资源的类:org.springframework.web.context.support.ServletContextResource
包装资源的访问地址:file:/C:/git-repo/tm/src/main/webapp/webapp.txt
读取到的内容:加载ServletContext资源
 
加载的资源:http://127.0.0.1:80/test.txt
包装资源的类:org.springframework.core.io.UrlResource
包装资源的访问地址:http://127.0.0.1:80/test.txt
读取到的内容:加载nginx服务中的资源
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

凌兮~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值