一、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:

  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)
  11. postProcessBeforeInitialization methods of BeanPostProcessors
  12. InitializingBean's afterPropertiesSet
  13. a custom init-method definition
  14. postProcessAfterInitialization methods of BeanPostProcessors

BeanFactory 是 IOC 容器的编程抽象,比如 ApplicationContext, XmlBeanFactory 等,这些都是 IOC 容器的具体表现

FactoryBean 是一个可以在 IOC而容器中被管理的一个 bean,是对各种处理过程和资源使用的抽象,FactoryBean 在需要

时产生另一个对象,而不返回 FactoryBean本身,我们可以把它看成是一个抽象工厂,对它的调用返回的是工厂生产的产

品。Spring对代理对象的处理,对事务性代理的处理都使用了FactoryBean

public interface FactoryBean<T>
Interface to be implemented by objects used within a 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.

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 aop对象生成

spring BeanFactory VS FactoryBean的更多相关文章

  1. Spring BeanFactory与FactoryBean的区别及其各自的详细介绍于用法

    Spring BeanFactory与FactoryBean的区别及其各自的详细介绍于用法 1. BeanFactory BeanFactory,以Factory结尾,表示它是一个工厂类(接口),用于 ...

  2. Spring BeanFactory 与 FactoryBean 的区别

    BeanFactory 和 FactoryBean 都是Spring Beans模块下的接口 BeanFactory是spring简单工厂模式的接口类,spring IOC特性核心类,提供从工厂类中获 ...

  3. BeanFactory 和FactoryBean的区别

    转自:https://www.cnblogs.com/aspirant/p/9082858.html BeanFacotry是spring中比较原始的Factory.如XMLBeanFactory就是 ...

  4. 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 ...

  5. Spring中BeanFactory与FactoryBean的区别

    在Spring中有BeanFactory和FactoryBean这2个接口,从名字来看很相似,比较容易搞混. 一.BeanFactory BeanFactory是一个接口,它是Spring中工厂的顶层 ...

  6. Spring的BeanFactory和FactoryBean

    官方定义 BeanFactory:Spring Bean容器的根接口 FactoryBean:各个对象的工厂接口,如果bean实现了这个接口,它将被用作对象的工厂,而不是直接作为bean实例. 源码解 ...

  7. spring:Beanfactory和ApplicationContext、BeanFactory 和 FactoryBean

    1.Beanfactory和ApplicationContext有什么区别 ApplicationContext (1)在配置文件加载后创建bean 利用debug方式,在Student类的无参构造方 ...

  8. Spring中的BeanFactory与FactoryBean看这一篇就够了

    前言 理解FactoryBean是非常非常有必要的,因为在Spring中FactoryBean最为典型的一个应用就是用来创建AOP的代理对象,不仅如此,而且对理解Mybatis核心源码也非常有帮助!如 ...

  9. Spring中BeanFactory与FactoryBean到底有什么区别?

    一.BeanFactory BeanFactory是一个接口,它是Spring中工厂的顶层规范,是SpringIoc容器的核心接口,它定义了getBean().containsBean()等管理Bea ...

随机推荐

  1. FILESTREAM feature can't be enabled if you use cluster shared volumes

    Create a SQL Cluster instance. Create Cluster Shared Volume Please note. No Share storage is added i ...

  2. Convolution卷积算法python以numpy,Matplotlib实现

    1:简述 Numpy拥有函数numpy.convolve(a, v, mode='full')[source]¶,通过该函数完成卷积算法并图形化(Matplotlib)实现. 2:卷积定理 原理: 设 ...

  3. 隐马尔可夫模型:HMM

    隐马尔可夫模型求解三大问题实例剖析 HMM 模型如图所示: 一.隐马尔可夫模型定义 隐马尔可夫模型由初始概率分布.状态转移概率分布以及观测概率分布确定. 设 Q(图中的q)是所有可能的状态的集合,V( ...

  4. vscode: Visual Studio Code 常用快捷键1

    ctrl + pAlt + shift 多行编辑

  5. koa文档参考

    koa文档 简介 koa 是由 Express 原班人马打造的,致力于成为一个更小.更富有表现力.更健壮的 Web 框架.使用 koa 编写 web 应用,通过组合不同的 generator,可以免除 ...

  6. VS2017 调试 Unity3D 脚本

    1. 安装Unity3D最新版本.   https://unity3d.com/cn/get-unity/download 2. 安装Visual Studio Community 2017. htt ...

  7. 一、K3 WISE 实施顾问教程《进度1-谈谈实施顾问》

    1.为什么要开这门课? 从自身的原因说起,在我从开发顾问转岗做实施顾问.售后服务顾问时,我就定下了我做顾问的目标. 第一个核心目标,帮助成千上万的企业客户促进他们商业的成功!第二个目标,成为最顶级的顾 ...

  8. C# 如何通过mailto标签和SMTP协议两种方式发送邮件

    本文主要讲解如何通过如现mailto标签和SMTP协议两种方式发送邮件,下面就直入主题 方法一.通过mailto标签发送邮件 通过mailto不是正真意义上的发送邮件,它只是会自动调用我们本地默认的邮 ...

  9. [原]获取openstack-pike安装包

    linux version: CentOS 7.5.1804 #进入仓库配置目录 cd /etc/yum.repo.d/ #批量重命名所有文件 for files in `ls *`; do mv $ ...

  10. linux signal

    1) SIGHUP 本信号在用户终端连接(正常或非正常)结束时发出, 通常是在终端的控制进程结束时, 通知同一session内的各个作业, 这时它们与控制终端不再关联. 登录Linux时,系统会分配给 ...