课程链接:

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

最新文章

  1. PostgresSQL的安装与基本命令使用
  2. Visual Studio 2013 Nuget控制台无法找到程序包
  3. 3-python学习——变量
  4. Topcoder SRM583 DIV 2 250
  5. DP:Multiplication Puzzle(POJ 1651)
  6. SQL Server数据库层面自定义数据同步性能测试
  7. spring-aop示例
  8. smarty 时间格式化date_format
  9. [Andrew]Ext.Net常用布局(Border布局)
  10. PCB電路板為何要有測試點?
  11. Memcached全面剖析–5. memcached的应用和兼容程序
  12. Java中实现Serializable接口为什么要声明serialVersionUID?
  13. django模型——数据库(二)
  14. Spark环境搭建(六)-----------sprk源码编译
  15. 探究高级的Kotlin Coroutines知识
  16. Java消息中间件入门笔记 - ActiveMQ篇
  17. Opentsdb分布式安装
  18. idea 关闭自动保存,未保存星号提醒, springboot + freemarker 热部署
  19. vuex,文件夹整理
  20. 关于作用域范围Scope

热门文章

  1. 【Linux】-Ubuntu常用命令吐血整理
  2. Linux之杀掉进程
  3. 暴力【bzoj2208】: [Jsoi2010]连通数
  4. ExtJS 4.2.1学习笔记(二)——主题theme
  5. SpringMVC from 表单标签和 input 表单标签
  6. redis实现SSO单点登录,集群,分布式锁
  7. sqlserver 索引进阶(下)
  8. python3 杂记
  9. centos7-根据端口号查看所属应用
  10. 第十一章:DOM扩展