`
Luob.
  • 浏览: 1588731 次
  • 来自: 上海
社区版块
存档分类
最新评论

spring 第9天 Resurce 资源访问

阅读更多
Spring Resource接口
spring提供的Resource接口,改进了Java的访问策略,提供了更强的资源访问能力
主要方法如下
getInputStream():定位并打开资源,返回资源对应的流,每次调用都返回新的输入流,调用者必须关闭
existx() 返回Resource所指向的资源是否存在
isOpen() 返回资源文件是否存在
getDescription() 返回资源的描述信息,用户资源处理出错是输入该信息,通常是全限定文件名和实际的URL
getFile() 返回资源对应的file对象 
getURL() 返回资源对应的URL对象

//Resource的实现类

UrlResource
package annotation.resources;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.springframework.core.io.UrlResource;

/**
 * Spring 的Resource 采用 策略模式  即使不用spring 也可以当作工具类来使用  
 * @author Bin
 */
public class UrlResourceTest {
	public static void main(String[] args) throws DocumentException, IOException{
		UrlResource ur=new UrlResource("file:book.xml");
		System.out.println(ur.getFilename());
		System.out.println(ur.getDescription());
		
		SAXReader read=new SAXReader();
		Document doc=read.read(ur.getFile());
		
		Element root=doc.getRootElement();
		List<Element> list=root.elements();
		
		for (Iterator<Element> it=list.iterator(); it.hasNext();) {
			Element book=it.next();
			
			List<Element> ll=book.elements();
			for (Iterator<Element> it2=ll.iterator(); it2.hasNext();) {
				Element book1=it2.next();
				System.out.println(book1.getName());
				System.out.println(book1.getText());
			}
			
		}
	}

}



ClassPathResource

package annotation.resources;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.springframework.core.io.ClassPathResource;

public class ClassPathResourceTest {

	public static void main(String[] args) throws DocumentException, IOException {
		ClassPathResource cr=new ClassPathResource("applicationContent_anno.xml");	
		System.out.println(cr.getFilename());
		System.out.println(cr.getDescription());
		
		SAXReader read=new SAXReader();
		Document doc=read.read(cr.getFile());
		
		Element root=doc.getRootElement();
		List<Element> list=root.elements();
		
		for (Iterator<Element> it=list.iterator(); it.hasNext();) {
			Element book=it.next();
			
			List<Element> ll=book.elements();
			System.out.println(book.getName());
			for (Iterator<Element> it2=ll.iterator(); it2.hasNext();) {
				Element book1=it2.next();
				System.out.println(book1.getName());
				System.out.println(book1.getText());
			}
			
		}
	}

}




FileSystemResource
package annotation.resources;

import java.util.Iterator;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.springframework.core.io.FileSystemResource;

public class FileSystemResourceTest {

	public static void main(String[] args) throws DocumentException {
		FileSystemResource fr=new FileSystemResource("src\\main\\resources\\applicationContent_anno.xml");
		System.out.println(fr.getFilename());
		System.out.println(fr.getDescription());
		SAXReader read=new SAXReader();
		Document doc=read.read(fr.getFile());
		
		Element root=doc.getRootElement();
		List<Element> list=root.elements();
		
		for (Iterator<Element> it=list.iterator(); it.hasNext();) {
			Element book=it.next();
			List<Element> ll=book.elements();
			System.out.println(book.getName());
			for (Iterator<Element> it2=ll.iterator(); it2.hasNext();) {
				Element book1=it2.next();
				System.out.println(book1.getName());
				System.out.println(book1.getText());
			}
			
		}
	}

}



ServletContextResource
<%
ServletContextResource src=new ServletContextResource(applicationContxt,"WEB-INf/book.xml");

System.out.println(src.getFilename());
		System.out.println(src.getDescription());
		SAXReader read=new SAXReader();
		Document doc=read.read(src.getFile());
..
%>


ByteStreamResource
package annotation.resources;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.springframework.core.io.ByteArrayResource;

public class ByteArrayResourcesTest {

	public static void main(String[] args) throws DocumentException, IOException {
		// TODO Auto-generated method stub
		String file="<dependency>"
					  +"<groupId>dom4j</groupId>"
					  +"<artifactId>dom4j</artifactId>"
					  +"<version>1.6.1</version>"
					  +"</dependency>";
		
		byte[] filebytes=file.getBytes();
		ByteArrayResource bar=new ByteArrayResource(filebytes);
		System.out.println(bar.getDescription());

		SAXReader read=new SAXReader();
		Document doc=read.read(bar.getInputStream());
		
		Element root=doc.getRootElement();
		List<Element> list=root.elements();
		
		for (Iterator<Element> it=list.iterator(); it.hasNext();) {
			Element book=it.next();
			
			List<Element> ll=book.elements();
			System.out.println(book.getName());
			for (Iterator<Element> it2=ll.iterator(); it2.hasNext();) {
				Element book1=it2.next();
				System.out.println(book1.getName());
				System.out.println(book1.getText());
			}
			
		}
	}

}


ResourceLoader可以获取一个Resource实例
ApplicationContext接口实现了 ResourceLoader接口
package annotation.resources;


import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.core.io.Resource;

//查看 spring 采用那个类来加载 资源文件
public class ResourceAwareTest {

	public static void main(String[] args) throws DocumentException, IOException {
		// TODO Auto-generated method stub
		ApplicationContext act=new ClassPathXmlApplicationContext("applicationContent_anno.xml");
		
		//ApplicationContext act=new FileSystemXmlApplicationContext("applicationContent_anno.xml");
		Resource res=act.getResource("applicationContent_anno.xml");
		
		//通过指定前缀 ,来强制使用特定的类来加载资源文件
		res=act.getResource("classpath:bean.xml");
		res=act.getResource("file:bean.xml");
		res=act.getResource("http://location:8080/bean.xml");
		
		System.out.println(res.getFilename());
		System.out.println(res.getDescription());
		SAXReader read=new SAXReader();
		Document doc=read.read(res.getFile());
		
		Element root=doc.getRootElement();
		List<Element> list=root.elements();
		
		for (Iterator<Element> it=list.iterator(); it.hasNext();) {
			Element book=it.next();
			List<Element> ll=book.elements();
			System.out.println(book.getName());
			for (Iterator<Element> it2=ll.iterator(); it2.hasNext();) {
				Element book1=it2.next();
				System.out.println(book1.getName());
				System.out.println(book1.getText());
			}
			
		}
	}

}



ResourceLoaderAware:该接口可以获取到一个ResourceLoader
package annotation.resources;

import org.springframework.context.ResourceLoaderAware;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Component;

@Component
public class TestBean implements ResourceLoaderAware {
	ResourceLoader rd;
	@Override
	public void setResourceLoader(ResourceLoader resourceLoader) {
		// TODO Auto-generated method stub
		this.rd=resourceLoader;
	}
	public ResourceLoader getRd() {
		return rd;
	}
	public void setRd(ResourceLoader rd) {
		this.rd = rd;
	}
	

}


//测试 ResourceLoader  
	@Test
	public void test2() {   //
		TestBean test=act.getBean("testBean",TestBean.class);
		
		ResourceLoader rl=test.getRd();
		//可以看到 true 表明: spring将自己注入到 TestBean 中了
		System.out.println(rl==act);
		
		Resource res=rl.getResource("applicationContent_anno.xml");
		
	}


使用Resource作为属性
package annotation.resources;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Component;

@Component
public class TestBean1 {
	
	@Value("classpath:applicationContent_anno.xml")
	Resource res;
	
	public void parse() throws DocumentException, IOException{
		System.out.println(res.getFilename());
		System.out.println(res.getDescription());
		SAXReader read=new SAXReader();
		Document doc=read.read(res.getFile());
		
		Element root=doc.getRootElement();
		List<Element> list=root.elements();
		
		for (Iterator<Element> it=list.iterator(); it.hasNext();) {
			Element book=it.next();
			List<Element> ll=book.elements();
			System.out.println(book.getName());
			for (Iterator<Element> it2=ll.iterator(); it2.hasNext();) {
				Element book1=it2.next();
				System.out.println(book1.getName());
				System.out.println(book1.getText());
			}
			
		}
	}

	public Resource getRes() {
		return res;
	}

	public void setRes(Resource res) {
		this.res = res;
	}
	
	
	

}
//可以使用 spring配置文件 给 Resource 赋值

在ApplicatoinContext中使用资源
//第一种方式
1.使用ClassPathXmlApplicationContext  对应ClassPathResource
2.使用FileSystemXmlApplicationContext 对应 FileSystemResource
3.使用XmlWebApplicationContext :对应 ServletContextResource 

ApplicatoinContext act=new FileSystemXmlApplicationContext("bean.xml");


//第二中方式 采用前缀访问策略
ApplicatoinContext act=new FileSystemXmlApplicationContext("classpath:bean.xml");
//这样就强制转换为ClassPathResource 来加载配置文件了


//第三种方式 使用classpath*: 前缀
//classpath*
ApplicatoinContext act=new FileSystemXmlApplicationContext("classpath*:bean.xml");
ApplicatoinContext act=new FileSystemXmlApplicationContext("classpath*:bean*.xml");
//和classpath的区别是 classpath*:可以加载多个资源文件 ,classpath只能加载一个资源文件


//第四种方式 使用file:前缀
//表面上一个是相对路径,一个绝对路径  其实 FileSystemXmlApplicationContext会将所有的FileSystemResource 实例 当成 相对路径来处理.
ApplicatoinContext act=new FileSystemXmlApplicationContext("bean.xml");
ApplicatoinContext act=new FileSystemXmlApplicationContext("/bean.xml");

//如果非要区分 相对路径 和绝对路径
ApplicatoinContext act=new FileSystemXmlApplicationContext("file:bean.xml");
ApplicatoinContext act=new FileSystemXmlApplicationContext("file:/bean.xml");
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics