Spring源码之DefaultListableBeanFactory及资源载入
1、XmlBeanFactory 的使用,参考MyEclipse Spring 学习总结一 Spring IOC容器
- public static void main(String[] args) {
- XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));
- HelloWorld obj = (HelloWorld)factory.getBean("helloWorld");
- obj.getMessage();
- }
2、使用DefaultListableBeanFactory和XmlBeanDefinitionReader
ClassPathResource resource = new ClassPathSource("beans.xml");
DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(factory);
reader.loadBeanDefinitions(resource);
Spring加载资源并装配对象的过程
1、定义好Spring的配置文件
2、通过Resource将Spring配置文件进行抽象,抽象成一个Resource对象
3、定义好Bean工厂(各种BeanFactory)
4、定义好XmlBeanDefinitionReader对象,并将工厂对象作为参数传递进去供后续回调使用。
5、通过XmlBeanDefinitionReader对象读取之前抽象出的Resource对象(包含了XML文件的解析过程)
6、本质上,XML文件的解析是有XmlBeanDefinitionReader对象交由BeanDefinitionParserDelegate委托来完成的。实质上这里使用到类委托模式。
7、Ioc容器创建完毕,用户可以通过容器获取所需的对象信息。
3、查看ClassPathResource
首先查看InputStreamSource接口,里面定义了一个getInputStream方法
- public interface InputStreamSource {
- /**
- * Return an {@link InputStream}.
- * <p>It is expected that each call creates a <i>fresh</i> stream.
- * <p>This requirement is particularly important when you consider an API such
- * as JavaMail, which needs to be able to read the stream multiple times when
- * creating mail attachments. For such a use case, it is <i>required</i>
- * that each <code>getInputStream()</code> call returns a fresh stream.
- * @throws IOException if the stream could not be opened
- * @see org.springframework.mail.javamail.MimeMessageHelper#addAttachment(String, InputStreamSource)
- */
- InputStream getInputStream() throws IOException;
- }
然后查看Resource接口
- public interface Resource extends InputStreamSource {
- /**
- * Return whether this resource actually exists in physical form.
- * <p>This method performs a definitive existence check, whereas the
- * existence of a <code>Resource</code> handle only guarantees a
- * valid descriptor handle.
- */
- boolean exists();
- /**
- * Return whether the contents of this resource can be read,
- * e.g. via {@link #getInputStream()} or {@link #getFile()}.
- * <p>Will be <code>true</code> for typical resource descriptors;
- * note that actual content reading may still fail when attempted.
- * However, a value of <code>false</code> is a definitive indication
- * that the resource content cannot be read.
- * @see #getInputStream()
- */
- boolean isReadable();
- /**
- * Return whether this resource represents a handle with an open
- * stream. If true, the InputStream cannot be read multiple times,
- * and must be read and closed to avoid resource leaks.
- * <p>Will be <code>false</code> for typical resource descriptors.
- */
- boolean isOpen();
- /**
- * Return a URL handle for this resource.
- * @throws IOException if the resource cannot be resolved as URL,
- * i.e. if the resource is not available as descriptor
- */
- URL getURL() throws IOException;
- /**
- * Return a URI handle for this resource.
- * @throws IOException if the resource cannot be resolved as URI,
- * i.e. if the resource is not available as descriptor
- */
- URI getURI() throws IOException;
- /**
- * Return a File handle for this resource.
- * @throws IOException if the resource cannot be resolved as absolute
- * file path, i.e. if the resource is not available in a file system
- */
- File getFile() throws IOException;
- /**
- * Determine the content length for this resource.
- * @throws IOException if the resource cannot be resolved
- * (in the file system or as some other known physical resource type)
- */
- long contentLength() throws IOException;
- /**
- * Determine the last-modified timestamp for this resource.
- * @throws IOException if the resource cannot be resolved
- * (in the file system or as some other known physical resource type)
- */
- long lastModified() throws IOException;
- /**
- * Create a resource relative to this resource.
- * @param relativePath the relative path (relative to this resource)
- * @return the resource handle for the relative resource
- * @throws IOException if the relative resource cannot be determined
- */
- Resource createRelative(String relativePath) throws IOException;
- /**
- * Determine a filename for this resource, i.e. typically the last
- * part of the path: for example, "myfile.txt".
- * <p>Returns <code>null</code> if this type of resource does not
- * have a filename.
- */
- String getFilename();
- /**
- * Return a description for this resource,
- * to be used for error output when working with the resource.
- * <p>Implementations are also encouraged to return this value
- * from their <code>toString</code> method.
- * @see java.lang.Object#toString()
- */
- String getDescription();
- /**
- * {@inheritDoc}
- * @return the input stream for the underlying resource (must not be {@code null}).
- */
- public InputStream getInputStream() throws IOException;
- }
4、ClassPathResource 源码
- public class ClassPathResource extends AbstractFileResolvingResource {
- private final String path;
- private ClassLoader classLoader;
- private Class<?> clazz;
- /**
- * Create a new ClassPathResource for ClassLoader usage.
- * A leading slash will be removed, as the ClassLoader
- * resource access methods will not accept it.
- * <p>The thread context class loader will be used for
- * loading the resource.
- * @param path the absolute path within the class path
- * @see java.lang.ClassLoader#getResourceAsStream(String)
- * @see org.springframework.util.ClassUtils#getDefaultClassLoader()
- */
- public ClassPathResource(String path) {
- this(path, (ClassLoader) null);
- }
- /**
- * Create a new ClassPathResource for ClassLoader usage.
- * A leading slash will be removed, as the ClassLoader
- * resource access methods will not accept it.
- * @param path the absolute path within the classpath
- * @param classLoader the class loader to load the resource with,
- * or <code>null</code> for the thread context class loader
- * @see java.lang.ClassLoader#getResourceAsStream(String)
- */
- public ClassPathResource(String path, ClassLoader classLoader) {
- Assert.notNull(path, "Path must not be null");
- String pathToUse = StringUtils.cleanPath(path);
- if (pathToUse.startsWith("/")) {
- pathToUse = pathToUse.substring(1);
- }
- this.path = pathToUse;
- this.classLoader = (classLoader != null ? classLoader : ClassUtils.getDefaultClassLoader());
- }
- /**
- * Create a new ClassPathResource for Class usage.
- * The path can be relative to the given class,
- * or absolute within the classpath via a leading slash.
- * @param path relative or absolute path within the class path
- * @param clazz the class to load resources with
- * @see java.lang.Class#getResourceAsStream
- */
- public ClassPathResource(String path, Class<?> clazz) {
- Assert.notNull(path, "Path must not be null");
- this.path = StringUtils.cleanPath(path);
- this.clazz = clazz;
- }
- /**
- * Create a new ClassPathResource with optional ClassLoader and Class.
- * Only for internal usage.
- * @param path relative or absolute path within the classpath
- * @param classLoader the class loader to load the resource with, if any
- * @param clazz the class to load resources with, if any
- */
- protected ClassPathResource(String path, ClassLoader classLoader, Class<?> clazz) {
- this.path = StringUtils.cleanPath(path);
- this.classLoader = classLoader;
- this.clazz = clazz;
- }
- /**
- * Return the path for this resource (as resource path within the class path).
- */
- public final String getPath() {
- return this.path;
- }
- /**
- * Return the ClassLoader that this resource will be obtained from.
- */
- public final ClassLoader getClassLoader() {
- return (this.classLoader != null ? this.classLoader : this.clazz.getClassLoader());
- }
- /**
- * This implementation checks for the resolution of a resource URL.
- * @see java.lang.ClassLoader#getResource(String)
- * @see java.lang.Class#getResource(String)
- */
- @Override
- public boolean exists() {
- URL url;
- if (this.clazz != null) {
- url = this.clazz.getResource(this.path);
- }
- else {
- url = this.classLoader.getResource(this.path);
- }
- return (url != null);
- }
- /**
- * This implementation opens an InputStream for the given class path resource.
- * @see java.lang.ClassLoader#getResourceAsStream(String)
- * @see java.lang.Class#getResourceAsStream(String)
- */
- public InputStream getInputStream() throws IOException {
- InputStream is;
- if (this.clazz != null) {
- is = this.clazz.getResourceAsStream(this.path);
- }
- else {
- is = this.classLoader.getResourceAsStream(this.path);
- }
- if (is == null) {
- throw new FileNotFoundException(getDescription() + " cannot be opened because it does not exist");
- }
- return is;
- }
- /**
- * This implementation returns a URL for the underlying class path resource.
- * @see java.lang.ClassLoader#getResource(String)
- * @see java.lang.Class#getResource(String)
- */
- @Override
- public URL getURL() throws IOException {
- URL url;
- if (this.clazz != null) {
- url = this.clazz.getResource(this.path);
- }
- else {
- url = this.classLoader.getResource(this.path);
- }
- if (url == null) {
- throw new FileNotFoundException(getDescription() + " cannot be resolved to URL because it does not exist");
- }
- return url;
- }
- /**
- * This implementation creates a ClassPathResource, applying the given path
- * relative to the path of the underlying resource of this descriptor.
- * @see org.springframework.util.StringUtils#applyRelativePath(String, String)
- */
- @Override
- public Resource createRelative(String relativePath) {
- String pathToUse = StringUtils.applyRelativePath(this.path, relativePath);
- return new ClassPathResource(pathToUse, this.classLoader, this.clazz);
- }
- /**
- * This implementation returns the name of the file that this class path
- * resource refers to.
- * @see org.springframework.util.StringUtils#getFilename(String)
- */
- @Override
- public String getFilename() {
- return StringUtils.getFilename(this.path);
- }
- /**
- * This implementation returns a description that includes the class path location.
- */
- public String getDescription() {
- StringBuilder builder = new StringBuilder("class path resource [");
- String pathToUse = path;
- if (this.clazz != null && !pathToUse.startsWith("/")) {
- builder.append(ClassUtils.classPackageAsResourcePath(this.clazz));
- builder.append('/');
- }
- if (pathToUse.startsWith("/")) {
- pathToUse = pathToUse.substring(1);
- }
- builder.append(pathToUse);
- builder.append(']');
- return builder.toString();
- }
- /**
- * This implementation compares the underlying class path locations.
- */
- @Override
- public boolean equals(Object obj) {
- if (obj == this) {
- return true;
- }
- if (obj instanceof ClassPathResource) {
- ClassPathResource otherRes = (ClassPathResource) obj;
- return (this.path.equals(otherRes.path)
- && ObjectUtils.nullSafeEquals(this.classLoader, otherRes.classLoader) && ObjectUtils.nullSafeEquals(
- this.clazz, otherRes.clazz));
- }
- return false;
- }
- /**
- * This implementation returns the hash code of the underlying
- * class path location.
- */
- @Override
- public int hashCode() {
- return this.path.hashCode();
- }
- }
Spring源码之DefaultListableBeanFactory及资源载入的更多相关文章
- spring源码第一章_获取源码并将源码转为eclipse工程
1.通过http://gitforwindows.org/下载github 2.通过http://services.gradle.org/distributions/下载gradle:gardle类似 ...
- Spring源码分析——资源访问利器Resource之实现类分析
今天来分析Spring的资源接口Resource的各个实现类.关于它的接口和抽象类,参见上一篇博文——Spring源码分析——资源访问利器Resource之接口和抽象类分析 一.文件系统资源 File ...
- 【Spring 源码】Spring 加载资源并装配对象的过程(XmlBeanDefinitionReader)
Spring 加载资源并装配对象过程 在Spring中对XML配置文件的解析从3.1版本开始不再推荐使用XmlBeanFactory而是使用XmlBeanDefinitionReader. Class ...
- Spring源码分析——资源访问利器Resource之接口和抽象类分析
从今天开始,一步步走上源码分析的路.刚开始肯定要从简单着手.我们先从Java发展史上最强大的框架——Spring...旗下的资源抽象接口Resource开始吧. 我看了好多分析Spring源码的,每每 ...
- Spring源码学习-容器BeanFactory(一) BeanDefinition的创建-解析资源文件
写在前面 从大四实习至今已一年有余,作为一个程序员,一直没有用心去记录自己工作中遇到的问题,甚是惭愧,打算从今日起开始养成写博客的习惯.作为一名java开发人员,Spring是永远绕不过的话题,它的设 ...
- Spring源码解读:核心类DefaultListableBeanFactory的继承体系
1 简介 我们常用的ClassPathXmlApplicationContext是AbstractRefreshableApplicationContext的子类,而DefaultListableBe ...
- Spring 源码学习 04:初始化容器与 DefaultListableBeanFactory
前言 在前一篇文章:创建 IoC 容器的几种方式中,介绍了四种方式,这里以 AnnotationConfigApplicationContext 为例,跟进代码,看看 IoC 的启动流程. 入口 从 ...
- 阅读spring源码
读Spring源码之前,你要先清楚,为什么你要用Spring... Spring最基本的功能是做为管理bean的容器,所以我以为应该先从org.springframework.context包了解咯, ...
- [spring源码] 小白级别的源码解析ioc(二)
之前一篇,整体描述了一下 Spring的整体概况和 jar包的介绍. 现在开始进入具体的源码解析,从本篇开始,先介绍spring的ioc容器.之前也看过一些介绍spring源码的, 有的是只讲整体的接 ...
随机推荐
- git 分支查看与切换
git 分支查看与切换 # 1.查看所有分支 > git branch -a # 2.查看当前使用分支(结果列表中前面标*号的表示当前使用分支) > git branch # 3.切换分支 ...
- Android笔记(五十二) 侧滑菜单SlidingMenu
SlidingMenu是一个优秀的开源项目,可以实现侧滑菜单,简单介绍一下这SlidingMenu的使用: 常用属性和方法: setTouchModeAbove(int i )是否可以通过滑动手势打开 ...
- php连接mySql,加密函数
连接MySQL mysql_connect(servername,username,password); 面向对象: <?php $servername = "localhost&qu ...
- Java注解annotation : invalid type of annotation member
前言 首先,关于注解的介绍就不多描述了,网上有很多这方面的资料.本文主要是介绍如何处理标题中遇到的问题:invalid type of annotation member ? 正文 Annotatio ...
- 适用于在线服务的A/B测试方法论
适用于在线服务的A/B测试方法论 简介: 这篇文章旨在介绍适用于为在线服务进行A/B测试(A/B Test)的方法论.中文网络中目前还缺乏全面的入门级介绍. 我将首先讨论在线服务业进行A/B测试所考虑 ...
- window下关闭占用端口使用
怎么在window下关闭端口! 1:查看特定端口被占用情况 命令: netstat -ano 和 netstat -ano|findstr 端口号 netstat -ano:查看电脑所有端口被占用 ...
- 圆柱模板价格计算器V1.0版本
因很多客户需求,就做了一个初始版本的产品圆柱模板面积和价格的计算器,界面非常简单,做工粗糙,但是功能是可以运行.后期会在界面和功能上进行升级,打算出一个微信小程序版本.这个程序仅供参考. 演示地址:h ...
- ado.net核心:DataTable对象
概述 DataTable表示内存中数据的一个表. .net命名空间:System.Data DataTable构造方法 DataTable() //不带参数初始化DataTable 类的新实例. Da ...
- Vue 组件生命周期钩子
Vue 组件生命周期钩子 # 1)一个组件从创建到销毁的整个过程,就称之为组件的生命周期 # 2)在组件创建到销毁的过程中,会出现众多关键的时间节点, 如: 组件要创建了.组件创建完毕了.组件数据渲染 ...
- NOI.ac 模拟赛20181103 排队 翘课 运气大战
题解 排队 20% 1≤n≤20,1≤x,hi≤201\le n\le 20, 1\le x,h_i\le 201≤n≤20,1≤x,hi≤20 随便暴力 50% 1≤n≤2000,1≤x,hi≤1 ...