Spring3之 Resource

本文详细介绍了Spring框架中Resource接口的三种实现:URLResource、FileSystemResource和ClassPathResource。分别演示了如何使用这些实现来访问不同类型的资源,如文件系统路径、HTTP资源和类路径资源。

主要是org.springframework.core.io.Resource接口

URLResource

@Test
	/**
	 * UrlResource 封装了java.net.URL,它能够被用来访问任何通过URL可以获得的对象,例如:文件、HTTP对象、FTP对象等。
	 * 所有的URL都有个标准的 String表示,这些标准前缀可以标识不同的URL类型,包括file:访问文件系统路径,
	 * http: 通过HTTP协议访问的资源,ftp: 通过FTP访问的资源等等。 
	 * UrlResource 对象可以在Java代码中显式地使用 UrlResource 构造函数来创建
	 *
	 */
	public void testUrlResource() throws Exception{
		Resource resource = new UrlResource("file:/D:/workspace/MyEclipseSSH/Spring305/src/bean.xml");
		//Resource resource = new UrlResource("http://127.0.0.1:8080/index.html");
		//Resource resource = new UrlResource(new URI("http://127.0.0.1:8080/index.html"));
		//System.out.println(resource.getFile().getAbsolutePath());//URL时就不要拿他的路径了,这是拿不到的
		System.out.println(resource.exists());
		System.out.println(resource.isOpen());
		System.out.println(resource.getURL());
		System.out.println(resource.getURI());
		System.out.println(resource.isReadable());
		System.out.println(resource.getFilename());
		System.out.println(resource.getDescription());
		System.out.println(new Date(resource.lastModified()));
	}

 FileSystemResource

	@Test
	/**
	 * 这是为处理 java.io.File 而准备的Resource实现。它既可以作为File提供,也可以作为URL
	 * @throws Exception 
	 */
	public void testFileSystemResource() throws Exception{
		//file:/D:/workspace/MyEclipseSSH/Spring305/ 到本工程的目录中
		Resource resource = new FileSystemResource("src/bean.xml");
		
		System.out.println(resource.exists());
		System.out.println(resource.isOpen());
		System.out.println(resource.getURL());
		System.out.println(resource.getURI());
		System.out.println(resource.isReadable());
		System.out.println(resource.getFilename());
		System.out.println(resource.getDescription());
		System.out.println(new Date(resource.lastModified()));
	}

 ClassPathResource

@Test
	/**
	 * 这个类标识从classpath获得的资源。它会使用线程context的类加载器(class loader)、给定的类加载器或者用来载入资源的给定类。
	 * 如果类路径上的资源存在于文件系统里,这个 Resource 的实现会提供类似于java.io.File的功能。而如果资源是存在于还未解开
	 * (被servlet引擎或其它的环境解开)的jar包中,这些 Resource 实现会提供类似于java.net.URL 的功能。 
	 */
	public void testClassPath() throws IOException{
		
		//Resource resource = new ClassPathResource("/com/spring305/test/resource/ResourceBean.xml");
		Resource resource = new ClassPathResource("com/spring305/test/resource/ResourceBean.xml");
		System.out.println(resource.getFile().getAbsolutePath());
		System.out.println(resource.exists());
		System.out.println(resource.isOpen());
		System.out.println(resource.getURL());
		System.out.println(resource.getURI());
		System.out.println(resource.isReadable());
		System.out.println(resource.getFilename());
		System.out.println(resource.getDescription());
		System.out.println(new Date(resource.lastModified()));
		File file = resource.getFile();
		if(file.isFile()){
			//以字节为单位读取文件的内容,常用于二进制文件,如声音,图象,影象等文件
			InputStream inputStream = new FileInputStream(file);
			int temp ;
			while ((temp = inputStream.read())!=-1) {
				System.out.write(temp);
				//System.out.println(temp);				
			}
			inputStream.close();
		}
		System.out.println("再读");
		if(file.isFile()){
			// 以字符为单位读取文件,常用与读文本,数字等类型的文件
			Reader reader = new InputStreamReader(new FileInputStream(file));
			int temp ;
			while ((temp = reader.read())!=-1) {
				System.out.write(temp);
				//System.out.println(temp);				
			}
			reader.close();
		}
		System.out.println(resource.isOpen());
		
		
	}

 

Spring Core框架中,资源管理(Resource Management)是其核心功能之一,Spring提供了强大的资源抽象机制,允许开发者以统一的方式访问不同类型的资源。资源可以是本地文件系统、类路径资源、URL资源等。Spring通过`Resource`接口和相关的实现类来处理资源加载。 ### 资源接口与实现 Spring框架中的资源管理主要通过 `org.springframework.core.io.Resource` 接口来实现。该接口提供了多种资源访问方式,包括: - **`ClassPathResource`**:用于从类路径加载资源。 - **`FileSystemResource`**:用于从文件系统加载资源。 - **`UrlResource`**:用于从URL加载资源。 - **`ServletContextResource`**:用于从Web应用上下文中加载资源。 - **`InputStreamResource`**:用于包装一个已有的`InputStream`作为资源。 这些实现类使得Spring能够灵活地处理各种资源类型,而无需关心底层资源的具体来源。 ### 资源加载方式 在Spring配置中,可以通过多种方式加载资源。例如,在XML配置文件中,可以通过 `<bean>` 标签的属性来指定资源路径: ```xml <bean id="myBean" class="com.example.MyBean"> <property name="resource" value="classpath:myresource.txt"/> </bean> ``` 在Java配置中,可以通过编程方式加载资源: ```java Resource resource = new ClassPathResource("myresource.txt"); ``` 此外,Spring还支持通过`ResourceLoader`接口来动态加载资源,适用于需要根据运行时条件选择资源的场景。 ### 资源路径通配符 Spring支持使用通配符来匹配多个资源路径: - `classpath*:`:用于匹配类路径下的所有匹配资源。 - `file:`:用于指定文件系统路径。 - `http:` 或 `ftp:`:用于远程资源访问。 例如,使用`classpath*:`可以加载多个JAR包中的相同资源文件: ```java Resource[] resources = new PathMatchingResourcePatternResolver().getResources("classpath*:com/example/config/*.xml"); ``` ### 资源与Spring上下文集成 在Spring应用上下文中,资源管理是自动进行的。当配置文件中指定资源路径时,Spring会自动解析并加载相应的资源。例如,在`ApplicationContext`初始化时,可以通过`contextConfigLocation`参数指定配置文件路径: ```xml <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> ``` Spring会根据该配置加载上下文所需的资源[^4]。 ### 资源管理的最佳实践 - **使用统一的资源抽象**:通过`Resource`接口,屏蔽底层资源的具体实现,提高代码的可移植性。 - **避免硬编码资源路径**:应通过配置文件或环境变量来定义资源路径,便于维护和部署。 - **合理使用通配符**:在需要加载多个资源时,合理使用`classpath*:`等通配符可以简化配置。 - **资源缓存**:对于频繁访问的资源,建议使用缓存机制,提高性能。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值