• 2-1-1.spring源码--BeanFactory


    Spring源码–BeanFactory

    在spring官方api文档中,查看BeanFactory介绍中,第一句对于BeanFactory的表述非常贴切。BeanFactory作为一个根接口用于访问spring bean的容器(原文 :The root interface for accessing a Spring bean container.)。

    源码

    因为BeanFactory是一个接口,所以在整个源码中内容过多的实现内容,多数的实现够交给了实现类去完成,下面可以看下一下源码内容:

    public interface BeanFactory {
    
    	//Used to dereference a FactoryBean instance and distinguish it from beans created by the FactoryBean.
    	//用于间接用FactoryBean实例,区分这个bean是否由FactoryBean创建(点击FactoryBean详情)
    	String FACTORY_BEAN_PREFIX = "&";
    
    	//返回指定 bean 的一个实例,该实例可以是共享的,也可以是独立的。
    	Object getBean(String name) throws BeansException;
    	 T getBean(String name, Class requiredType) throws BeansException;
    	Object getBean(String name, Object... args) throws BeansException;
    	 T getBean(Class requiredType) throws BeansException;
    	 T getBean(Class requiredType, Object... args) throws BeansException;
    
    	//返回指定 Bean 的提供程序,允许对实例进行延迟按需检索,包括可用性和唯一性选项。
    	 ObjectProvider getBeanProvider(Class requiredType);
    	 ObjectProvider getBeanProvider(ResolvableType requiredType);
    
    	boolean containsBean(String name);
    
    	//是否是单例(详情见BeanDefinition)
    	boolean isSingleton(String name) throws NoSuchBeanDefinitionException;
    	//是否是原型(详情见BeanDefinition)
    	boolean isPrototype(String name) throws NoSuchBeanDefinitionException;
    
    	//类型是否匹配
    	boolean isTypeMatch(String name, ResolvableType typeToMatch) throws NoSuchBeanDefinitionException;
    	boolean isTypeMatch(String name, Class typeToMatch) throws NoSuchBeanDefinitionException;
    
    
    	@Nullable
    	Class getType(String name) throws NoSuchBeanDefinitionException;
    	@Nullable
    	Class getType(String name, boolean allowFactoryBeanInit) throws NoSuchBeanDefinitionException;
    
    	//如果有别名,返回别名
    	String[] getAliases(String name);
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38

    上面的源码涉及到spring 中BeanDefinition(核心)和FactoryBean,后面会介绍这两个内容,因为这两个类在bean的创建环节中占据一定的位置。

    上面有说道bean的创建涉及到一个常见spring面试题:bean创建过程是什么样子的?

    这个问题的答案,spring BeanFactory官方接口文献给出了答案:

    Bean factory implementations should support the standard bean lifecycle interfaces as far as possible. The full set of initialization methods and their standard order is:
    
    1. BeanNameAware's setBeanName
    2. BeanClassLoaderAware's setBeanClassLoader
    3. BeanFactoryAware's setBeanFactory
    4. EnvironmentAware's setEnvironment
    5. EmbeddedValueResolverAware's setEmbeddedValueResolver
    6. ResourceLoaderAware's setResourceLoader (only applicable when running in an application context)
    7. ApplicationEventPublisherAware's setApplicationEventPublisher (only applicable when running in an application context)
    8. MessageSourceAware's setMessageSource (only applicable when running in an application context)
    9. ApplicationContextAware's setApplicationContext (only applicable when running in an application context)
    10. ServletContextAware's setServletContext (only applicable when running in a web application context)
    
    //前10项可以统称为现实aware增强接口
    11. postProcessBeforeInitialization methods of BeanPostProcessors
    12. InitializingBean's afterPropertiesSet
    13. a custom init-method definition
    14. postProcessAfterInitialization methods of BeanPostProcessors
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
  • 相关阅读:
    MYSQL补充SQL语句
    rtl 开发必会技能
    Docker安装Bitbucket
    Docker的简介及安装
    常用DOS命令
    两行TextView前面添加一个图片/控件
    Mybatis plus 一对多关联查询分页不准确的问题
    Linux 学习笔记——make原理补充,进度条
    编译原理中的token简介
    ElementUI + vue 与 js 使用不同 + 外部引用 +loading区域加载()2022.9.17
  • 原文地址:https://blog.csdn.net/smd2575624555/article/details/126231038