BeanFactory是Spring实现依赖注入的核心接口.提供应用的统一配置注册功能,实现业务开发解偶.使用getBean可以代替单例,原型设计模式.

顶重要的BeanFactory里注释写得太好了.所以咱们先翻译下注释,后面再详细分析.

重点直接看红色标注吧.

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容器的基本客户端视图;  进一步的接口,如{@link ListableBeanFactory}和  {@link org.springframework.beans.factory.config.ConfigurableBeanFactory}  可用于特殊目的。

This interface is implemented by objects that hold a number of bean definitions,
each uniquely identified by a String name. Depending on the bean definition,
the factory will return either an independent instance of a contained object
(the Prototype design pattern), or a single shared instance (a superior
alternative to the Singleton design pattern, in which the instance is a
singleton in the scope of the factory). Which type of instance will be returned
depends on the bean factory configuration: the API is the same. Since Spring
2.0, further scopes are available depending on the concrete application
context (e.g. "request" and "session" scopes in a web environment).

此接口由持有一些bean定义的对象来实现,每个bean由String字符串唯一标识。根据bean定义,  工厂将返回一个独立对象实例(原型设计模式),或者一个单个共享实例(Singleton设计模式的优雅代替实现,其中该实例是一个factory范围内的单例)。实例的哪种类型将被返回依赖于bean工厂配置:即使API是一样的。从Spring2.0开始,作用域扩展到根据具体的应用上下文,如web环境的request,session。

The point of this approach is that the BeanFactory is a central registry
of application components, and centralizes configuration of application
components (no more do individual objects need to read properties files,
for example). See chapters 4 and 11 of "Expert One-on-One J2EE Design and
Development" for a discussion of the benefits of this approach.

这种方案的关键是,BeanFactory的是应用程序组件注册的中心,同时集中应用程序组件的配置(程序模块不再需要读取诸如properties的配置文件)。这种设计的更多好处讨论详见的<J2EE设计开发编程指南>第4和第11章.

强烈推荐看这本书,就是国内不好买了.

Note that it is generally better to rely on Dependency Injection
("push" configuration) to configure application objects through setters
or constructors, rather than use any form of "pull" configuration like a
BeanFactory lookup. Spring's Dependency Injection functionality is
implemented using this BeanFactory interface and its subinterfaces.

相比诸如 BeanFactory 中查找的pull配置方式,通过setters或者构造方法,依赖注入的方式配置应用对象更好.Spring的依赖注入功能就是通过实现BeanFactory和其子接口实现的.

Normally a BeanFactory will load bean definitions stored in a configuration
source (such as an XML document), and use the {@code org.springframework.beans}
package to configure the beans. However, an implementation could simply return
Java objects it creates as necessary directly in Java code. There are no
constraints on how the definitions could be stored: LDAP, RDBMS, XML,
properties file, etc. Implementations are encouraged to support references
amongst beans (Dependency Injection).

通常,一个BeanFactory会从配置源(如X​​ML文件)中加载bena 定义,并使用{@code org.springframework.beans}包解析bean。然而,实现可以简单地返回Java代码直接新建的Java对象。这里没有限制bean 定义文件的格式:LDAP,RDBMS,XML.实现类欢迎支持应用而非bean(依赖注入)

In contrast to the methods in {@link ListableBeanFactory}, all of the
operations in this interface will also check parent factories if this is a
{@link HierarchicalBeanFactory}. If a bean is not found in this factory instance,
the immediate parent factory will be asked. Beans in this factory instance
are supposed to override beans of the same name in any parent factory.

对比{@link ListableBeanFactory}中的方法,如果这是一个{@link HierarchicalBeanFactory},这个接口的全部实现都会查找父工厂.如果在这个工厂实例找不到bean,去直接父工厂查找。factory实例中的bean会覆盖父factory实例中的同名bean。

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:
bean factory 实现类应该尽量支持标准bean的生命周期接口.全套的初始化方法,已经排序如下

感觉这坨概念得好好理理
1. BeanNameAware's {@code setBeanName}
2. BeanClassLoaderAware's {@code setBeanClassLoader}
3. BeanFactoryAware's {@code setBeanFactory}
4. ResourceLoaderAware's {@code setResourceLoader}
(only applicable when running in an application context)
5. ApplicationEventPublisherAware's {@code setApplicationEventPublisher}
(only applicable when running in an application context)
6. MessageSourceAware's {@code setMessageSource}
(only applicable when running in an application context)
7. ApplicationContextAware's {@code setApplicationContext}
(only applicable when running in an application context)
8. ServletContextAware's {@code setServletContext}
(only applicable when running in a web application context)
9. {@code postProcessBeforeInitialization} methods of BeanPostProcessors
10. InitializingBean's {@code afterPropertiesSet}
11. a custom init-method definition
12. {@code postProcessAfterInitialization} methods of BeanPostProcessors

On shutdown of a bean factory, the following lifecycle methods apply:
1. DisposableBean's {@code destroy}
2. a custom destroy-method definition

 package org.springframework.beans.factory;
public interface BeanFactory { /**
* 用于区分是否直接获取FactoryBean实例.
* bean以&开头表示获取FactoryBean实例.否则获取created的实例.For example, if the bean named
* {@code myJndiObject} is a FactoryBean, getting {@code &myJndiObject}
* will return the factory, not the instance returned by the factory.
*/
String FACTORY_BEAN_PREFIX = "&"; /**
* 返回一个原型或者单例实例.
* 抢单例,原型设计模式的饭碗
* 可以根据别名查找,也可以去父容器实例查找
*/
Object getBean(String name) throws BeansException; /**
* 加个类型
*/
<T> T getBean(String name, Class<T> requiredType) throws BeansException; /**
* 根据类型获取bean实例.可以是接口或子类,但不能是{@code null}.
* {@link ListableBeanFactory}也可以使用类型转化为name进行查找.更多bean集合的操作可以看
* ListableBeanFactory和BeanFactoryUtils
*/
<T> T getBean(Class<T> requiredType) throws BeansException; /**
* 多了构造方法,工厂方法的参数
*/
Object getBean(String name, Object... args) throws BeansException; /**
* 判断是否包含bean(包括别名,父容器)
* 陷阱出现:这边不管类是否抽象类,懒加载,是否在容器范围内,只要符合都返回true,所以这边true,不一定能从getBean获取实例
*/
boolean containsBean(String name); /**
* 是否单例
*/
boolean isSingleton(String name) throws NoSuchBeanDefinitionException; /**
* 是否原型
*/
boolean isPrototype(String name) throws NoSuchBeanDefinitionException; /**
* 是否有跟name匹配类型的bean
*/
boolean isTypeMatch(String name, Class<?> targetType) throws NoSuchBeanDefinitionException; /**
* 根据bean name获取类型
*/
Class<?> getType(String name) throws NoSuchBeanDefinitionException; /**
* 获取别名
*/
String[] getAliases(String name); }

Spring源码解析 - BeanFactory的更多相关文章

  1. Spring源码解析 - BeanFactory接口体系解读

    不知道为什么看着Spring的源码,感触最深的是Spring对概念的抽象,所以我就先学接口了. BeanFactory是Spring IOC实现的基础,这边定义了一系列的接口,我们通过这些接口的学习, ...

  2. Spring源码解析 - AbstractBeanFactory 实现接口与父类分析

    我们先来看类图吧: 除了BeanFactory这一支的接口,AbstractBeanFactory主要实现了AliasRegistry和SingletonBeanRegistry接口. 这边主要提供了 ...

  3. Spring源码分析——BeanFactory体系之抽象类、类分析(二)

    上一篇分析了BeanFactory体系的2个类,SimpleAliasRegistry和DefaultSingletonBeanRegistry——Spring源码分析——BeanFactory体系之 ...

  4. Spring源码分析——BeanFactory体系之抽象类、类分析(一)

    上一篇介绍了BeanFactory体系的所有接口——Spring源码分析——BeanFactory体系之接口详细分析,本篇就接着介绍BeanFactory体系的抽象类和接口. 一.BeanFactor ...

  5. spring 源码解析

    1. [文件] spring源码.txt ~ 15B     下载(167) ? 1 springн┤┬вио╬Ш: 2. [文件] spring源码分析之AOP.txt ~ 15KB     下载( ...

  6. Spring源码解析-ioc容器的设计

    Spring源码解析-ioc容器的设计 1 IoC容器系列的设计:BeanFactory和ApplicatioContext 在Spring容器中,主要分为两个主要的容器系列,一个是实现BeanFac ...

  7. Spring源码解析系列汇总

    相信我,你会收藏这篇文章的 本篇文章是这段时间撸出来的Spring源码解析系列文章的汇总,总共包含以下专题.喜欢的同学可以收藏起来以备不时之需 SpringIOC源码解析(上) 本篇文章搭建了IOC源 ...

  8. Spring源码解析之BeanFactoryPostProcessor(三)

    在上一章中笔者介绍了refresh()的<1>处是如何获取beanFactory对象,下面我们要来学习refresh()方法的<2>处是如何调用invokeBeanFactor ...

  9. Spring源码解析之八finishBeanFactoryInitialization方法即初始化单例bean

    Spring源码解析之八finishBeanFactoryInitialization方法即初始化单例bean 七千字长文深刻解读,Spirng中是如何初始化单例bean的,和面试中最常问的Sprin ...

随机推荐

  1. CentOS7 RPM安装 rabbitmqDownloads on Bintray

    下载 0依赖Erlang RPM for RabbitMQ包(https://github.com/rabbitmq/erlang-rpm) https://dl.bintray.com/rabbit ...

  2. opencv中读取显示图像

    opencv是个开源的图像处理的库,小到基本的图像处理函数,如图像移动放大缩小,大到人脸识别,部分机器学习的知识,所以是个学习的不错的库.之前有图像处理的知识,这次再学习下这个开源库. 先上基础的图像 ...

  3. 真实赛车3,FERRARI之魂不买FERRARI 599 GTO可以解锁顶点系列。

    难点1,在仅有458 SPIDER的情况下,“TURBO BURST技巧混战”中 Mount Panorama速度快照,比较难.多重试十几次. 难点2,“TURBO BURST大满贯”中直道赛,用45 ...

  4. unity里面的gameobject和transform的关系

    一切都是物体(gameobject),而transform是物体的一个基本属性类,包含位置,旋转,缩放,三个基本属性,两者之间可以互相转换 查找物体,建议用transform,GameObject无法 ...

  5. 5月23日笔记-js绑定事件、解绑事件、复合事件

    each() $("p").each(function(i,ele){ //alert(ele.innerHTML); alert($("p:eq("+i+&q ...

  6. 【转载】Python ConfigParser的使用

    1.基本的读取配置文件-read(filename) 直接读取ini文件内容-sections() 得到所有的section,并以列表的形式返回-options(section) 得到该section ...

  7. M3截止阶段小结

    python知识点总结1.copy模块中深浅拷贝copy() deepcopy()2.__new__ 方法参数    def __new__(cls, *args, **kwargs):        ...

  8. 客户端调用wcf服务,如何提高调用性能

    IO调用服务 1.使用using(每次自动释放) ; i < ; i++) { var watch = new Stopwatch(); watch.Start(); using (var cl ...

  9. win10开启开发人员模式

    工具: win10 方法如下: 1.在Windows10系统桌面,点击开始菜单,然后在弹出窗口中选择“设置”菜单项 2.在打开的设置窗口中,选择“更新和安全”图标,并点击打开更新和安全窗口 3.在打开 ...

  10. canvas设置repeat

    canvas设置repeat 方法 ctx.createPattern(img, 'repeat'); repeat repeat-x repeat-y no-repeat 重复图片 const ca ...