一、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. 我的2018:OCR、实习和秋招

    真的是光阴似箭,好像昨天还沉浸在考研成功的喜悦,今天却要即将步入2019年,即将硕士毕业.老规矩,还是在每一年的最后一天总结今年以及展望明年.回首2018,经历的东西特别多,视野也开阔了不少,可以说, ...

  2. 记录一下idea自动生成Entity

    最近在鼓捣spring -boot ,真好用,学习到jpa. 通过生成Entity 文件,能够快速的生成数据库,并且使用 JpaRepository 的基本增删查改 方法,好用的一批. 可是随之,问题 ...

  3. prometheus杂碎

    一个监控及告警的系统,内含一个TSDB(时序数据库).在我而言是一个数采程序 重要成员分三块 exploter:实际是外部接口,让各个程序实现这个接口,供普罗米修斯定时从此接口中取数 alert:告警 ...

  4. HTTP 04 web 服务器

    用单台虚拟主机实现多个域名 HTTP/1.1 允许一台 HTTP 服务器搭建多个 web 站点, 例如提供 web 托管服务的供应商, 可以用一台服务器为多位客户服务, 也可以以每位客户持有的域名运行 ...

  5. C语言 结构体(联合体)对齐规则

    /* 结构体(联合体)对齐规则 */ #include <stdio.h> #include <stdlib.h> #include <string.h> /* * ...

  6. HBuilder打包React单页面,Android返回功能

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...

  7. MongoDB学习总结(二)

    前言:学习札记! MongoDB学习总结(二) 1.  安装.初识 之前写过一篇MongoDB的快速上手文章,里边详细的讲了如何安装.启动MongoDB,这里就不再累述安装过程,简单介绍一下Mongo ...

  8. 2单一职责原则SRP

    一.什么是单一职责原则 单一职责原则(Single Responsibility Principle ): 就一个类而言,应该仅有一个引起它变化的 原因. 二.多功能的山寨手机 山寨手机的功能: 拍照 ...

  9. LeetCode - Subarray sum equals k

    Given an array of integers and an integer k, you need to find the total number of continuous subarra ...

  10. ToolBar样式颜色,图标设置

    extends:http://blog.csdn.net/w1054993544/article/details/48339565 <resources> <style name=& ...