spring---FactoryBean与BeanFactory的区别
1.BeanFactory
BeanFactory是IOC最基本的容器,负责生产和管理bean,它为其他具体的IOC容器提供了最基本的规范,例如DefaultListableBeanFactory,
XmlBeanFactory,ApplicationContext 等具体的容器都是实现了BeanFactory,再在其基础之上附加了其他的功能。
BeanFactory源码
- package org.springframework.beans.factory;
- import org.springframework.beans.BeansException;
- public interface BeanFactory {
- String FACTORY_BEAN_PREFIX = "&";
- Object getBean(String name) throws BeansException;
- <T> T getBean(String name, Class<T> requiredType) throws BeansException;
- <T> T getBean(Class<T> requiredType) throws BeansException;
- Object getBean(String name, Object... args) throws BeansException;
- boolean containsBean(String name);
- boolean isSingleton(String name) throws NoSuchBeanDefinitionException;
- boolean isPrototype(String name) throws NoSuchBeanDefinitionException;
- boolean isTypeMatch(String name, Class<?> targetType) throws NoSuchBeanDefinitionException;
- Class<?> getType(String name) throws NoSuchBeanDefinitionException;
- String[] getAliases(String name);
- }
2.FactoryBean
FactoryBean是一个接口,当在IOC容器中的Bean实现了FactoryBean后,通过getBean(String BeanName)获取到的Bean对象并不是FactoryBean的实现类对象,而是这个实现类中的getObject()方法返回的对象。要想获取FactoryBean的实现类,就要getBean(&BeanName),在BeanName之前加上&。
FactoryBean源码
- package org.springframework.beans.factory;
- public interface FactoryBean<T> {
- T getObject() throws Exception;
- Class<?> getObjectType();
- boolean isSingleton();
- }
下面是一个应用FactoryBean的例子
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:aop="http://www.springframework.org/schema/aop"
- xmlns:tx="http://www.springframework.org/schema/tx"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-3.0.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
- http://www.springframework.org/schema/tx
- http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
- <bean id="student" class="com.spring.bean.Student">
- <property name="name" value="zhangsan" />
- </bean>
- <bean id="school" class="com.spring.bean.School">
- </bean>
- <bean id="factoryBeanPojo" class="com.spring.bean.FactoryBeanPojo">
- <property name="type" value="student" />
- </bean>
- </beans>
FactoryBean的实现类
- import org.springframework.beans.factory.FactoryBean;
- /**
- * @author 作者 wangbiao
- * @date 创建时间:2016年11月14日 上午11:19:31
- * @parameter
- * @return
- */
- public class FactoryBeanPojo implements FactoryBean{
- private String type;
- @Override
- public Object getObject() throws Exception {
- if("student".equals(type)){
- return new Student();
- }else{
- return new School();
- }
- }
- @Override
- public Class getObjectType() {
- return School.class;
- }
- @Override
- public boolean isSingleton() {
- return true;
- }
- public String getType() {
- return type;
- }
- public void setType(String type) {
- this.type = type;
- }
- }
普通的bean
- /**
- * @author 作者 wangbiao
- * @date 创建时间:2016年11月14日 上午11:13:18
- * @parameter
- * @return
- */
- public class School {
- private String schoolName;
- private String address;
- private int studentNumber;
- public String getSchoolName() {
- return schoolName;
- }
- public void setSchoolName(String schoolName) {
- this.schoolName = schoolName;
- }
- public String getAddress() {
- return address;
- }
- public void setAddress(String address) {
- this.address = address;
- }
- public int getStudentNumber() {
- return studentNumber;
- }
- public void setStudentNumber(int studentNumber) {
- this.studentNumber = studentNumber;
- }
- @Override
- public String toString() {
- return "School [schoolName=" + schoolName + ", address=" + address
- + ", studentNumber=" + studentNumber + "]";
- }
- }
测试类
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import com.spring.bean.FactoryBeanPojo;
- /**
- * @author 作者 wangbiao
- * @date 创建时间:2016年11月14日 上午11:11:35
- * @parameter
- * @return
- */
- public class FactoryBeanTest {
- public static void main(String[] args){
- String url = "com/spring/config/BeanConfig.xml";
- ClassPathXmlApplicationContext cpxa = new ClassPathXmlApplicationContext(url);
- Object school= cpxa.getBean("factoryBeanPojo");
- FactoryBeanPojo factoryBeanPojo= (FactoryBeanPojo) cpxa.getBean("&factoryBeanPojo");
- System.out.println(school.getClass().getName());
- System.out.println(factoryBeanPojo.getClass().getName());
- }
- }
输出的结果:
- 十一月 16, 2016 10:28:24 上午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
- INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1e8ee5c0: startup date [Wed Nov 16 10:28:24 CST 2016]; root of context hierarchy
- 十一月 16, 2016 10:28:24 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
- INFO: Loading XML bean definitions from class path resource [com/spring/config/BeanConfig.xml]
- 十一月 16, 2016 10:28:24 上午 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
- INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@35b793ee: defining beans [student,school,factoryBeanPojo]; root of factory hierarchy
- com.spring.bean.Student
- 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的区别的更多相关文章
- 一文带你解读Spring5源码解析 IOC之开启Bean的加载,以及FactoryBean和BeanFactory的区别。
前言 通过往期的文章我们已经了解了Spring对XML配置文件的解析,将分析的信息组装成BeanDefinition,并将其保存到相应的BeanDefinitionRegistry中,至此Spring ...
- Spring中FactoryBean与BeanFactory的区别
版本:spring-framework-4.1 一概述 BeanFactory 与 FactoryBean的区别, 两个名字很像,面试中也经常遇到,所以容易搞混,现从源码以及示例两方面来分析. 二.源 ...
- Spring FactoryBean和BeanFactory 区别
1 BeanFactory 是ioc容器的底层实现接口,是ApplicationContext 顶级接口 spring不允许我们直接操作 BeanFactory bean工厂,所以为我们提供了App ...
- 7.FactoryBean 和BeanFactory去区别
FactoryBean源码: /* * Copyright 2002-2012 the original author or authors. * * Licensed under the Apach ...
- Spring中的BeanFactory与FactoryBean看这一篇就够了
前言 理解FactoryBean是非常非常有必要的,因为在Spring中FactoryBean最为典型的一个应用就是用来创建AOP的代理对象,不仅如此,而且对理解Mybatis核心源码也非常有帮助!如 ...
- Spring高级特性之四:FactoryBean和BeanFactory
FactoryBean和BeanFactory两只是两个单词顺序不同但是内容大不相同.落脚点在后面一个单词,前面一个单词是其功能描述:FactoryBean--工厂bean,一个建工厂的bean?Be ...
- 转:Spring系列之beanFactory与ApplicationContext
原文地址:Spring系列之beanFactory与ApplicationContext 一.BeanFactoryBeanFactory 是 Spring 的“心脏”.它就是 Spring IoC ...
- Spring FactoryBean用法
最近在看spring ioc源码,看到FactoryBean这个内容.这个和BeanFactory的区别 1. BeanFactory: 生成bean的工厂,是一个接口,定义了很多方法 2. Fact ...
- 【Spring IoC】BeanFactory 和 ApplicationContext(五)
一.BeanFactory容器 BeanFactory 容器是一个最简单的容器,它主要的功能是为依赖注入 (DI) 提供支持,这个容器接口在 org.springframework.beans.fac ...
- 转:Spring FactoryBean源码浅析
http://blog.csdn.net/java2000_wl/article/details/7410714 在Spring BeanFactory容器中管理两种bean 1.标准Java Bea ...
随机推荐
- php 对象方式传入参数
类是单例模式,对象方式传入参数,如果参数过多是,使用形参容易混乱 class Object { /** * 基本配置信息 * @var array */ private $config = array ...
- C++_标准模板库STL概念介绍4-算法
STL包含很多处理容器的非成员函数: sort() copy() find() random_shuffle() set_union() set_intersection() set_differen ...
- C++_标准模板库STL概念介绍3-函数对象
函数对象也叫做函数符(functor). 函数符是可以以函数方式和( )结合使用的任意对象. 包括函数名,指向函数的指针,重载了()运算符的类对象. 可以这样定义一个类: class Linear { ...
- HDU6393(LCA + RMQ + 树状数组) n边图,两点最短距离 , 修改边权
这道题的进阶版本 进阶版本 题意: 一个n个点,n条边的图,2中操作,1是将某条边的权值更改,2是询问两点的最短距离. 题解: 由于n个点,n条边,所以是树加一个环,将环上的边随意取出一条,就是1颗树 ...
- c# 实现无符号右移
/// <summary> /// 无符号右移, 相当于java里的 value>>>pos /// </summary> /// <param nam ...
- DictionaryHelper
/// <summary> /// DictionaryHelper /// </summary> public static class DictionaryHelper { ...
- HTML5实现多文件的上传示例代码
[转自] http://www.jb51.net/html5/136791.html 主要用到的是<input>的multiple属性 代码如下: <input type=" ...
- mutillidae之注册页面的Insert型报错注入
http://127.0.0.1/mutillidae/index.php?page=register.php 1.注册一个用户试一试,发现页面只提示用户注册成功信息,并五其它可回显信息,果断尝试盲注 ...
- python附录-builtins.py模块str类源码(含str官方文档链接)
python附录-builtins.py模块str类源码 str官方文档链接:https://docs.python.org/3/library/stdtypes.html#text-sequence ...
- 多个git库的ssh配置
当拥有多个git服务器,而且都是用ssh认证方式时,需要在~/.ssh下的config文件做如下配置 如下:第一个为本地git库,第二个为github库,第三个为默认git库 Host 192.168 ...