解析Spring源码(3)---XmlBeanDefinitionReader(1)
来源:互联网 发布:java面向对象易错题 编辑:程序博客网 时间:2024/06/18 15:05
上节我们对XmlBeanFactory进行了初步了解
简化版源码:
/** * public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFactory implements ConfigurableListableBeanFactory, BeanDefinitionRegistry, Serializable public XmlBeanDefinitionReader(BeanDefinitionRegistry registry) { super(registry); } protected AbstractBeanDefinitionReader(BeanDefinitionRegistry registry) { this.registry = registry; */ //reader的registry得到初始化值为XmlBeanFactory对象,this指代的就是此时创建的XmlBeanFactory对象 private final XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(this);
首先分析一下上面这段代码(调用有参构造函数);
/** * 在XmlBeanFactory进行了实例化操作,传递XmlBeanFactory对象参数 */ public XmlBeanDefinitionReader(BeanDefinitionRegistry registry) { super(registry); }
/** * */ protected AbstractBeanDefinitionReader(BeanDefinitionRegistry registry) { // private final BeanDefinitionRegistry registry;属性初始化XmlBeanFactory对象 this.registry = registry; //private ResourceLoader resourceLoader;赋值 this.resourceLoader = new PathMatchingResourcePatternResolver(); this.environment = new StandardEnvironment();//初始化,创建StandardEnvironment对象 }
this.resourceLoader = new PathMatchingResourcePatternResolver();
private final ResourceLoader resourceLoader; /** * 默认构造函数,初始化 * Create a new PathMatchingResourcePatternResolver with a DefaultResourceLoader. */ public PathMatchingResourcePatternResolver() { this.resourceLoader = new DefaultResourceLoader(); }
/** * Create a new DefaultResourceLoader. */ public DefaultResourceLoader() { this.classLoader = ClassUtils.getDefaultClassLoader(); }
/** * 返回类加载器 * Return the default ClassLoader to use: typically the thread context */ public static ClassLoader getDefaultClassLoader() { ClassLoader cl = null; //获得类加载器 cl = Thread.currentThread().getContextClassLoader(); return cl; }
返回的是同一个类加载器;
通过以上操作:
我们得到了什么?
private final XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(this);//对象的属性this.registry = registry;this.resourceLoader = new PathMatchingResourcePatternResolver(); new PathMatchingResourcePatternResolver();对象的属性 this.resourceLoader = new DefaultResourceLoader(); new DefaultResourceLoader();对象的属性 this.classLoader = ClassUtils.getDefaultClassLoader();赋值与ClassPathResource同一个类加载器
综上所述:reader可以获取到XmlBeanFactory对象和类加载器!!!
原版代码:
/** * public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFactory implements ConfigurableListableBeanFactory, BeanDefinitionRegistry, Serializable public XmlBeanDefinitionReader(BeanDefinitionRegistry registry) { super(registry); } protected AbstractBeanDefinitionReader(BeanDefinitionRegistry registry) { Assert.notNull(registry, "BeanDefinitionRegistry must not be null"); this.registry = registry; */ //reader的registry得到初始化值为XmlBeanFactory对象,this指代的就是此时创建的XmlBeanFactory对象 private final XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(this);
首先分析一下上面这段代码(调用有参构造函数);
/** * 在XmlBeanFactory进行了实例化操作,传递XmlBeanFactory对象参数 * Create new XmlBeanDefinitionReader for the given bean factory. * @param registry the BeanFactory to load bean definitions into, * in the form of a BeanDefinitionRegistry */ public XmlBeanDefinitionReader(BeanDefinitionRegistry registry) { super(registry); }
/** * Create a new AbstractBeanDefinitionReader for the given bean factory. * <p>If the passed-in bean factory does not only implement the BeanDefinitionRegistry * interface but also the ResourceLoader interface, it will be used as default * ResourceLoader as well. This will usually be the case for * {@link org.springframework.context.ApplicationContext} implementations. * <p>If given a plain BeanDefinitionRegistry, the default ResourceLoader will be a * {@link org.springframework.core.io.support.PathMatchingResourcePatternResolver}. * <p>If the passed-in bean factory also implements {@link EnvironmentCapable} its * environment will be used by this reader. Otherwise, the reader will initialize and * use a {@link StandardEnvironment}. All ApplicationContext implementations are * EnvironmentCapable, while normal BeanFactory implementations are not. * @param registry the BeanFactory to load bean definitions into, * in the form of a BeanDefinitionRegistry * @see #setResourceLoader * @see #setEnvironment */ protected AbstractBeanDefinitionReader(BeanDefinitionRegistry registry) { Assert.notNull(registry, "BeanDefinitionRegistry must not be null"); // private final BeanDefinitionRegistry registry;属性初始化XmlBeanFactory对象 this.registry = registry; // Determine ResourceLoader to use. //false if (this.registry instanceof ResourceLoader) { this.resourceLoader = (ResourceLoader) this.registry; } else { //private ResourceLoader resourceLoader;赋值 this.resourceLoader = new PathMatchingResourcePatternResolver(); } // Inherit Environment if possible if (this.registry instanceof EnvironmentCapable) { this.environment = ((EnvironmentCapable) this.registry).getEnvironment(); } else { this.environment = new StandardEnvironment();//初始化,创建StandardEnvironment对象 } }
this.resourceLoader = new PathMatchingResourcePatternResolver();
private final ResourceLoader resourceLoader; /** * 默认构造函数,初始化 * Create a new PathMatchingResourcePatternResolver with a DefaultResourceLoader. * <p>ClassLoader access will happen via the thread context class loader. * @see org.springframework.core.io.DefaultResourceLoader */ public PathMatchingResourcePatternResolver() { this.resourceLoader = new DefaultResourceLoader(); }
/** * Create a new DefaultResourceLoader. * <p>ClassLoader access will happen using the thread context class loader * at the time of this ResourceLoader's initialization. * @see java.lang.Thread#getContextClassLoader() */ public DefaultResourceLoader() { this.classLoader = ClassUtils.getDefaultClassLoader(); }
/** * 返回类加载器 * Return the default ClassLoader to use: typically the thread context * ClassLoader, if available; the ClassLoader that loaded the ClassUtils * class will be used as fallback. * <p>Call this method if you intend to use the thread context ClassLoader * in a scenario where you clearly prefer a non-null ClassLoader reference: * for example, for class path resource loading (but not necessarily for * {@code Class.forName}, which accepts a {@code null} ClassLoader * reference as well). * @return the default ClassLoader (only {@code null} if even the system * ClassLoader isn't accessible) * @see Thread#getContextClassLoader() * @see ClassLoader#getSystemClassLoader() */ public static ClassLoader getDefaultClassLoader() { ClassLoader cl = null; try { //获得类加载器 cl = Thread.currentThread().getContextClassLoader(); } catch (Throwable ex) { // Cannot access thread context ClassLoader - falling back... } if (cl == null) { // No thread context class loader -> use class loader of this class. cl = ClassUtils.class.getClassLoader(); if (cl == null) { // getClassLoader() returning null indicates the bootstrap ClassLoader try { cl = ClassLoader.getSystemClassLoader(); } catch (Throwable ex) { // Cannot access system ClassLoader - oh well, maybe the caller can live with null... } } } return cl; }
返回的是同一个类加载器;
通过以上操作:
我们得到了什么?
private final XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(this);//对象的属性this.registry = registry;this.resourceLoader = new PathMatchingResourcePatternResolver(); new PathMatchingResourcePatternResolver();对象的属性 this.resourceLoader = new DefaultResourceLoader(); new DefaultResourceLoader();对象的属性 this.classLoader = ClassUtils.getDefaultClassLoader();赋值与ClassPathResource同一个类加载器
综上所述:reader可以获取到XmlBeanFactory对象和类加载器!!!
0 0
- 解析Spring源码(3)---XmlBeanDefinitionReader(1)
- 解析Spring源码(4)--XmlBeanDefinitionReader
- 解析Spring源码(9)--XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(this);
- XmlBeanDefinitionReader-----Spring源码解析 配置文件装载与解析
- Spring4.3.x 浅析xml配置的解析过程(1)——使用XmlBeanDefinitionReader解析xml配置
- spring源码之XmlBeanDefinitionReader与bean的注册
- spring 源码解读与设计详解:5 XmlBeanDefinitionReader与Resource
- Spring核心类XmlBeanDefinitionReader
- spring源码解析(收藏)
- spring源码解析(一)
- spring源码解析(二)
- Spring源码解析(一)
- xmlBeanDefinitionReader解析xml的过程
- structure-3-XmlBeanDefinitionReader
- Spring源码解析之-Aop源码解析(2)
- Spring源码解析--Spring配置文件解析BeanDefinitionParserDelegate(四)
- Spring源码解析--Spring配置文件解析NamespaceHandler(五)
- spring IoC源码分析 (3)Resource解析
- Ubuntu自动获取IP地址的相关操作/方法
- 算法学习-在路上
- poj 3468
- 快速上手Ubuntu之安装篇——安装win7,Ubuntu16.04双系统
- C++单例模式
- 解析Spring源码(3)---XmlBeanDefinitionReader(1)
- clojure 项目构建 boot
- 毕业一年的我
- 大数据IMF传奇行动绝密课程第32课:Spark Worker原理和源码剖析解密
- 【HDU5816】Hearthstone(状压DP)
- Boost基础学习
- EasyUI数据分页实现(真假分页)
- 简单客户端 服务端代码
- php CGI、Fastcgi、PHP-FPM的详细介绍与之间的关系