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 ...
随机推荐
- php 过滤掉多维数组空值
//过滤掉空值 function filter_array($arr, $values = ['',[]]){ foreach ($arr as $k => $v) { if (is_array ...
- hdu1845(a^b的因子和%p)
题目链接:http://poj.org/problem?id=1845 思路: 1.整数唯一分解定理: 任意正整数都有且只有一种方式写出其素因子的乘积表达式. a=(p1^k1)*(p2^k2)*(p ...
- js计算每个月的总天数
js中相关日期的计算 var year = new Date().getFullYear(),//计算当前年份 month = new Date().getMonth()+1,//计算当前月份 dat ...
- 树状数组【bzoj1103】: [POI2007]大都市meg
1103: [POI2007]大都市meg 在经济全球化浪潮的影响下,习惯于漫步在清晨的乡间小路的邮递员Blue Mary也开始骑着摩托车传递邮件了. 不过,她经常回忆起以前在乡间漫步的情景.昔日,乡 ...
- maven部署Tomcat(出现空白页面,最终解决)
- 《C程序设计II》简易计算器,杨辉,数字杯子图形
<C程序设计II>简易计算器,杨辉,数字杯子图形.<C程序设计II>简易计算器,杨辉,数字杯子图形.<C程序设计II>简易计算器,杨辉,数字杯子图形. <C程 ...
- java Response 设置中文编码
response.setHeader("Content-type", "text/html;charset=UTF-8"); response.setChara ...
- hdu6229 Wandering Robots 2017沈阳区域赛M题 思维加map
题目传送门 题目大意: 给出一张n*n的图,机器人在一秒钟内任一格子上都可以有五种操作,上下左右或者停顿,(不能出边界,不能碰到障碍物).题目给出k个障碍物,但保证没有障碍物的地方是强联通的,问经过无 ...
- A. Free Cash
A. Free Cash time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- 工具类_GsonUtils
import java.lang.reflect.Type; import com.google.gson.Gson; /** * Gson工具类 2015-02-01<br/> * 1, ...