• Spring课程 Spring入门篇 3-5 Spring bean装配(上)之Resource


    课程链接:

    1    resource简析

    2    resource代码演练

    1    resource简析

    urlsource:url对应的资源

    classpath:获取类路径下的资源文件

    filesystemresource:获取文件系统里面的资源

    servletContextResource:servlet封装的资源

    inputStreamResource:输入流封装的资源

    ByteArrayResource:字节数组封装的资源

    2    resource代码演练(可以配置在右键项目==》build path==》source==》add folder==》将加载文件config.txt加载进去即可)

    2.1  classpath:

    实体类:

    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 aContext)
                throws BeansException {
            // TODO Auto-generated method stub
            this.applicationContext = aContext;
        }
        
        public void resource(){
            try {
                Resource resource = applicationContext.getResource("classpath:config.txt");
                System.out.println("fileName is "+resource.getFilename());
                System.out.println("resource's length is "+resource.contentLength());
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        
        
        
    }

    配置文件:

    <?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.resource;
    
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.junit.runners.BlockJUnit4ClassRunner;
    
    import com.imooc.test.base.UnitTestBase;
    
    @RunWith(BlockJUnit4ClassRunner.class)
    public class TestResource extends UnitTestBase{
    
        public TestResource() {
            super("classpath:spring-resource.xml");
        }
        
        
        @Test
        public void testMoocResource(){
            try {
                MoocResource mResource = super.getbean("moocResource");
                mResource.resource();
            } catch (Exception e) {
                // TODO: handle exception
            }
        }    
        
        
    }

    打印结果:

    三月 04, 2019 9:36:41 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
    信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@6e038230: startup date [Mon Mar 04 21:36:41 CST 2019]; root of context hierarchy
    三月 04, 2019 9:36:41 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
    信息: Loading XML bean definitions from class path resource [spring-resource.xml]
    fileName is config.txt
    resource's length is 9
    三月 04, 2019 9:36:41 下午 org.springframework.context.support.ClassPathXmlApplicationContext doClose
    信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@6e038230: startup date [Mon Mar 04 21:36:41 CST 2019]; root of context hierarchy

    config.txt:

    123456789

    2.2  file的形式(除实体类不一样之外,其他的完全一致)

    实体类:

    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 aContext)
                throws BeansException {
            // TODO Auto-generated method stub
            this.applicationContext = aContext;
        }
        
        public void resource(){
            try {
    //            Resource resource = applicationContext.getResource("classpath:config.txt");
                Resource resource = applicationContext.getResource("file:F:\xiangmu3\Xin\FuQiang\Spring\ddwei-dao\src\main\java\config.txt");
                System.out.println("fileName is "+resource.getFilename());
                System.out.println("resource's length is "+resource.contentLength());
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        
        
        
    }

    打印结果:

    三月 05, 2019 6:36:15 上午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
    信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@14bb39a3: startup date [Tue Mar 05 06:36:15 CST 2019]; root of context hierarchy
    三月 05, 2019 6:36:15 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
    信息: Loading XML bean definitions from class path resource [spring-resource.xml]
    fileName is config.txt
    三月 05, 2019 6:36:15 上午 org.springframework.context.support.ClassPathXmlApplicationContext doClose
    信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@14bb39a3: startup date [Tue Mar 05 06:36:15 CST 2019]; root of context hierarchy
    resource's length is 9

    2.3  url的形式

    实体类:

    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 aContext)
                throws BeansException {
            // TODO Auto-generated method stub
            this.applicationContext = aContext;
        }
        
        public void resource(){
            try {
    //            Resource resource = applicationContext.getResource("classpath:config.txt");
    //            Resource resource = applicationContext.getResource("file:F:\xiangmu3\Xin\FuQiang\Spring\ddwei-dao\src\main\java\config.txt");
                Resource resource = applicationContext.getResource("url:https://www.kanzhun.com/gsr856943.html");
                System.out.println("fileName is "+resource.getFilename());
                System.out.println("resource's length is "+resource.contentLength());
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        
        
        
    }

    打印结果:

    三月 05, 2019 6:45:23 上午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
    信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@fc506f7: startup date [Tue Mar 05 06:45:23 CST 2019]; root of context hierarchy
    三月 05, 2019 6:45:23 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
    信息: Loading XML bean definitions from class path resource [spring-resource.xml]
    fileName is gsr856943.html
    resource's length is 9863
    三月 05, 2019 6:45:25 上午 org.springframework.context.support.ClassPathXmlApplicationContext doClose
    信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@fc506f7: startup date [Tue Mar 05 06:45:23 CST 2019]; root of context hierarchy

    2.4  no的形式(除实体类不一样之外,其他的完全一致)

    实体类:

    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 aContext)
                throws BeansException {
            // TODO Auto-generated method stub
            this.applicationContext = aContext;
        }
        
        public void resource(){
            try {
    //            Resource resource = applicationContext.getResource("classpath:config.txt");
    //            Resource resource = applicationContext.getResource("file:F:\xiangmu3\Xin\FuQiang\Spring\ddwei-dao\src\main\java\config.txt");
    //            Resource resource = applicationContext.getResource("url:https://www.kanzhun.com/gsr856943.html");
                
      
           //当没有前缀的时候,依赖于applicationContext,而applicationContext依赖于classPath:*.xml创建(在UnitTestBase java类中创建,前方有概述, //aware创建的applicationContext和UnitTestBase创建出来的applicationContext是同一个) Resource resource = applicationContext.getResource("config.txt"); System.out.println("fileName is "+resource.getFilename()); System.out.println("resource's length is "+resource.contentLength()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }

    打印结果:

    三月 05, 2019 6:49:10 上午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
    信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@14bb39a3: startup date [Tue Mar 05 06:49:10 CST 2019]; root of context hierarchy
    三月 05, 2019 6:49:10 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
    信息: Loading XML bean definitions from class path resource [spring-resource.xml]
    fileName is config.txt
    三月 05, 2019 6:49:11 上午 org.springframework.context.support.ClassPathXmlApplicationContext doClose
    信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@14bb39a3: startup date [Tue Mar 05 06:49:10 CST 2019]; root of context hierarchy
    resource's length is 9
  • 相关阅读:
    学习笔记26_MVC前台强类型参数
    Elasticsearch 和 solr 的区别
    Mysq索引优化(什么情况创建索引,什么情况不创建索引)
    Spring支持的常用数据库传播属性和事务隔离级别
    方法的参数传递机制
    JAVA类初始化和实例初始化
    JAVA设计模式
    CentOS 7 下安装 Nginx 服务,并开机自启动
    JAVA发送xml格式的接口请求
    JAVA接收postman的中raw的参数
  • 原文地址:https://www.cnblogs.com/1446358788-qq/p/10473392.html
Copyright © 2020-2023  润新知