原文地址:http://blog.csdn.net/joenqc/article/details/66479154

首先,这俩都是个接口…

实现 BeanFactory 接口的类表明此类事一个工厂,作用就是配置、新建、管理 各种Bean。

而 实现 FactoryBean 的类表明此类也是一个Bean,类型为工厂Bean(Spring中共有两种bean,一种为普通bean,另一种则为工厂bean)。顾名思义,它也是用来管理Bean的,而它本身由spring管理。

一个Bean想要实现 FactoryBean ,必须实现以下三个接口:

1. Object getObject():返回由FactoryBean创建的Bean的实例

2. boolean isSingleton():确定由FactoryBean创建的Bean的作用域是singleton还是prototype;

3. getObjectType():返回FactoryBean创建的Bean的类型。
  • 1
  • 2
  • 3
  • 4
  • 5

有一点需要注意,如果将一个实现了FactoryBean的类成功配置到了spring上下文中,那么通过该类对象的名称(比如appleFactoryBean)从spring的applicationContext或者beanFactory获取bean时,获取到的是appleFactoryBean创建的apple实例,而不是appleFactoryBean自己,如果想通过spring拿到appleFactoryBean,需要在名称前加 & 符号 :

out.println(applicationContext.getBean("&appleFactoryBean"))
  • 1

这个prefix在BeanFactory接口源码中有提到:

/**
* Used to dereference a {@link FactoryBean} instance and distinguish it from
* beans <i>created</i> by the FactoryBean. For example, if the bean named
* {@code myJndiObject} is a FactoryBean, getting {@code &myJndiObject}
* will return the factory, not the instance returned by the factory.
*/
String FACTORY_BEAN_PREFIX = "&";
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

还有一点需要注意,FactoryBean管理的bean实际上也是由spring进行配置、实例化、管理,因此由FactoryBean管理的bean不能再次配置到spring配置文件中(xml、java类配置、注解均不可以),否则会报如下异常:

Exception in thread "main" org.springframework.beans.factory.BeanIsNotAFactoryException: Bean named 'appleFactoryBean' is expected to be of type 'org.springframework.beans.factory.FactoryBean' but was actually of type 'java.lang.Object'
at org.springframework.beans.factory.support.AbstractBeanFactory.getObjectForBeanInstance(AbstractBeanFactory.java:1612)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:317)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:742)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:866)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542)
at org.springframework.context.annotation.AnnotationConfigApplicationContext.<init>(AnnotationConfigApplicationContext.java:84)
at com.joen.testspringcontainer.Start.main(Start.java:11)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

附上一个例子:

spring配置类:

@Configuration
@ComponentScan
public class Configurations {
}
  • 1
  • 2
  • 3
  • 4

AppleBean :

//@Component  这里不可以加注解 !!!!!!
public class AppleBean{ }
  • 1
  • 2
  • 3
  • 4

AppleFactoryBean :

@Component
public class AppleFactoryBean implements FactoryBean{ public Object getObject() throws Exception {
return new AppleBean();
} public Class<?> getObjectType() {
return AppleBean.class;
} public boolean isSingleton() {
return false;
}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

启动类 :

public class Start {
public static void main(String[] args){
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(Configurations.class);
out.println(applicationContext.getBean("appleFactoryBean"));//得到的是apple
out.println(applicationContext.getBean("&appleFactoryBean"));//得到的是apple工厂
}
}

理解spring中的BeanFactory和FactoryBean的区别与联系的更多相关文章

  1. spring中的BeanFactory和FactoryBean的区别与联系

    首先,这俩都是个接口… 实现 BeanFactory 接口的类表明此类是一个工厂,作用就是配置.新建.管理 各种Bean. 而 实现 FactoryBean 的类表明此类也是一个Bean,类型为工厂B ...

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

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

  3. Spring学习笔记——Spring中的BeanFactory与FactoryBean

    BeanFactory BeanFactory是Spring的org.springframework.beans.factory下的一个接口,是Spring IOC所遵守的基本编程规范.他的实现类有D ...

  4. Spring中的BeanFactory和ApplicationContext的区别

    我用一个例子去测试BeanFactory和ApplicationContext的区别 首先建立一个bean public class User { //声明无参构造,打印一句话,监测对象创建时机 pu ...

  5. 【Java面试】Spring中 BeanFactory和FactoryBean的区别

    一个工作了六年多的粉丝,胸有成竹的去京东面试. 然后被Spring里面的一个问题卡住,唉,我和他说,6年啦,Spring都没搞明白? 那怎么去让面试官给你通过呢? 这个问题是: Spring中Bean ...

  6. Spring中BeanFactory与FactoryBean的区别

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

  7. 通过BeanPostProcessor理解Spring中Bean的生命周期

    通过BeanPostProcessor理解Spring中Bean的生命周期及AOP原理 Spring源码解析(十一)Spring扩展接口InstantiationAwareBeanPostProces ...

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

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

  9. 深入理解spring中的各种注解

    Spring中的注解大概可以分为两大类: 1)spring的bean容器相关的注解,或者说bean工厂相关的注解: 2)springmvc相关的注解. spring的bean容器相关的注解,先后有:@ ...

随机推荐

  1. Linux系统中磁盘block和windos中的簇一个意思

    block就是几个连续扇区组成一个block,每个分区可以设置block大小,好比一个txt只有2字节,但是这个分区的block为4K,那么其实这个txt需要4k来存储(所以大文件block设置大比较 ...

  2. NodeJS CSV导出文件名和内容乱码解决

    // 解决不同浏览器下载文件名称乱码 var userAgent = (req.headers['user-agent']||'').toLowerCase(); res.set('Content-T ...

  3. tuple 元组

    创建单元素tuple tuple和list一样,可以包含 0 个.1个和任意多个元素. 包含多个元素的 tuple,前面我们已经创建过了. 包含 0 个元素的 tuple,也就是空tuple,直接用 ...

  4. linux命令-进阶1

    Linux查看系统信息的一些命令及查看已安装软件包的命令 centos下如何查看某个软件是否安装http://www.landui.com/help/show-3959.html CentOS 下 r ...

  5. Map根据value排序

    网上找到的资源, package com.test.ch1; import java.util.ArrayList; import java.util.Collections; import java ...

  6. LINUX内核升级-更新网卡驱动

    因项目需要,将当前内核(2.6.32-220.el6.x86_64)升级到目标内核(2.6.33-110.el6.x86_64),但是编译的目标 内核(2.6.33-110.el6.x86_64)的对 ...

  7. java开发目前技术选型

    目前系统采用 1.后端 服务框架:Dubbo.zookeeper 缓存:Redis.ehcache 消息中间件:ActiveMQ,kafka 负载均衡:Nginx 分布式文件:FastDFS 数据库连 ...

  8. oracle高水位

    oracle高水位http://www.cnblogs.com/chuyuhuashi/p/3548260.htmlhttp://blog.csdn.net/wyzxg/article/details ...

  9. 桥接模式和NAT模式差别

    bridged networking(桥接模式)      在这样的模式下.VMWare虚拟出来的操作系统就像是局域网中的一台独立的主机,它能够訪问网内不论什么一台机器. 在桥接模式下.你须要手工为虚 ...

  10. chrome 设置代理服务器

    通过设置google chrome浏览器的代理服务器可以让google chrome浏览器通过代理服务器上网,可以隐藏本机的IP地址或者访问一些不能直接访问的网站. 工具/原料   google ch ...