zoukankan      html  css  js  c++  java
  • 学习 Spring (七) Resource

    Spring入门篇 学习笔记

    Resource: Spring 针对资源文件的统一接口

    • UrlResource: URL 对应的资源,根据一个 URL 地址即可构建
    • ClassPathResource: 获取类路径下的资源文件
    • FileSystemResource: 获取文件系统里面的资源
    • ServletContextResource: ServletContext 封装的资源,用于访问 ServletContext 环境下的资源
    • InputStreamResource: 针对于输入流封装的资源
    • ByteArrayResource: 针对于字节数组封装的资源

    ResourceLoader: 所有的 application context 都实现了 ResourceLoader 接口

    public interface ResourceLoader{
        Resource getResource(String location);
    }
    

    ResourceLoader 注入参数前缀:

    前缀 例子 解释
    classpath: classpath:com/myapp/config.xml 从 classpath 加载
    file: file:/data/config.xml 从文件系统加载
    http: http://myserver/logo.png 从 URL 加载
    /data/config.xml 依赖于 ApplicationContext

    示例

    新建类:

    public class MoocResource implements ApplicationContextAware  {
    	
    	private ApplicationContext applicationContext;
    	
    	@Override
    	public void setApplicationContext(ApplicationContext applicationContext)
    			throws BeansException {
    		this.applicationContext = applicationContext;
    	}
    	
    	public void resource() throws IOException {
    		//Resource resource = applicationContext.getResource("classpath:config.txt");
    		//Resource resource = applicationContext.getResource("file:E:\project\java\demo\learningspring\src\main\resources\config.txt");
    		//Resource resource = applicationContext.getResource("https://www.cnblogs.com/victorbu/p/10430698.html");
    		Resource resource = applicationContext.getResource("config.txt");
    		System.out.println(resource.getFilename());
    		System.out.println(resource.contentLength());
    	}
    
    }
    

    添加配置文件:

    <?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.karonda.resource.MoocResource" ></bean>
    	
     </beans>
    

    添加测试类:

    @RunWith(BlockJUnit4ClassRunner.class)
    public class TestResource extends UnitTestBase {
    	
    	public TestResource() {
    		super("classpath:spring-resource.xml");
    	}
    	
    	@Test
    	public void testResource() {
    		MoocResource resource = super.getBean("moocResource");
    		try {
    			resource.resource();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}
    	
    }
    

    源码:learning-spring

  • 相关阅读:
    MS CRM 2011 RC中的新特性(4)——活动方面之批量编辑、自定义活动
    最近的一些有关MS CRM 2011的更新
    MS CRM 2011 RC中的新特性(6)——连接
    MS CRM 2011 RC中的新特性(7)—仪表板
    参加MS CRM2011深度培训课程——第一天
    MS CRM 2011插件调试工具
    MS CRM2011实体介绍(四)——目标管理方面的实体
    MS CRM 2011 RC中的新特性(3)——客户服务管理方面
    MS CRM 2011 RC中的新特性(8)—数据管理
    ExtAspNet 登陆
  • 原文地址:https://www.cnblogs.com/victorbu/p/10431712.html
Copyright © 2011-2022 走看看