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 ...
随机推荐
- 好程序员web前端分享CSS元素类型
好程序员web前端分享CSS元素类型 目标 1.元素类型分类依据和元素类型分类 2.元素类型的转换 3.inline-block元素类型的应用 4.置换和非置换元素的概念和应用案例 一.元素类型分类依 ...
- sqlplus能登录但查询数据报ORA-00942: 表或视图不存在
在表名前加表所属的用户就能查了: SELECT * FROM ABC.TABLENAME;(ABC是表的OWNER)
- SP34096 DIVCNTK - Counting Divisors (general)(Min_25筛)
题面 洛谷 \(\sigma_0(i)\) 表示\(i\) 的约数个数 求\(S_k(n)=\sum_{i=1}^n\sigma_0(i^k)\mod 2^{64}\) 多测,\(T\le10^4,n ...
- MongoDB3.2(C#版) CRUD
Retrieve(检索.查询): 分两种(一种是插入对象没有自定义; 第二种就是自定义插入对象) 这两种情况下的区别就是插入数据库中的文档类型不一样,一个是BsonDocument,一个是自定义对象( ...
- HTTP,HTTPS端口号
博主暂做个笔记,查询资料再纠正~~~~小白不要不假思索的抄走~~~ 一般情况下,HTTP默认工作端口是8000,HTTPS默认工作端口是443
- Codeforces-C-Nice Garland(枚举+暴力)
You have a garland consisting of nn lamps. Each lamp is colored red, green or blue. The color of the ...
- 2015苏州大学ACM-ICPC集训队选拔赛(1) 1005
发现 Time Limit : 3000/1000ms (Java/Other) Memory Limit : 65535/32768K (Java/Other) Total Submission ...
- A. Wrong Subtraction
A. Wrong Subtraction time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Appium appium 基础之键盘处理
方法1 AppiumDriver实现了在上述功能,代码如下(java版本) driver.sendKeyEvent(66);方法2 HashMap keycode = new HashMap();ke ...
- Spring实现AOP
转载: https://blog.csdn.net/tolcf/article/details/49133119 [框架][Spring]XML配置实现AOP拦截-切点:JdkRegexpMethod ...