Spring入门——(八、Resource和ResourceLoader)

Resources
>针对资源文件的统一接口
>Resources
- UrlResource:URL对应的资源,根据一个URL地址即可构建
- ClassPathResource:获取类路径下的资源文件
- FileSystemResource:获取文件系统里面的资源
- ServletContextResource:ServletContext封装的资源,用于访问ServletContext环境下的资源
- InputStreamResource:针对于输入流封装的资源
- ByteArrayResource:针对于字节数组封装的资源
ResourceLoader


示例:

MoocResource类:

package com.imooc.resource;

import java.io.IOException;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.io.Resource;


public class MoocResource implements ApplicationContextAware  {
	
	private ApplicationContext applicationContext;
	
	@Override
	public void setApplicationContext(ApplicationContext applicationContext)
			throws BeansException {
		this.applicationContext = applicationContext;
	}
	
	public void resource() throws IOException {
		Resource resource = applicationContext.getResource("config.txt");
		System.out.println(resource.getFilename());
		System.out.println(resource.contentLength());
	}

}

spring-resource.xml配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd" >
        
        <bean  id="moocResource" class="com.imooc.resource.MoocResource" ></bean>
	
 </beans>

测试类:

package com.imooc.test.resource;

import java.io.IOException;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;

import com.imooc.resource.MoocResource;
import com.imooc.test.base.UnitTestBase;

@RunWith(BlockJUnit4ClassRunner.class)
public class TestResource extends UnitTestBase {
	
	public TestResource() {
		super("classpath:spring-resource.xml");
	}
	
	@Test
	public void testResource() {
		MoocResource resource = super.getBean("moocResource");
		try {
			resource.resource();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
}

输出结果:


Spring中,ResourceLoader是一个接口,提供了一种简单的方式来加载资源文件,如XML配置文件、Properties文件、图片等。它提供了一种统一的方式来访问资源,不需要关心资源文件存储在哪里,比如在文件系统、在类路径下、在Web应用程序上下文中等。 以下是一个简单的ResourceLoader用法示例: ```java import org.springframework.core.io.Resource; import org.springframework.core.io.ResourceLoader; public class MyResourceLoader { private ResourceLoader resourceLoader; public MyResourceLoader(ResourceLoader resourceLoader) { this.resourceLoader = resourceLoader; } public void loadResource() throws IOException { // 加载一个资源文件 Resource resource = resourceLoader.getResource("classpath:my-resource.xml"); // 获取资源文件的输入流 InputStream is = resource.getInputStream(); // 处理资源文件 // ... } } ``` 在上面的示例中,我们首先定义了一个MyResourceLoader类,它依赖于ResourceLoader接口。然后我们使用ResourceLoader接口的`getResource()`方法加载一个名为“my-resource.xml”的资源文件,并获取它的输入流。最后,我们可以使用输入流来处理资源文件的内容。 Spring提供了多种ResourceLoader实现,如ClassPathResourceLoader、FileSystemResourceLoader、ServletContextResourceLoader等。我们可以根据不同的情况选择不同的ResourceLoader实现。例如,在Web应用程序中,我们可以使用ServletContextResourceLoader来加载Web应用程序上下文中的资源文件。 ```java import org.springframework.core.io.Resource; import org.springframework.web.context.support.ServletContextResourceLoader; public class MyResourceLoader { private ServletContextResourceLoader resourceLoader; public MyResourceLoader(ServletContextResourceLoader resourceLoader) { this.resourceLoader = resourceLoader; } public void loadResource() throws IOException { // 加载一个资源文件 Resource resource = resourceLoader.getResource("/WEB-INF/my-resource.xml"); // 获取资源文件的输入流 InputStream is = resource.getInputStream(); // 处理资源文件 // ... } } ``` 在上面的示例中,我们使用了ServletContextResourceLoader来加载一个Web应用程序上下文中的资源文件。在调用getResource()方法时,我们可以使用相对于Web应用程序根目录的路径来指定资源文件的位置。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值