BeanFactory和FactoryBean的区别(详解)

BeanFactory

BeanFactory是一个接口也是IOC最基本的容器,负责生产和管理bean,它为其他具体的IOC容器提供了最基本的规范,比如DefaultListableBeanFactory、XmlBeanFactory、ApplicationContext等具体的容器都是对BeanFactory接口的实现,再在其基础之上附加其它的功能。

FactoryBean

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

  • 隐藏了实例化一些复杂Bean的细节,给上层应用带来了便利.
  • 和设计模式中的工厂模式和修饰器模式相似.
  • getBean(beanName)方法获取的是getObject()返回的对象。
  • getBean(&beanName)方法获取的才是实现FactoryBean的对象。
  • getObject()里面可以返回如何对象,所以FactoryBean为Bean提供了更加灵活的实现方式。

FactoryBean的案例

1.创建FactoryBean的实现类

  • getBean(beanName)方法获取的是getObject()返回的对象。
  • getBean(&beanName)方法获取的才是实现FactoryBean的对象。
  • getObject()方法确实可以实现隐藏实现的细节的功能。
  • getObject()里面可以返回如何对象,所以FactoryBean为Bean提供了更加灵活的实现方式。
package factorybean;
import org.springframework.beans.factory.FactoryBean;
public class FactoryBeanPojo implements FactoryBean {

    private String type;

    /**
     * getBean获取的是getObject()方法返回的对象
     * @return
     * @throws Exception
     */
    @Override
    public Object getObject() throws Exception {
        if("student".equals(type)){
            Student student = new Student();
            student.setName("学生");
            return student;
        }else{
            Teacher teacher = new Teacher();
            teacher.setName("老司机");
            return teacher;
        }
    }

    @Override
    public boolean isSingleton() {
        return true;
    }

    @Override
    public Class<?> getObjectType() {
        return Teacher.class;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }
}

2.创建两个普通的Bean

package factorybean;
public class Student {

    private String name;

    public Student(){

    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
package factorybean;
public class Teacher {
    private String name;

    public Teacher(){

    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

3.测试代码

package factorybean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class FactoryBeanTest {
    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
        Object student = applicationContext.getBean("factoryBeanPojo");
        System.out.println("student:"+student);
        Object factoryBeanPojo = applicationContext.getBean("&factoryBeanPojo");
        System.out.println("factoryBeanPojo:"+factoryBeanPojo);
    }
}

4.spring配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="factoryBeanPojo" class="factorybean.FactoryBeanPojo">
        <property name="type" value="student"/>
    </bean>
</beans>

5.代码执行结果

在这里插入图片描述

  • 1.getBean(beanName)由于spring配置的是student所以getObject()返回的是Student对象。
  • 2.getBean(&beanName)返回的是实现FactoryBean接口对象本身在这里也验证了。
发布了60 篇原创文章 · 获赞 1 · 访问量 3327

猜你喜欢

转载自blog.csdn.net/qq_16438883/article/details/103680167