课程链接:

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

Spring课程 Spring入门篇 3-5 Spring bean装配(上)之Resource的更多相关文章

  1. Spring Boot -01- 快速入门篇(图文教程)

    Spring Boot -01- 快速入门篇(图文教程) 今天开始不断整理 Spring Boot 2.0 版本学习笔记,大家可以在博客看到我的笔记,然后大家想看视频课程也可以到[慕课网]手机 app ...

  2. Spring实践系列-入门篇(一)

    本文主要介绍了在本地搭建并运行一个Spring应用,演示了Spring依赖注入的特性 1 环境搭建 1.1 Maven依赖 目前只用到依赖注入的功能,故以下三个包已满足使用. <properti ...

  3. Spring Cloud Alibaba入门篇

    学习条件 了解web三层架构 熟练应用SSM架构 了解Maven管理工具的使用 熟练使用SpringBoot,以及了解SpringBoot基本原理. 了解部分术语:应用.工具.耦合.负载等 温馨提示: ...

  4. Spring Data JPA 入门篇

    Spring Data JPA是什么 它是Spring基于ORM框架(如hibernate,Mybatis等).JPA规范(Java Persistence API)封装的一套 JPA应用框架,可使开 ...

  5. Spring Boot源码(四):Bean装配

    为了演示Spring中对象是如何创建并放到spring容器中,这里新建一个maven项目: 其中pom.xm文件中只引入了一个依赖: <dependencies> <dependen ...

  6. Spring课程 Spring入门篇 2-1 IOC和bean容器

    课程链接: 本节讲了5部分内容,6为项目demo: 1 接口及面向接口编程 2 什么是IOC 3 Spring的bean配置 4 Bean的初始化 5 Demo 自己理解: 1 高层模块和底层模块都依 ...

  7. spring boot 学习入门篇【spring boot项目的搭建以及如何加载jsp界面】

    [ 前言]  Spring Boot 简介:Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置, ...

  8. spring boot(一):入门篇

    构建微服务:Spring boot 入门篇 什么是spring boot Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框 ...

  9. Spring Boot(一):入门篇+前端访问后端

    转自:Spring Boot(一):入门篇 什么是Spring Boot Spring Boot 是由 Pivotal 团队提供的全新框架,其设计目的是用来简化新 Spring 应用的初始搭建以及开发 ...

  10. (转)Spring boot(一):入门篇

    https://www.cnblogs.com/ityouknow/p/5662753.html#!comments 构建微服务:Spring boot 入门篇 什么是Spring Boot Spri ...

随机推荐

  1. loj#6435. 「PKUSC2018」星际穿越(倍增)

    题面 传送门 题解 我们先想想,在这个很特殊的图里该怎么走最短路 先设几个量,\(a_i\)表示\([a_i,i-1]\)之间的点都和\(i\)有边(即题中的\(l_i\)),\(l\)表示当前在计算 ...

  2. Oracle练习(2)

    有如下三张表: 销售表:SALE_FACT  工号   年月   城市   客户   销售额  C00001 201601 上海 A 1000 C00001 201601 上海 B 5000 C000 ...

  3. 学习C/C++需要掌握哪些知识

    初级阶段 1.C语言 数据类型.变量.内存布局.指针基础: 字符串.一维数组.二维数组: 一级指针,二级指针,三级指针,N级指针概念,指针数组和数组指针: 结构体.文件的使用: 动态库的封装和设计: ...

  4. Codeforces Beta Round #87 (Div. 2 Only)-Party(DFS找树的深度)

    A company has n employees numbered from 1 to n. Each employee either has no immediate manager or exa ...

  5. Go语言基础之21--反射

    一.变量介绍 1.1 变量的内在机制 A. 类型信息,这部分是元信息,是预先定义好的:比如:string.int等 B. 值类型,这部分是程序运行过程中,动态改变的:比如:是变量实例所存储的真是的值. ...

  6. ZPL打印机公用代码

    using System;using System.Collections.Generic;using System.Linq;using System.Web; using System.Text; ...

  7. Photoshop入门教程(五):滤镜

    学习心得:滤镜通常用于摄影行业,是安装在相机镜头前用于过滤自然光的附加镜头,从而获得一些特殊的效果.同理,Photoshop的滤镜也是为了产生特殊的效果.Photoshop滤镜分为两类:一种是内部滤镜 ...

  8. Lambda 快速改造

    对于只有一个参数的接口类,可以使用e->{}  idea会给出快速提示 先写正常的表达式 将虚线处直接删掉, 在原来的方法参数和左大括号之间加上 -> 改造后比原来少了几行, 对于熟手阅读 ...

  9. man bash 关于shell的应有尽有 语法、快捷键...

    文件加载顺序 for if case ... 语法 往前移动一个单词 alt f https://github.com/hokein/Wiki/wiki/Bash-Shell%E5%B8%B8%E7% ...

  10. OpenCV教程(转自:浅墨_毛星云博客)

    2.图像的载入,显示和输出 一站式完全解析 本系列文章由@浅墨_毛星云 出品,转载请注明出处. 文章链接:http://blog.csdn.net/poem_qianmo/article/detail ...