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
Spring课程 Spring入门篇 3-5 Spring bean装配(上)之Resource的更多相关文章
- Spring Boot -01- 快速入门篇(图文教程)
Spring Boot -01- 快速入门篇(图文教程) 今天开始不断整理 Spring Boot 2.0 版本学习笔记,大家可以在博客看到我的笔记,然后大家想看视频课程也可以到[慕课网]手机 app ...
- Spring实践系列-入门篇(一)
本文主要介绍了在本地搭建并运行一个Spring应用,演示了Spring依赖注入的特性 1 环境搭建 1.1 Maven依赖 目前只用到依赖注入的功能,故以下三个包已满足使用. <properti ...
- Spring Cloud Alibaba入门篇
学习条件 了解web三层架构 熟练应用SSM架构 了解Maven管理工具的使用 熟练使用SpringBoot,以及了解SpringBoot基本原理. 了解部分术语:应用.工具.耦合.负载等 温馨提示: ...
- Spring Data JPA 入门篇
Spring Data JPA是什么 它是Spring基于ORM框架(如hibernate,Mybatis等).JPA规范(Java Persistence API)封装的一套 JPA应用框架,可使开 ...
- Spring Boot源码(四):Bean装配
为了演示Spring中对象是如何创建并放到spring容器中,这里新建一个maven项目: 其中pom.xm文件中只引入了一个依赖: <dependencies> <dependen ...
- Spring课程 Spring入门篇 2-1 IOC和bean容器
课程链接: 本节讲了5部分内容,6为项目demo: 1 接口及面向接口编程 2 什么是IOC 3 Spring的bean配置 4 Bean的初始化 5 Demo 自己理解: 1 高层模块和底层模块都依 ...
- spring boot 学习入门篇【spring boot项目的搭建以及如何加载jsp界面】
[ 前言] Spring Boot 简介:Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置, ...
- spring boot(一):入门篇
构建微服务:Spring boot 入门篇 什么是spring boot Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框 ...
- Spring Boot(一):入门篇+前端访问后端
转自:Spring Boot(一):入门篇 什么是Spring Boot Spring Boot 是由 Pivotal 团队提供的全新框架,其设计目的是用来简化新 Spring 应用的初始搭建以及开发 ...
- (转)Spring boot(一):入门篇
https://www.cnblogs.com/ityouknow/p/5662753.html#!comments 构建微服务:Spring boot 入门篇 什么是Spring Boot Spri ...
随机推荐
- loj #108. 多项式乘法
#108. 多项式乘法 题目描述 这是一道模板题. 输入两个多项式,输出这两个多项式的乘积. 输入格式 第一行两个整数 n nn 和 m mm,分别表示两个多项式的次数. 第二行 n+1 n + ...
- NOIWC2019游记
更新完了? ghj1222这个智障因为NOIP考的太菜没有去THUWC和PKUWC,但是NOIWC还是苟进去了 由于已经结束了,好多事实忘了,所以可能不完整 2019/1/23 Wednesday 明 ...
- [BOI2007]Sequence 序列问题 BZOJ1345
题目描述 对于一个给定的序列a1, …, an,我们对它进行一个操作reduce(i),该操作将数列中的元素ai和ai+1用一个元素max(ai,ai+1)替代,这样得到一个比原来序列短的新序列.这一 ...
- kuangbin专题七 POJ3264 Balanced Lineup (线段树最大最小)
For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same order. One d ...
- Qt 学习之路 2(2):Qt 简介
Home / Qt 学习之路 2 / Qt 学习之路 2(2):Qt 简介 Qt 学习之路 2(2):Qt 简介 豆子 2012年8月21日 Qt 学习之路 2 43条评论 Qt 是一个著名的 ...
- 异步解决方案(三)Promise
首先建议大家先看看这篇博文,这是我看过的最清晰给力的博文了: https://www.cnblogs.com/lvdabao/p/es6-promise-1.html 附赠一篇笑死我了的博客,加入有一 ...
- Apache 403 错误。。
两个方面.. 一: httpd.conf 是否有 <directory '/www'></directory> 是否有 Deny from all 或者 Require l ...
- 2016 Multi-University Training Contest 10 [HDU 5861] Road (线段树:区间覆盖+单点最大小)
HDU 5861 题意 在n个村庄之间存在n-1段路,令某段路开放一天需要交纳wi的费用,但是每段路只能开放一次,一旦关闭将不再开放.现在给你接下来m天内的计划,在第i天,需要对村庄ai到村庄bi的道 ...
- BZOJ1725】[Usaco2006 Nov]Corn Fields牧场的安排 状压DP
Description Farmer John新买了一块长方形的牧场,这块牧场被划分成M列N行(1<=M<=12; 1<=N<=12),每一格都是一块正方形的土地.FJ打算在牧 ...
- Educational Codeforces Round 13 C
Description Little Joty has got a task to do. She has a line of n tiles indexed from 1 to n. She has ...