一、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. spring mvc 传入中文参数乱码问题解决

    一个简单的学习springmvc的demo中,当http请求传入中文参数时,在controller中接受到的参数就已经是乱码了,经百度一番解决方案如下: 1. get请求方式乱码解决 对于get方式, ...

  2. easyUI的汇总列,在前端生成

    1.easyUI初始化,启用汇总列,showFooter:true 2.后台json有默认的footer的值 {"total":28,"rows":[ {&qu ...

  3. Activiti 工作流变量的修改方法

    修改方法: //此方法的更新会同时更新 ru_ 和 hi_的数据 @Override public Map<String, Object> updateProcessFormValue(S ...

  4. Maven知识总结(转)

    原文地址:http://blog.csdn.net/caihaijiang/article/details/6664910 1.Maven内置变量说明: ${basedir} 项目根目录 ${proj ...

  5. JAVA中有一个特殊的类: Object。它是JAVA体系中所有类的父类(直接父类或者间接父类)。

    接口往往被我们定义成一类XX的东西. 接口实际上是定义一个规范.标准.   ①  通过接口可以实现不同层次.不同体系对象的共同属性: 通过接口实现write once as anywhere. 以JA ...

  6. Linux 文件普通权限_011

    一.文件权限10个字符对应类型和权限 二.Linux普通文件和Linux目录读.写.执行权限说明 标注:Linux 中的文件名是存在于父目录的block里面,并指向这个文件的inode节点 1.lin ...

  7. android studio设置imageview显示图片

    拖动imageview 选择图片 .png 代码引用: private Imageview pay; pay = (ImageView)findViewById(R.id.imageView2); p ...

  8. 从头配置,开发 cocos2dx 3.14.1

    要使用cocos2dx写一个麻将测试工具. 配置流程如下: 1. 下载安装最新版本的VS(VS2017): https://www.visualstudio.com/zh-hans/downloads ...

  9. 公开的免费WebService接口分享

    天气预报Web服务,数据来源于中国气象局 Endpoint  Disco  WSDL IP地址来源搜索 WEB 服务(是目前最完整的IP地址数据) Endpoint  Disco  WSDL 随机英文 ...

  10. linux shell的here document用法(cat << EOF)

    什么是Here Document?Here Document 是在Linux Shell 中的一种特殊的重定向方式,它的基本的形式如下cmd << delimiter  Here Docu ...