spring 第9天 Resurce 资源访问

来源:互联网 发布:农村网络消费研究报告 编辑:程序博客网 时间:2024/04/27 15:16

spring 第9天 Resurce 资源访问

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

//Resource的实现类 

UrlResource 
Java代码  收藏代码
  1. package annotation.resources;  
  2.   
  3. import java.io.IOException;  
  4. import java.util.Iterator;  
  5. import java.util.List;  
  6.   
  7. import org.dom4j.Document;  
  8. import org.dom4j.DocumentException;  
  9. import org.dom4j.Element;  
  10. import org.dom4j.io.SAXReader;  
  11. import org.springframework.core.io.UrlResource;  
  12.   
  13. /** 
  14.  * Spring 的Resource 采用 策略模式  即使不用spring 也可以当作工具类来使用   
  15.  * @author Bin 
  16.  */  
  17. public class UrlResourceTest {  
  18.     public static void main(String[] args) throws DocumentException, IOException{  
  19.         UrlResource ur=new UrlResource("file:book.xml");  
  20.         System.out.println(ur.getFilename());  
  21.         System.out.println(ur.getDescription());  
  22.           
  23.         SAXReader read=new SAXReader();  
  24.         Document doc=read.read(ur.getFile());  
  25.           
  26.         Element root=doc.getRootElement();  
  27.         List<Element> list=root.elements();  
  28.           
  29.         for (Iterator<Element> it=list.iterator(); it.hasNext();) {  
  30.             Element book=it.next();  
  31.               
  32.             List<Element> ll=book.elements();  
  33.             for (Iterator<Element> it2=ll.iterator(); it2.hasNext();) {  
  34.                 Element book1=it2.next();  
  35.                 System.out.println(book1.getName());  
  36.                 System.out.println(book1.getText());  
  37.             }  
  38.               
  39.         }  
  40.     }  
  41.   
  42. }  


ClassPathResource 
Java代码  收藏代码
  1. package annotation.resources;  
  2.   
  3. import java.io.IOException;  
  4. import java.util.Iterator;  
  5. import java.util.List;  
  6.   
  7. import org.dom4j.Document;  
  8. import org.dom4j.DocumentException;  
  9. import org.dom4j.Element;  
  10. import org.dom4j.io.SAXReader;  
  11. import org.springframework.core.io.ClassPathResource;  
  12.   
  13. public class ClassPathResourceTest {  
  14.   
  15.     public static void main(String[] args) throws DocumentException, IOException {  
  16.         ClassPathResource cr=new ClassPathResource("applicationContent_anno.xml");    
  17.         System.out.println(cr.getFilename());  
  18.         System.out.println(cr.getDescription());  
  19.           
  20.         SAXReader read=new SAXReader();  
  21.         Document doc=read.read(cr.getFile());  
  22.           
  23.         Element root=doc.getRootElement();  
  24.         List<Element> list=root.elements();  
  25.           
  26.         for (Iterator<Element> it=list.iterator(); it.hasNext();) {  
  27.             Element book=it.next();  
  28.               
  29.             List<Element> ll=book.elements();  
  30.             System.out.println(book.getName());  
  31.             for (Iterator<Element> it2=ll.iterator(); it2.hasNext();) {  
  32.                 Element book1=it2.next();  
  33.                 System.out.println(book1.getName());  
  34.                 System.out.println(book1.getText());  
  35.             }  
  36.               
  37.         }  
  38.     }  
  39.   
  40. }  



FileSystemResource 
Java代码  收藏代码
  1. package annotation.resources;  
  2.   
  3. import java.util.Iterator;  
  4. import java.util.List;  
  5.   
  6. import org.dom4j.Document;  
  7. import org.dom4j.DocumentException;  
  8. import org.dom4j.Element;  
  9. import org.dom4j.io.SAXReader;  
  10. import org.springframework.core.io.FileSystemResource;  
  11.   
  12. public class FileSystemResourceTest {  
  13.   
  14.     public static void main(String[] args) throws DocumentException {  
  15.         FileSystemResource fr=new FileSystemResource("src\\main\\resources\\applicationContent_anno.xml");  
  16.         System.out.println(fr.getFilename());  
  17.         System.out.println(fr.getDescription());  
  18.         SAXReader read=new SAXReader();  
  19.         Document doc=read.read(fr.getFile());  
  20.           
  21.         Element root=doc.getRootElement();  
  22.         List<Element> list=root.elements();  
  23.           
  24.         for (Iterator<Element> it=list.iterator(); it.hasNext();) {  
  25.             Element book=it.next();  
  26.             List<Element> ll=book.elements();  
  27.             System.out.println(book.getName());  
  28.             for (Iterator<Element> it2=ll.iterator(); it2.hasNext();) {  
  29.                 Element book1=it2.next();  
  30.                 System.out.println(book1.getName());  
  31.                 System.out.println(book1.getText());  
  32.             }  
  33.               
  34.         }  
  35.     }  
  36.   
  37. }  


ServletContextResource 
Java代码  收藏代码
  1. <%  
  2. ServletContextResource src=new ServletContextResource(applicationContxt,"WEB-INf/book.xml");  
  3.   
  4. System.out.println(src.getFilename());  
  5.         System.out.println(src.getDescription());  
  6.         SAXReader read=new SAXReader();  
  7.         Document doc=read.read(src.getFile());  
  8. ..  
  9. %>  


ByteStreamResource 
Java代码  收藏代码
  1. package annotation.resources;  
  2.   
  3. import java.io.IOException;  
  4. import java.util.Iterator;  
  5. import java.util.List;  
  6.   
  7. import org.dom4j.Document;  
  8. import org.dom4j.DocumentException;  
  9. import org.dom4j.Element;  
  10. import org.dom4j.io.SAXReader;  
  11. import org.springframework.core.io.ByteArrayResource;  
  12.   
  13. public class ByteArrayResourcesTest {  
  14.   
  15.     public static void main(String[] args) throws DocumentException, IOException {  
  16.         // TODO Auto-generated method stub  
  17.         String file="<dependency>"  
  18.                       +"<groupId>dom4j</groupId>"  
  19.                       +"<artifactId>dom4j</artifactId>"  
  20.                       +"<version>1.6.1</version>"  
  21.                       +"</dependency>";  
  22.           
  23.         byte[] filebytes=file.getBytes();  
  24.         ByteArrayResource bar=new ByteArrayResource(filebytes);  
  25.         System.out.println(bar.getDescription());  
  26.   
  27.         SAXReader read=new SAXReader();  
  28.         Document doc=read.read(bar.getInputStream());  
  29.           
  30.         Element root=doc.getRootElement();  
  31.         List<Element> list=root.elements();  
  32.           
  33.         for (Iterator<Element> it=list.iterator(); it.hasNext();) {  
  34.             Element book=it.next();  
  35.               
  36.             List<Element> ll=book.elements();  
  37.             System.out.println(book.getName());  
  38.             for (Iterator<Element> it2=ll.iterator(); it2.hasNext();) {  
  39.                 Element book1=it2.next();  
  40.                 System.out.println(book1.getName());  
  41.                 System.out.println(book1.getText());  
  42.             }  
  43.               
  44.         }  
  45.     }  
  46.   
  47. }  


ResourceLoader可以获取一个Resource实例 
ApplicationContext接口实现了 ResourceLoader接口 
Java代码  收藏代码
  1. package annotation.resources;  
  2.   
  3.   
  4. import java.io.IOException;  
  5. import java.util.Iterator;  
  6. import java.util.List;  
  7.   
  8. import org.dom4j.Document;  
  9. import org.dom4j.DocumentException;  
  10. import org.dom4j.Element;  
  11. import org.dom4j.io.SAXReader;  
  12. import org.springframework.context.ApplicationContext;  
  13. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  14. import org.springframework.context.support.FileSystemXmlApplicationContext;  
  15. import org.springframework.core.io.Resource;  
  16.   
  17. //查看 spring 采用那个类来加载 资源文件  
  18. public class ResourceAwareTest {  
  19.   
  20.     public static void main(String[] args) throws DocumentException, IOException {  
  21.         // TODO Auto-generated method stub  
  22.         ApplicationContext act=new ClassPathXmlApplicationContext("applicationContent_anno.xml");  
  23.           
  24.         //ApplicationContext act=new FileSystemXmlApplicationContext("applicationContent_anno.xml");  
  25.         Resource res=act.getResource("applicationContent_anno.xml");  
  26.           
  27.         //通过指定前缀 ,来强制使用特定的类来加载资源文件  
  28.         res=act.getResource("classpath:bean.xml");  
  29.         res=act.getResource("file:bean.xml");  
  30.         res=act.getResource("http://location:8080/bean.xml");  
  31.           
  32.         System.out.println(res.getFilename());  
  33.         System.out.println(res.getDescription());  
  34.         SAXReader read=new SAXReader();  
  35.         Document doc=read.read(res.getFile());  
  36.           
  37.         Element root=doc.getRootElement();  
  38.         List<Element> list=root.elements();  
  39.           
  40.         for (Iterator<Element> it=list.iterator(); it.hasNext();) {  
  41.             Element book=it.next();  
  42.             List<Element> ll=book.elements();  
  43.             System.out.println(book.getName());  
  44.             for (Iterator<Element> it2=ll.iterator(); it2.hasNext();) {  
  45.                 Element book1=it2.next();  
  46.                 System.out.println(book1.getName());  
  47.                 System.out.println(book1.getText());  
  48.             }  
  49.               
  50.         }  
  51.     }  
  52.   
  53. }  


ResourceLoaderAware:该接口可以获取到一个ResourceLoader 
Java代码  收藏代码
  1. package annotation.resources;  
  2.   
  3. import org.springframework.context.ResourceLoaderAware;  
  4. import org.springframework.core.io.ResourceLoader;  
  5. import org.springframework.stereotype.Component;  
  6.   
  7. @Component  
  8. public class TestBean implements ResourceLoaderAware {  
  9.     ResourceLoader rd;  
  10.     @Override  
  11.     public void setResourceLoader(ResourceLoader resourceLoader) {  
  12.         // TODO Auto-generated method stub  
  13.         this.rd=resourceLoader;  
  14.     }  
  15.     public ResourceLoader getRd() {  
  16.         return rd;  
  17.     }  
  18.     public void setRd(ResourceLoader rd) {  
  19.         this.rd = rd;  
  20.     }  
  21.       
  22.   
  23. }  

Java代码  收藏代码
  1. //测试 ResourceLoader    
  2.     @Test  
  3.     public void test2() {   //  
  4.         TestBean test=act.getBean("testBean",TestBean.class);  
  5.           
  6.         ResourceLoader rl=test.getRd();  
  7.         //可以看到 true 表明: spring将自己注入到 TestBean 中了  
  8.         System.out.println(rl==act);  
  9.           
  10.         Resource res=rl.getResource("applicationContent_anno.xml");  
  11.           
  12.     }  


使用Resource作为属性 
Java代码  收藏代码
  1. package annotation.resources;  
  2.   
  3. import java.io.IOException;  
  4. import java.util.Iterator;  
  5. import java.util.List;  
  6.   
  7. import org.dom4j.Document;  
  8. import org.dom4j.DocumentException;  
  9. import org.dom4j.Element;  
  10. import org.dom4j.io.SAXReader;  
  11. import org.springframework.beans.factory.annotation.Value;  
  12. import org.springframework.core.io.Resource;  
  13. import org.springframework.stereotype.Component;  
  14.   
  15. @Component  
  16. public class TestBean1 {  
  17.       
  18.     @Value("classpath:applicationContent_anno.xml")  
  19.     Resource res;  
  20.       
  21.     public void parse() throws DocumentException, IOException{  
  22.         System.out.println(res.getFilename());  
  23.         System.out.println(res.getDescription());  
  24.         SAXReader read=new SAXReader();  
  25.         Document doc=read.read(res.getFile());  
  26.           
  27.         Element root=doc.getRootElement();  
  28.         List<Element> list=root.elements();  
  29.           
  30.         for (Iterator<Element> it=list.iterator(); it.hasNext();) {  
  31.             Element book=it.next();  
  32.             List<Element> ll=book.elements();  
  33.             System.out.println(book.getName());  
  34.             for (Iterator<Element> it2=ll.iterator(); it2.hasNext();) {  
  35.                 Element book1=it2.next();  
  36.                 System.out.println(book1.getName());  
  37.                 System.out.println(book1.getText());  
  38.             }  
  39.               
  40.         }  
  41.     }  
  42.   
  43.     public Resource getRes() {  
  44.         return res;  
  45.     }  
  46.   
  47.     public void setRes(Resource res) {  
  48.         this.res = res;  
  49.     }  
  50.       
  51.       
  52.       
  53.   
  54. }  
  55. //可以使用 spring配置文件 给 Resource 赋值  

在ApplicatoinContext中使用资源 
//第一种方式 
Java代码  收藏代码
  1. 1.使用ClassPathXmlApplicationContext  对应ClassPathResource  
  2. 2.使用FileSystemXmlApplicationContext 对应 FileSystemResource  
  3. 3.使用XmlWebApplicationContext :对应 ServletContextResource   
  4.   
  5. ApplicatoinContext act=new FileSystemXmlApplicationContext("bean.xml");  


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


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


//第四种方式 使用file:前缀 
Java代码  收藏代码
  1. //表面上一个是相对路径,一个绝对路径  其实 FileSystemXmlApplicationContext会将所有的FileSystemResource 实例 当成 相对路径来处理.  
  2. ApplicatoinContext act=new FileSystemXmlApplicationContext("bean.xml");  
  3. ApplicatoinContext act=new FileSystemXmlApplicationContext("/bean.xml");  
  4.   
  5. //如果非要区分 相对路径 和绝对路径  
  6. ApplicatoinContext act=new FileSystemXmlApplicationContext("file:bean.xml");  
  7. ApplicatoinContext act=new FileSystemXmlApplicationContext("file:/bean.xml");  
0 0
原创粉丝点击