探索SpringBoot-一块儿看看Spring核心源码之BeanFactory(七)

前文回顾

上篇讲解了探索SpringBoot-一块儿来看看Spring容器加载核心源码(六),讲解到要探索obtainFreshBeanFactory()函数,可是不了解Spring容器的设计理念是没有办法来理解obtainFreshBeanFactory()函数的,因此今天来看看Spring容器最最基本的接口设计BeanFactoryjava

ClassPathXmlApplicationContext引出的类继承关系

接着咱们的探索SpringBoot-一块儿来看看Spring容器加载核心源码(六)中的ClassPathXmlApplicationContext,咱们打开Idea而且打开这个源代码文件,而且右键依次点击Diagrams->show Diagrams,最后咱们能够获得一张很是神奇的图。(我感受不少人都不知道idea可以作到这个事情,不知道的举手,hahah)spring

从上图中,咱们能够清晰地看到最中心的接口是ApplicationContext,最底层的接口是BeanFacotry今天暂时先无论ApplicationContext,由于咱们必须先理解了BeanFactory以后,才能理解ApplicationContext编程

BeanFactory

而后,咱们打开BeanFactory,翻到源代码对BeanFactory的注释。bash

* The root interface for accessing a Spring bean container.
 * This is the basic client view of a bean container;
 * further interfaces such as {@link ListableBeanFactory} and
 * {@link org.springframework.beans.factory.config.ConfigurableBeanFactory}
 * are available for specific purposes.
复制代码

大意为可以访问Spring bean 容器的根接口。这个是对于bean容器来讲最基本的客户视图。像其余ConfigurableBeanFactory和ListableBeanFactory都是为了特定的功能提供的ide

在《Spring技术内幕》中也这样解释到函数

BeanFactory提供的是最基本的IoC容器的功能,BeanFactory接口定义了IoC容器最基本的形式,而且提供了IoC容器所应该遵照的最基本的服务契约,也是咱们使用IoC容器所应遵照的最底层和最基本的编程规范,这些接口定义勾画了Ioc的基本轮廓。post

让咱们来看看BeanFactory的接口定义是什么样子的?ui

public interface BeanFactory {

	/** * Used to dereference a {@link FactoryBean} instance and distinguish it from * beans <i>created</i> by the FactoryBean. For example, if the bean named * <code>myJndiObject</code> is a FactoryBean, getting <code>&myJndiObject</code> * will return the factory, not the instance returned by the factory. */
	String FACTORY_BEAN_PREFIX = "&";
	
        //1.从容器中获取指定的Bean
	Object getBean(String name) throws BeansException;

	Object getBean(String name, Class requiredType) throws BeansException;

	Object getBean(String name, Object[] args) throws BeansException;
        //2.判断容器是否包含了指定的Bean
	boolean containsBean(String name);
        //3.判断指定的Bean是不是Singleton的
	boolean isSingleton(String name) throws NoSuchBeanDefinitionException;
        //4.判断指定的Bean是不是Prototype的
	boolean isPrototype(String name) throws NoSuchBeanDefinitionException;
        //5.判断指定的Bean的Class类型是不是特定的Class类型
	boolean isTypeMatch(String name, Class targetType) throws NoSuchBeanDefinitionException;
        //6.获取指定名字Bean的Class类型
	Class getType(String name) throws NoSuchBeanDefinitionException;
        //7.查询指定Bean的全部的别名
	String[] getAliases(String name);

}
复制代码

从详细的注释中能够看到总共定义了7个方法,这些方法勾画出IoC容器的基本特性。用大白话讲就是容器起码得有这几个方法才能算是一个基本的,合格的容器。idea

在这个基础上,Spring还提供了一系列符合Ioc的容器工开发人员使用。其中DefaultListableBeanFactorySpring提供的一个实现Ioc容器的最最基本的实现类。依照惯例,咱们也来看看DefaultListableBeanFactory的继承类图。spa

经过上图,咱们能够看到DefaultListableBeanFactory一方面是实现了左侧以BeanFactory为根的接口,右侧是实现BeanDefinitionRegistry接口。打开该类的源代码,咱们也能够看到这么一段话。

/**
 * Default implementation of the
 * {@link org.springframework.beans.factory.ListableBeanFactory} and
 * {@link BeanDefinitionRegistry} interfaces: a full-fledged bean factory
 * based on bean definition objects.
 *
复制代码

大意是这是一个实现了ListableBeanFactoryBeanDefinitionRegistry的完整的bean定义的对象。理解就是这是一个最基本的,可是也是可用的BeanFacotry实现

那么具体是怎么实现的呢?且听下回分解。

关于写做

之后这里天天都会写一篇文章,题材不限,内容不限,字数不限。尽可能把本身天天的思考都放入其中。

若是这篇文章给你带来了帮助,能请你写下是哪一个部分吗?有效的反馈是对我最大的帮助。

我是shane。今天是2019年8月12日。百天写做计划的第十九天,19/100。

相关文章
相关标签/搜索