BeanFactory 和 FactoryBean 都是Spring Beans模塊下的接口
BeanFactory是spring簡單工廠模式的接口類,spring IOC特性核心類,提供從工廠類中獲取bean的各種方法,是所有bean的容器。見以下結構視圖

FactoryBean仍然是一個bean,但不同於普通bean,它的實現類最終也需要注冊到BeanFactory中。它也是一種簡單工廠模式的接口類,但是生產的是單一類型的對象,與BeanFactory生產多種類型對象不同。
FactoryBean是一個接口,實現了這個接口的類,在注冊到spring BeanFactory后,並不像其它類注冊后暴露的是自己,它暴露的是FactoryBean
中getObject方法的返回值。這一點可在FactoryBean的類注釋上找到證據。
/** * Interface to be implemented by objects used within a {@link BeanFactory} which * are themselves factories for individual objects. If a bean implements this * interface, it is used as a factory for an object to expose, not directly as a * bean instance that will be exposed itself. * * <p><b>NB: A bean that implements this interface cannot be used as a normal bean.</b> * A FactoryBean is defined in a bean style, but the object exposed for bean * references ({@link #getObject()}) is always the object that it creates.
比如:在spring配置文件中配置了名字 myFcBean 的一個類型,該類型是 FactoryBean 的實現類。那么通過
BeanFactory.getBean(“myFcBean”) 返回的並不是這個類型本身的對象,而是調用這個對象的getObject方法的返回值。
FactoryBean的結構見下圖

轉載自:https://baijiahao.baidu.com/s?id=1623501566419189221&wfr=spider&for=pc