1.BeanFactory

BeanFactory是IOC最基本的容器,负责生产和管理bean,它为其他具体的IOC容器提供了最基本的规范,例如DefaultListableBeanFactory,

XmlBeanFactory,ApplicationContext 等具体的容器都是实现了BeanFactory,再在其基础之上附加了其他的功能。

BeanFactory源码

  1.  
    package org.springframework.beans.factory;
  2.  
    import org.springframework.beans.BeansException;
  3.  
    public interface BeanFactory {
  4.  
    String FACTORY_BEAN_PREFIX = "&";
  5.  
    Object getBean(String name) throws BeansException;
  6.  
    <T> T getBean(String name, Class<T> requiredType) throws BeansException;
  7.  
    <T> T getBean(Class<T> requiredType) throws BeansException;
  8.  
    Object getBean(String name, Object... args) throws BeansException;
  9.  
    boolean containsBean(String name);
  10.  
    boolean isSingleton(String name) throws NoSuchBeanDefinitionException;
  11.  
    boolean isPrototype(String name) throws NoSuchBeanDefinitionException;
  12.  
    boolean isTypeMatch(String name, Class<?> targetType) throws NoSuchBeanDefinitionException;
  13.  
    Class<?> getType(String name) throws NoSuchBeanDefinitionException;
  14.  
    String[] getAliases(String name);
  15.  
    }

2.FactoryBean

FactoryBean是一个接口,当在IOC容器中的Bean实现了FactoryBean后,通过getBean(String BeanName)获取到的Bean对象并不是FactoryBean的实现类对象,而是这个实现类中的getObject()方法返回的对象。要想获取FactoryBean的实现类,就要getBean(&BeanName),在BeanName之前加上&。

FactoryBean源码

  1.  
    package org.springframework.beans.factory;
  2.  
    public interface FactoryBean<T> {
  3.  
    T getObject() throws Exception;
  4.  
    Class<?> getObjectType();
  5.  
    boolean isSingleton();
  6.  
    }

下面是一个应用FactoryBean的例子

  1.  
    <beans xmlns="http://www.springframework.org/schema/beans"
  2.  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3.  
    xmlns:context="http://www.springframework.org/schema/context"
  4.  
    xmlns:aop="http://www.springframework.org/schema/aop"
  5.  
    xmlns:tx="http://www.springframework.org/schema/tx"
  6.  
    xsi:schemaLocation="http://www.springframework.org/schema/beans
  7.  
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  8.  
    http://www.springframework.org/schema/context
  9.  
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
  10.  
    http://www.springframework.org/schema/aop
  11.  
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
  12.  
    http://www.springframework.org/schema/tx
  13.  
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
  14.  
     
  15.  
    <bean id="student" class="com.spring.bean.Student">
  16.  
    <property name="name" value="zhangsan" />
  17.  
    </bean>
  18.  
     
  19.  
    <bean id="school" class="com.spring.bean.School">
  20.  
    </bean>
  21.  
     
  22.  
    <bean id="factoryBeanPojo" class="com.spring.bean.FactoryBeanPojo">
  23.  
    <property name="type" value="student" />
  24.  
    </bean>
  25.  
    </beans>

FactoryBean的实现类

  1.  
    import org.springframework.beans.factory.FactoryBean;
  2.  
     
  3.  
    /**
  4.  
    * @author 作者 wangbiao
  5.  
    * @date 创建时间:2016年11月14日 上午11:19:31
  6.  
    * @parameter
  7.  
    * @return
  8.  
    */
  9.  
    public class FactoryBeanPojo implements FactoryBean{
  10.  
    private String type;
  11.  
     
  12.  
    @Override
  13.  
    public Object getObject() throws Exception {
  14.  
    if("student".equals(type)){
  15.  
    return new Student();
  16.  
    }else{
  17.  
    return new School();
  18.  
    }
  19.  
     
  20.  
    }
  21.  
     
  22.  
    @Override
  23.  
    public Class getObjectType() {
  24.  
    return School.class;
  25.  
    }
  26.  
     
  27.  
    @Override
  28.  
    public boolean isSingleton() {
  29.  
    return true;
  30.  
    }
  31.  
     
  32.  
    public String getType() {
  33.  
    return type;
  34.  
    }
  35.  
     
  36.  
    public void setType(String type) {
  37.  
    this.type = type;
  38.  
    }
  39.  
     
  40.  
    }

普通的bean

  1.  
    /**
  2.  
    * @author 作者 wangbiao
  3.  
    * @date 创建时间:2016年11月14日 上午11:13:18
  4.  
    * @parameter
  5.  
    * @return
  6.  
    */
  7.  
    public class School {
  8.  
    private String schoolName;
  9.  
    private String address;
  10.  
    private int studentNumber;
  11.  
    public String getSchoolName() {
  12.  
    return schoolName;
  13.  
    }
  14.  
    public void setSchoolName(String schoolName) {
  15.  
    this.schoolName = schoolName;
  16.  
    }
  17.  
    public String getAddress() {
  18.  
    return address;
  19.  
    }
  20.  
    public void setAddress(String address) {
  21.  
    this.address = address;
  22.  
    }
  23.  
    public int getStudentNumber() {
  24.  
    return studentNumber;
  25.  
    }
  26.  
    public void setStudentNumber(int studentNumber) {
  27.  
    this.studentNumber = studentNumber;
  28.  
    }
  29.  
    @Override
  30.  
    public String toString() {
  31.  
    return "School [schoolName=" + schoolName + ", address=" + address
  32.  
    + ", studentNumber=" + studentNumber + "]";
  33.  
    }
  34.  
    }

测试类

  1.  
    import org.springframework.context.support.ClassPathXmlApplicationContext;
  2.  
     
  3.  
    import com.spring.bean.FactoryBeanPojo;
  4.  
     
  5.  
    /**
  6.  
    * @author 作者 wangbiao
  7.  
    * @date 创建时间:2016年11月14日 上午11:11:35
  8.  
    * @parameter
  9.  
    * @return
  10.  
    */
  11.  
    public class FactoryBeanTest {
  12.  
    public static void main(String[] args){
  13.  
    String url = "com/spring/config/BeanConfig.xml";
  14.  
    ClassPathXmlApplicationContext cpxa = new ClassPathXmlApplicationContext(url);
  15.  
    Object school= cpxa.getBean("factoryBeanPojo");
  16.  
    FactoryBeanPojo factoryBeanPojo= (FactoryBeanPojo) cpxa.getBean("&factoryBeanPojo");
  17.  
    System.out.println(school.getClass().getName());
  18.  
    System.out.println(factoryBeanPojo.getClass().getName());
  19.  
    }
  20.  
    }

输出的结果:

  1.  
    十一月 16, 2016 10:28:24 上午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
  2.  
    INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1e8ee5c0: startup date [Wed Nov 16 10:28:24 CST 2016]; root of context hierarchy
  3.  
    十一月 16, 2016 10:28:24 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
  4.  
    INFO: Loading XML bean definitions from class path resource [com/spring/config/BeanConfig.xml]
  5.  
    十一月 16, 2016 10:28:24 上午 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
  6.  
    INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@35b793ee: defining beans [student,school,factoryBeanPojo]; root of factory hierarchy
  7.  
    com.spring.bean.Student
  8.  
    com.spring.bean.FactoryBeanPojo

从结果上可以看到当从IOC容器中获取FactoryBeanPojo对象的时候,用getBean(String BeanName)获取的确是Student对象,可以看到在FactoryBeanPojo中的type属性设置为student的时候,会在getObject()方法中返回Student对象。所以说从IOC容器获取实现了FactoryBean的实现类时,返回的却是实现类中的getObject方法返回的对象,要想获取FactoryBean的实现类,得在getBean(String BeanName)中的BeanName之前加上&,写成getBean(String
&BeanName)。

3.BeanFactory和FactoryBean的区别

BeanFactory和FactoryBean其实没有什么比较性的,只是两者的名称特别接近,所以有时候会拿出来比较一番,BeanFactory是提供了OC容器最基本的形式,给具体的IOC容器的实现提供了规范,FactoryBean可以说为IOC容器中Bean的实现提供了更加灵活的方式,FactoryBean在IOC容器的基础上给Bean的实现加上了一个简单工厂模式和装饰模式,我们可以在getObject()方法中灵活配置。其实在Spring源码中有很多FactoryBean的实现类,要想深入准确的理解FactoryBean,只有去读读Spring源码了。

spring---FactoryBean与BeanFactory的区别的更多相关文章

  1. 一文带你解读Spring5源码解析 IOC之开启Bean的加载,以及FactoryBean和BeanFactory的区别。

    前言 通过往期的文章我们已经了解了Spring对XML配置文件的解析,将分析的信息组装成BeanDefinition,并将其保存到相应的BeanDefinitionRegistry中,至此Spring ...

  2. Spring中FactoryBean与BeanFactory的区别

    版本:spring-framework-4.1 一概述 BeanFactory 与 FactoryBean的区别, 两个名字很像,面试中也经常遇到,所以容易搞混,现从源码以及示例两方面来分析. 二.源 ...

  3. Spring FactoryBean和BeanFactory 区别

    1 BeanFactory 是ioc容器的底层实现接口,是ApplicationContext 顶级接口 spring不允许我们直接操作 BeanFactory  bean工厂,所以为我们提供了App ...

  4. 7.FactoryBean 和BeanFactory去区别

    FactoryBean源码: /* * Copyright 2002-2012 the original author or authors. * * Licensed under the Apach ...

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

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

  6. Spring高级特性之四:FactoryBean和BeanFactory

    FactoryBean和BeanFactory两只是两个单词顺序不同但是内容大不相同.落脚点在后面一个单词,前面一个单词是其功能描述:FactoryBean--工厂bean,一个建工厂的bean?Be ...

  7. 转:Spring系列之beanFactory与ApplicationContext

    原文地址:Spring系列之beanFactory与ApplicationContext 一.BeanFactoryBeanFactory 是 Spring 的“心脏”.它就是 Spring IoC ...

  8. Spring FactoryBean用法

    最近在看spring ioc源码,看到FactoryBean这个内容.这个和BeanFactory的区别 1. BeanFactory: 生成bean的工厂,是一个接口,定义了很多方法 2. Fact ...

  9. 【Spring IoC】BeanFactory 和 ApplicationContext(五)

    一.BeanFactory容器 BeanFactory 容器是一个最简单的容器,它主要的功能是为依赖注入 (DI) 提供支持,这个容器接口在 org.springframework.beans.fac ...

  10. 转:Spring FactoryBean源码浅析

    http://blog.csdn.net/java2000_wl/article/details/7410714 在Spring BeanFactory容器中管理两种bean 1.标准Java Bea ...

随机推荐

  1. 014 Android BottomNavigationView 底部导航组件使用

    1.导入BottomNavigationView组件(点击下载按钮,安装组件) 2.新建菜单 (1)app--->src-->main--->res ,选中res目录右击new--- ...

  2. PHP 中 快捷的三元运算...

    a!=null ? a: 123 当 a 不为空时,返回 123, 可简写为 a ?: 123(a 不为null 不为 '','0',0 时,返回它本身,否则返回123...)

  3. C++_代码重用4-多重继承

    继承使用时要注意,默认是私有派生.所以要公有派生时必须记得加关键字Public. MI(Multi Inheritance)会带来哪些问题?以及如何解决它们? 两个主要问题: 从两个不同的基类继承同名 ...

  4. ubuntu系统下如何切换输入法

    如何切换输入法:ctrl+空格键 输入中文时如何翻页:键盘上的 - +两个键 super表示:美式键盘中的Win键

  5. CMakeFiles/species.inc.dir/build.make:57: recipe for target 'CMakeFiles/species.inc' failed

    新装的WSL编译2017.3.4版本的mfix,只要涉及到带化学反应的就会报错: 由于之前从没遇到过,对cmake又不熟悉,所以有些摸不着头脑,后来仔细查看报错提示,发现是在CMakeFiles/sp ...

  6. 【算法笔记】B1027 打印沙漏

    1027 打印沙漏 (20 分) 本题要求你写个程序把给定的符号打印成沙漏的形状.例如给定17个“*”,要求按下列格式打印 ***** *** * *** ***** 所谓“沙漏形状”,是指每行输出奇 ...

  7. SPOJ - REPEATS RMQ循环节

    题意:求重复次数最多的重复子串(并非长度最长) 枚举循环子串长度\(L\),求最多能连续出现多少次,相邻的节点往后的判断可以使用\(LCP\)得到值为\(K\),那么得到一个可能的解就是\(K/L+1 ...

  8. UESTC - 1652 递推方程

    方程很简单,每一公里往上推就行 WA了2发,忘了单通道时的特判,还有n m傻傻分不清,忘了fixed什么的我好弱啊QAQ.. #include<bits/stdc++.h> #define ...

  9. 安装软件或运行软件时提示缺少api-ms-win-crt-runtime库解决方法

    最近碰到一个问题,在我软件安装或运行时会提示缺少api-ms-win-crt-runtime-|1-1-0.dll 当然第一个想到的是运行库没有装,但是很清楚的是我的电脑是装过vc_redist_20 ...

  10. 设计模式学习总结(七)适配器模式(Adapter)

    适配器模式主要是通过适配器来实现接口的统一,如要实现国内手机在国外充电,则需要在不同的国家采用不同的适配器来进行兼容! 一.示例展示: 以下例子主要通过给笔记本电脑添加类似手机打电话和发短信的功能来详 ...