spring 框架 - litter-fish/ReadSource GitHub Wiki

DefaultListableBeanFactory结构体系

DefaultListableBeanFactory.png

  1. AliasRegistry:alias简单的增删改接口
  2. SimpleAliasRegistry:使用map存储alias,对AliasRegistry接口的实现
  3. SingletonBeanRegistry:对单例bean的注册和获取
  4. BeanFactory:获取bean及bean各种属性
  5. DefaultSingletonBeanRegistry:SingletonBeanRegistry的默认实现
  6. HierarchicalBeanFactory:继承BeanFactory增加对parentFactory的实现
  7. BeanDefinitionRegistry:定义对BeanDefinition的增删改查操作
  8. FactoryBeanRegistrySupport:继承DefaultSingletonBeanRegistry,增加对FactoryBean的特殊处理
  9. ConfigurableBeanFactory:继承HierarchicalBeanFactory、SingletonBeanRegistry,提供配置Factory的各种属性
  10. ListableBeanFactory:提供根据各种条件获取bean的方法
  11. AbstractBeanFactory:继承FactoryBeanRegistrySupport并实现ConfigurableBeanFactory接口
  12. AutowireCapableBeanFactory:提供创建bean、自动注入、初始化以及应用bean的后处理器
  13. AbstractAutowireCapableBeanFactory:继承AbstractBeanFactory并实现AutowireCapableBeanFactory接口
  14. ConfigurableListableBeanFactory中:BeanFactory配置清单,指定忽略类型及接口等
  15. DefaultListableBeanFactory:综合上述功能

XmlBeanDefinitionReader

XmlBeanDefinitionReader.png

  1. ResourceLoader:定义资源加载器,根据给定的文件地址返回Resource对象
  2. BeanDefinitionReader:定义资源文件读取并转换为BeanDefinition的各种功能
  3. EnvironmentCapable:定义获取Environment的方法
  4. DocumentLoader:定义从文件加载到转换为Document的功能
  5. AbstractBeanDefinitionReader:实现EnvironmentCapable, BeanDefinitionReader接口
  6. BeanDefinitionDocumentReader:定义读取Document并注册BeanDefinition功能
  7. BeanDefinitionParserDelegate:定义解析各种Element的方法

步骤:

  1. 通过AbstractBeanDefinitionReader中的方法,来使用ResourceLoader将资源文件路径转换为Resource对象
  2. 通过DocumentLoader 对Resource文件进行转换,将Resource文件转换为Document文件
  3. 通过实现BeanDefinitionDocumentReader接口的DefaultBeanDefinitionDocumentReader类对Document进行解析, 并使用BeanDefinitionParserDelegate对各种Element进行解析

Resource资源的加载

Resource.png

  1. Resource:资源的读取、判断是否可读、打开
public interface Resource extends InputStreamSource {

	/**
	 * 资源是否存在
	 */
	boolean exists();

	/**
	 * 资源是否可读
	 */
	boolean isReadable();

	/**
	 * 判断流是否可以重复读,为true表示不可以重复读,读取完成需要进行关闭
	 */
	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
	 * @since 2.5
	 */
	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;

	/**
	 * 获取资源长度
	 */
	long contentLength() throws IOException;

	/**
	 * 资源上次更新时间
	 */
	long lastModified() throws IOException;

	/**
	 * 根据当前路径获取资源
	 */
	Resource createRelative(String relativePath) throws IOException;

	/**
	 * 获取资源名称
	 */
	String getFilename();

	/**
	 * 获取资源描述信息
	 */
	String getDescription();

}

  1. WritableResource:对资源写入的支持
public interface WritableResource extends Resource {

	/**
	 * 资源是否可写
	 */
	boolean isWritable();

	/**
	 * 获取写入流
	 */
	OutputStream getOutputStream() throws IOException;

}
  1. ContextResource:获取容器中的资源
  2. AbstractResource:具体资源的抽象实现

bean的承载对象BeanDefinition

BeanDefinition.png

public static AbstractBeanDefinition createBeanDefinition(
        String parentName, String className, ClassLoader classLoader) throws ClassNotFoundException {

    GenericBeanDefinition bd = new GenericBeanDefinition();
    bd.setParentName(parentName);
    if (className != null) {
        if (classLoader != null) {
            bd.setBeanClass(ClassUtils.forName(className, classLoader));
        }
        else {
            bd.setBeanClassName(className);
        }
    }
    return bd;
}