spring BeanFactory VS FactoryBean
一、FactoryBean示例
public class DateStringFactoryBean implements FactoryBean<Object> {
private boolean isDate;
public void setDate(boolean date) {
isDate = date;
}
@Override
public Object getObject() {
return isDate ? new Date() : "2018-03-04";
}
@Override
public Class<?> getObjectType() {
return isDate ? Date.class : String.class;
}
@Override
public boolean isSingleton() {
return false;
}
}
AppConfig
public class AppConfig {
@Bean(name = "dateFactoryBean")
public DateStringFactoryBean createString(){
DateStringFactoryBean bean = new DateStringFactoryBean();
bean.setDate(true);
return bean;
}
@Bean(name = "stringFactoryBean")
public DateStringFactoryBean createDate(){
DateStringFactoryBean bean = new DateStringFactoryBean();
bean.setDate(false);
return bean;
}
}
Main
public class Main {
public static void main(String[] args) {
AbstractApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
System.out.println(context.getBean("dateFactoryBean", Date.class));
System.out.println(context.getBean("stringFactoryBean", String.class));
System.out.println(context.getBean("&dateFactoryBean")); //使用&+beanName获得DateStringFactoryBean实例
System.out.println(context.getBean("&stringFactoryBean"));
}
}
二、调试分析
1. 系统启动时,会注册FactoryBean

2. context.getBean("dateFactoryBean", Date.class) 时

三、相关文档
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:
- BeanNameAware's
setBeanName - BeanClassLoaderAware's
setBeanClassLoader - BeanFactoryAware's
setBeanFactory - EnvironmentAware's
setEnvironment - EmbeddedValueResolverAware's
setEmbeddedValueResolver - ResourceLoaderAware's
setResourceLoader(only applicable when running in an application context) - ApplicationEventPublisherAware's
setApplicationEventPublisher(only applicable when running in an application context) - MessageSourceAware's
setMessageSource(only applicable when running in an application context) - ApplicationContextAware's
setApplicationContext(only applicable when running in an application context) - ServletContextAware's
setServletContext(only applicable when running in a web application context) postProcessBeforeInitializationmethods of BeanPostProcessors- InitializingBean's
afterPropertiesSet - a custom init-method definition
postProcessAfterInitializationmethods of BeanPostProcessors
BeanFactory 是 IOC 容器的编程抽象,比如 ApplicationContext, XmlBeanFactory 等,这些都是 IOC 容器的具体表现
FactoryBean 是一个可以在 IOC而容器中被管理的一个 bean,是对各种处理过程和资源使用的抽象,FactoryBean 在需要
时产生另一个对象,而不返回 FactoryBean本身,我们可以把它看成是一个抽象工厂,对它的调用返回的是工厂生产的产
品。Spring对代理对象的处理,对事务性代理的处理都使用了FactoryBean
public interface FactoryBean<T>
BeanFactory which are themselves factories for individual objects.NB: A bean that implements this interface cannot be used as a normal bean. A FactoryBean is defined in a bean style,
but the object exposed for bean references (getObject()) is always the object that it creates.
FactoryBeans can support singletons and prototypes, and can either create objects lazily on demand or eagerly on startup.
The SmartFactoryBean interface allows for exposing more fine-grained behavioral metadata.
This interface is heavily used within the framework itself, for example for the AOP ProxyFactoryBean .
It can be used for custom components as well; however, this is only common for infrastructure code.
FactoryBean is a programmatic contract. Implementations are not supposed to rely on annotation-driven injection
or other reflective facilities. getObjectType() getObject() invocations may arrive early in the bootstrap process, even
ahead of any post-processor setup. If you need access other beans, implement BeanFactoryAware and obtain them programmatically.
Finally, FactoryBean objects participate in the containing BeanFactory's synchronization of bean creation. There is usually no
need for internal synchronization other than for purposes of lazy initialization within the FactoryBean itself (or the like).
同类文章
spring BeanFactory VS FactoryBean的更多相关文章
- Spring BeanFactory与FactoryBean的区别及其各自的详细介绍于用法
Spring BeanFactory与FactoryBean的区别及其各自的详细介绍于用法 1. BeanFactory BeanFactory,以Factory结尾,表示它是一个工厂类(接口),用于 ...
- Spring BeanFactory 与 FactoryBean 的区别
BeanFactory 和 FactoryBean 都是Spring Beans模块下的接口 BeanFactory是spring简单工厂模式的接口类,spring IOC特性核心类,提供从工厂类中获 ...
- BeanFactory 和FactoryBean的区别
转自:https://www.cnblogs.com/aspirant/p/9082858.html BeanFacotry是spring中比较原始的Factory.如XMLBeanFactory就是 ...
- Difference between BeanFactory and FactoryBean in Spring Framework (Spring BeanFactory与Factory区别)
参见原文:http://www.geekabyte.io/2014/11/difference-between-beanfactory-and.html geekAbyte Codes and Ran ...
- Spring中BeanFactory与FactoryBean的区别
在Spring中有BeanFactory和FactoryBean这2个接口,从名字来看很相似,比较容易搞混. 一.BeanFactory BeanFactory是一个接口,它是Spring中工厂的顶层 ...
- Spring的BeanFactory和FactoryBean
官方定义 BeanFactory:Spring Bean容器的根接口 FactoryBean:各个对象的工厂接口,如果bean实现了这个接口,它将被用作对象的工厂,而不是直接作为bean实例. 源码解 ...
- spring:Beanfactory和ApplicationContext、BeanFactory 和 FactoryBean
1.Beanfactory和ApplicationContext有什么区别 ApplicationContext (1)在配置文件加载后创建bean 利用debug方式,在Student类的无参构造方 ...
- Spring中的BeanFactory与FactoryBean看这一篇就够了
前言 理解FactoryBean是非常非常有必要的,因为在Spring中FactoryBean最为典型的一个应用就是用来创建AOP的代理对象,不仅如此,而且对理解Mybatis核心源码也非常有帮助!如 ...
- Spring中BeanFactory与FactoryBean到底有什么区别?
一.BeanFactory BeanFactory是一个接口,它是Spring中工厂的顶层规范,是SpringIoc容器的核心接口,它定义了getBean().containsBean()等管理Bea ...
随机推荐
- Unity应用架构设计(6)——设计动态数据集合ObservableList
什么是 『动态数据集合』 ?简而言之,就是当集合添加.删除项目或者重置时,能提供一种通知机制,告诉UI动态更新界面.有经验的程序员脑海里迸出的第一个词就是 ObservableCollection.没 ...
- 如何将自己写的verilog模块封装成IP核
如何将自己写的verilog模块封装成IP核 (2014-11-21 14:53:29) 转载▼ 标签: 财经 分类: 我的东东 =======================第一篇========= ...
- mysql表空间加密 keyring encryption
从5.7.11开始,mysql开始支持物理表空间的加密,它使用两层加密架构.包括:master key 和 tablespace key master key用于加密tablespace key,加密 ...
- 全局Threshold和动态阈值分割Dyn_Threshold的应用场景
手册里面的particle例子,例子的任务是分析颗粒在液体中.在这个应用程序的主要困难:存在两种类型的对象:大明亮物体和较低的小物体的对比.此外噪音使分割的存在困难:无法使用全局灰度阈值thresho ...
- 用WordPress建立专业网站教程 (一步步建站, 一步也不少)
最新美国域名中心US Domain Center: http://www.usdomaincenter.com/ 建站教程 (10分钟上线, 无需备案): https://www.qiyewp.com ...
- SAP PA Document List
RE-Real Estate 房地产 RE010-EN-Col95-Real Estate Processes in SAP ERP RE200-EN-Col95-Real Estate Manage ...
- 东南亚 SAP 实施 马来西亚税收在SAP的设计和实现
马来西亚属于中等收入国家,现行税种主要有: 公司所得税.个人所得税.不动产利得税.石油所得税.销售税.合同税.暴利税.服务税和关税等. (一)主要税种1.公司所得税 ⑴ 纳税人 公司所得税的纳税人分为 ...
- C++中常用的std标准容器
从c++11标准以来,c++中std定义的几种容器的效率非常高,优化的非常好,完全没有必要自己去定义类似的数据结构.了解使用它们,可以满足90%的日常编程需要.该篇文章基于c++11标准,从用户角度来 ...
- CustomDrawableTextView
public class CustomDrawableTextView extends TextView{ //image width.height private int imageWidth; p ...
- [原][openstack-pike][controller node][issue-2][glance] Could not parse rfc1738 URL from string 'mysql+pymysql=http://glance:glance@controller/glance'
问题点 在手动上传镜像的时候:出现错误 Could not parse rfc1738 URL from string 'mysql+pymysql=http://glance:glance@cont ...