Difference between BeanFactory and FactoryBean in Spring Framework (Spring BeanFactory与Factory区别)
参见原文:http://www.geekabyte.io/2014/11/difference-between-beanfactory-and.html
Codes and Rants | Javascript, Java and Scala
Saturday, November 15, 2014
Difference between BeanFactory and FactoryBean in Spring Framework
tl;dr A FactoryBean is an interface that you, as a developer, implements when writing factory classes and you want the object created by the factory to be managed as a bean by Spring, while a BeanFactory on the other hand, represents the Spring IoC container, it contains the managed beans and provides access to retrieving them. It is part of the core of the framework which implements the base functionality of an inversion of control container.
This post clarifies the difference between BeanFactory and FactoryBean in Spring Framework, and in the processes sheds some light on how the core component of Spring Frameworks stack together.
The Spring Framework has grown to be a lot of things, but at its core, it is a very versatile Dependency Injection container, upon which other things get built on. This core, this dependency injection container is actually built from 4 components.
1. The org.springframework.core package. In which, base functionalities that cut across the whole framework is implemented, e.g. exceptions, version detection etc.
2. The org.springframework.beans package. In which we have interfaces and classes for managing Java beans. The BeanFactory interface is found here.
3. The org.springframework.context Package. This builds on the beans package. An example of an interface out of this package that you would have used is the ApplicationContext (it implements the BeanFactory interface)
4. The org.springframework.expression package. This provides the expression language that can be used to traverse and manipulate the object graph within the container at runtime.
Diagrammatically this would look thus:

The places where the IoC container mechanism is implemented is mainly in Spring-Beans
package with supporting base functionality from Spring-Core and (Spring-Context builds on these two, extending it and adding additional functionalities certain). By the way, the fact that Spring-Context builds on Spring-Core and Spring-Beans is why only including Spring-Context as a dependency is all you need to get access to the Spring IoC container functionality in your application.
The functionality for creating, wiring and managing beans are packed into the org.springframework.beans. It is in this package we have BeanFactory and in a sub package (org.springframework.beans.factory) that you would find FactoryBean. The two interfaces that are the subject of this post.
The question then is, how does the BeanFactory and FactoryBean interfaces differ?
Apart from the fact that they share the same root package and are both anagrams (which make them confusing), they serve two totally different purposes.
A FactoryBean is an interface that you, as a developer, implements when writing factory classes and you want the object created by the factory class to be managed as a bean by Spring, while a BeanFactory on the other hand, represents the Spring IoC container, it contains the managed beans and provides access to retrieving them. It is part of the core of the framework which implements the base functionality of an inversion of control container.
In most cases, you won't find yourself using or implementing the BeanFactory interface directly, unless you are extending the core functionality of the framework. While you would do implement the FactoryBean when you have objects that are created by Factories that needs to be managed by Spring.
In more succinct words, the BeanFactory represents the Spring container while the FactoryBean represents factory classes whose created object are picked up and registered as a bean in the container.
To get more feel of these two, let us quickly take a look at two instances from the framework where they are put to use:
LocalContainerEntityManagerFactoryBean as an example of a FactoryBean
In configuring Hibernate JPA in Spring Framework, I mentioned the role of the org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean in the configuration process when creating a container managed EntityManagerFactory. It is the class that is wired up in the configuration. E.g:
<bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory"> <!-- the data source -->
<property name="dataSource" ref="dataSource"></property>
<!-- configuration that specifies the JPA Adapter to use -->
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
</bean>
</property> <!-- configuration to specify JPA behaviors -->
<property name="jpaProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">create-drop</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
</props>
</property>
</bean>
But if you look at the configuration, the class that is being wired up is of type LocalContainerEntityManagerFactoryBean, how come, it then produces a bean of type EntityManagerFactory?
That is because LocalContainerEntityManagerFactoryBean is a factory that creates and returns an object of type EntityManagerFactory. And because it implements FactoryBeaninterface, when configured, the Spring container knows that the registered bean won't be of type LocalContainerEntityManagerFactoryBean, but the type it returns in its getObject() method.
That is an example of what FactoryBean is used for.
ClassPathXmlApplicationContext as an example of BeanFactory.
The ClassPathXmlApplicationContext is one of the ways you can initiate Springs IoC container. You supply it with the configuration(s), it does the necessary object resolution and dependency injection and returns to you a container from which you can retrieve a fully resolved bean.
For example:
ApplicationContext context =
new ClassPathXmlApplicationContext("classpath:context-config.xml");
ClassPathXmlApplicationContext is of type ApplicationContext, and the ApplicationContext interface itself is found in the Spring-Context (refer to the diagram above).
The Spring-Context module part of the core component, contains classes and interfaces that provides enhanced functionality over the Spring-Beans and Spring-Core modules. The ApplicationContext interface is one of such convenience found in the Spring-Context module as it defines amongst various functionalities, the ability to retrieve beans from the container, ability to load file resources in a generic fashion, internationalization support etc...
But the ApplicatonContext interface is itself a type of BeanFactory as it extends the BeanFactory interface indirectly by extending the ListableBeanFactory and HierarchicalBeanFactory, as its signature shows:

Which means whenever you use any of the classes that implements the ApplicationContext(ClassPathXmlApplicationContext, FileSystemXmlApplicationContext, etc) you are accessing and using the Spring Container, through interfaces that are extension of the base BeanFactory interface.
Difference between BeanFactory and FactoryBean in Spring Framework (Spring BeanFactory与Factory区别)的更多相关文章
- Spring中的BeanFactory与FactoryBean看这一篇就够了
前言 理解FactoryBean是非常非常有必要的,因为在Spring中FactoryBean最为典型的一个应用就是用来创建AOP的代理对象,不仅如此,而且对理解Mybatis核心源码也非常有帮助!如 ...
- Spring Framework(框架)整体架构 变迁
Spring Framework(框架)整体架构 2018年04月24日 11:16:41 阅读数:1444 标签: Spring框架架构 更多 个人分类: Spring框架 版权声明:本文为博主 ...
- 1.Spring Framework 5.0 入门篇
1.为什么学习Spring? 随着对Java EE的不断接触和理解,你会发现Spring 在各个企业和项目中发挥着越来越重要的作用.掌握Spring 已成为我们IT行业生存必学的本领之一. Spri ...
- Benefits of Using the Spring Framework Dependency Injection 依赖注入 控制反转
小结: 1. Dependency Injection is merely one concrete example of Inversion of Control. 依赖注入是仅仅是控制反转的一个具 ...
- Spring学习笔记——Spring中的BeanFactory与FactoryBean
BeanFactory BeanFactory是Spring的org.springframework.beans.factory下的一个接口,是Spring IOC所遵守的基本编程规范.他的实现类有D ...
- Spring Framework 组件注册 之 FactoryBean
Spring Framework 组件注册 之 FactoryBean 前言 前两篇文章介绍了如何使用@Component,@Import注解来向spring容器中注册组件(javaBean),本文将 ...
- [spring源码学习]六、IOC源码-BeanFactory和factory-bean
一.代码实例 在我们分析spring的IOC源码的时候,发现除了配置标准的bean,并且通过getBean(beanName)的方法获取到一个bean的实例外,似乎还有这不少其他获取的方法,例如在第四 ...
- Spring BeanFactory与FactoryBean的区别及其各自的详细介绍于用法
Spring BeanFactory与FactoryBean的区别及其各自的详细介绍于用法 1. BeanFactory BeanFactory,以Factory结尾,表示它是一个工厂类(接口),用于 ...
- 理解spring中的BeanFactory和FactoryBean的区别与联系
原文地址:http://blog.csdn.net/joenqc/article/details/66479154 首先,这俩都是个接口… 实现 BeanFactory 接口的类表明此类事一个工厂,作 ...
随机推荐
- Maven(五)Eclipse配置Maven插件
较新版本的Eclipse内置Maven插件 1. 设置installation 一般不适用内置的 指定核心程序的位置 2. 设置user settings 指定本地仓库的位置 1.如果setting. ...
- Linux 系统的安装 (最全收集)
在几年前,我曾经多次萌生抛弃Win系统,从而使用Linux系统-----(Ubuntu),但是我每次都会遇到同一个问题,TM怎么安装啊. 不是安装奇慢就是不知道安装的方法. 怎样安装Ubuntu操作系 ...
- 用Python实现Zabbix-API 监控
做运维的朋友应该知道,公司IDC机房经常有上架.下架.报修和报废的服务器.如果服务器数量很多的时候很容易造成监控遗漏. 大的互联网公司把监控系统和CMDB(资产管理系统|配置管理数据库系统 ...
- JavaScript 基础(一) - JavaScript的引入方式,JavaScript 变量命名规则,JS 的五种基本数据类型,ECMAScript 算数运算符,逻辑运算符
JavaScript的引入方式 直接编写 <!DOCTYPE html> <html lang="en"> <head> <meta ch ...
- Java 去除字符串中的空白字符
通过String的trim()方法只能去掉字符串两端的空格字符,但是对于\t \n等其它空白字符确不能去掉,因此需通过正则表达式,将其中匹配到的空白字符去掉,代码如下: protected Strin ...
- Linux 安装 jdk8
切换目录 cd /usr 创建目录 mkdir java cd java 下载 jdk rz 或者 ftp 都行,只要能成功上传 解压 tar zxvf jdk-8u181-linux-x64.tar ...
- mysql数据库表操作-表的主键索引和普通索引
数据库索引就象书的目录一样,如果在字段上建立了索引,那么以索引列为查询条件时可以加快查询数据的速度.查询数据库,按主键查询是最快的,每个表只能有一个主键列,但是可以有多个普通索引列,主键列要求列的所有 ...
- Html5 和 CSS的简单应用
本文是利用几个简单的小例子,来实现html+css的简单应用. 菱形链接菜单 本例是采用html5+css3.0设置的菜单链接.其中主要用到了以下几个方面: CSS3.0中的2D变换,如:旋转tran ...
- Jmeter接口测试问题及解决方法积累
一.各个类型参数的位置 plain body query parameter URI 路径上 二.特殊字符处理 \"作为参数输入到接口,会出现错误 响应断言中如果有用到正则表达式,不能包含正 ...
- Android--手势及触摸事件的注意点(一)
实现onInterceptTouchEvent方法可以用来拦截父ViewGroup传递下来的所有触屏事件,可以将所有触屏事件交由此ViewGroup自身的onTouchEvent来处理,也可以继续传递 ...