Spring IOC容器

1.1 IOC概念:控制反转

              控制:接口实现类的控制权

              反转:实现类的控制权从调用类中移除,转交给第三方决定

 1.2资源抽象接口 Resource    提供了更强的底层资源访问能力

                 接口主要方法: boolean exists():资源是否存在

                                        boolean isOpen()资源是否打开

                                        URL getURL()      如果资源可以表示成url ,那么返回资源对应的url

                                        File getFile()    如果底层资源时一个文件,那么就返回对应的File对象

                                      InputStream getInputStream() 返回资源对象对应的输入流

              Resource实现类关系如下

                                       

   其中  虚线表示实现某个接口, 实线表示的时继承关系

     WritableResource 可写资源接口   有两个实现类  即FileSystemResource和PathResource 类

     ByteArrayResource 二进制数组表示的资源,

     ClassPathResource 类路径下的资源,资源已相对于类路径的方式表示

    FileSystemResource 文件系统资源,资源以文件系统路径的方式表示

   InputStreamREsource 以输入流返回表示的资源

ServletContextResource 为访问Web容器上下文中的资源而设计的类,负责以相对于WEB应用根目录的路径下载资源

URLResource  封装java.net.URL 它使用户能够访问任何可以通过URL表示的资源

 

  PathResource和ClassPathResource的具体案例

 public static void textResource() throws IOException {
        //       测试接口Resource的使用
        String filepath = "A:/ReadingSofterware/AllProjects/Mytest/TextDemon/src/main/java/com/example/text/nihao.xml";
//        使用系统文件路径方式加载文件 也可以使用FileSystemResource
        WritableResource res1 = new PathResource(filepath);
//        使用类路径方式加载文件
        Resource res2 = new ClassPathResource("conf/file1.txt");
//使用WritableResource接口写资源文件
        OutputStream stream1 = res1.getOutputStream();
        stream1.write("欢迎光临\n小春论坛".getBytes());
        stream1.close();
//        使用Resource接口读资源文件
        InputStream ins1 = res1.getInputStream();
        InputStream ins2 = res2.getInputStream();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        int i;
        while ((i = ins1.read()) != -1) {
            baos.write(i);
        }
        System.out.println(baos.toString());
        System.out.println("res1:" + res1.getFilename());
        System.out.println("res2:" + res2.getFilename());
    }
View Code

 

资源加载默认采用系统编码读取资源内容,如果资源文件采用特殊的编码格式,可以通过EncodedResource对资源进行编码

   Resource re = new ClassPathResource("conf/file1.txt");
        EncodedResource ure = new EncodedResource(re, "utf-8");
        String content = FileCopyUtils.copyToString(ure.getReader());
        System.out.println(content);
View Code

1.3资源加载器    Spring定义的加载器如下   

       

ResourceLoader接口有getResource(String locationl)方法 可以根据地址加载文件资源,不过,资源地址仅支持带资源前缀的表达式,不支持Ant风格的资源表达式,而ResourcePatternResolver继承自ResourceLoader扩展定义了 getResources(String location)方法,该方法支持资源表达式和Ant风格的资源路劲。   PathMatchingResourcePatternResolver是Spring提供的标准实现类\

测试案例如下    下述案例能com/example目录下所有的xml文件

 ResourcePatternResolver rs = new PathMatchingResourcePatternResolver();
        Resource[] resources = rs.getResources("classpath*:com/example/**/*.xml");
        assertNotNull(resources);
        for (Resource re : resources) {
            System.out.println(re.getDescription());
        }

 

 

 

                                    

 

posted @ 2017-10-27 17:14  张秀杰  阅读(156)  评论(0编辑  收藏  举报