12--Spring BeanFactory和FactoryBean的区别

版权声明:如有转载,请标明出处,谢谢合作! https://blog.csdn.net/lyc_liyanchao/article/details/82424122

上一篇介绍了Spring中bean的作用域和生命周期,今天继续来温习Spring中的另一个重要接口FactoryBean,在初学Spring时,大家可能混淆Spring中的两个接口,FactoryBean和BeanFactory,我们先来看一下这两者的各自含义,再通过简单的例子说明一下FactoryBean的使用。

  • BeanFactory:在前面的博客中已经做了大量的介绍,该接口是IoC容器的顶级接口,是IoC容器的最基础实现,也是访问Spring容器的根接口,负责对bean的创建,访问等工作
  • FactoryBean:是一种工厂bean,可以返回bean的实例,我们可以通过实现该接口对bean进行额外的操作,例如根据不同的配置类型返回不同类型的bean,简化xml配置等;其在使用上也有些特殊,大家还记得BeanFactory中有一个字符常量String FACTORY_BEAN_PREFIX = "&"; 当我们去获取BeanFactory类型的bean时,如果beanName不加&则获取到对应bean的实例;如果beanName加上&,则获取到BeanFactory本身的实例;FactoryBean接口对应Spring框架来说占有重要的地位,Spring本身就提供了70多个FactoryBean的实现。他们隐藏了实例化一些复杂的细节,给上层应用带来了便利。从Spring3.0开始,FactoryBean开始支持泛型。

上面的文字可能生涩难懂,我们通过两个例子才简单说明下FactoryBean的使用:

1.简化xml配置,隐藏细节

如果一个类有很多的属性,我们想通过Spring来对类中的属性进行值的注入,势必要在配置文件中书写大量属性配置,造成配置文件臃肿,那么这时可以考虑使用FactoryBean来简化配置

  • 新建bean
package com.lyc.cn.day03;

/**
 * @author: LiYanChao
 * @create: 2018-09-05 11:50
 */
public class Student {
    /** 姓名 */
    private String name;
    /** 年龄 */
    private int age;
    /** 班级名称 */
    private String className;

    public Student() {
    }

    public Student(String name, int age, String className) {
        this.name = name;
        this.age = age;
        this.className = className;
    }

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getClassName() {
        return className;
    }

    public void setClassName(String className) {
        this.className = className;
    }

    @Override
    public String toString() {
        return "Student{" + "name='" + name + '\'' + ", age=" + age + ", className='" + className + '\'' + '}';
    }
}
  • 实现FactoryBean接口
package com.lyc.cn.day03;

import org.springframework.beans.factory.FactoryBean;

/**
 * @author: LiYanChao
 * @create: 2018-09-05 11:49
 */
public class StudentFactoryBean implements FactoryBean<Student> {

    private String studentInfo;

    @Override
    public Student getObject() throws Exception {
        if (this.studentInfo == null) {
            throw new IllegalArgumentException("'studentInfo' is required");
        }

        String[] splitStudentInfo = studentInfo.split(",");
        if (null == splitStudentInfo || splitStudentInfo.length != 3) {
            throw new IllegalArgumentException("'studentInfo' config error");
        }

        Student student = new Student();
        student.setName(splitStudentInfo[0]);
        student.setAge(Integer.valueOf(splitStudentInfo[1]));
        student.setClassName(splitStudentInfo[2]);
        return student;
    }

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

    public void setStudentInfo(String studentInfo) {
        this.studentInfo = studentInfo;
    }
}
  • 新建day03.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--注意:class是StudentFactoryBean而不是Student-->
    <bean id="student" class="com.lyc.cn.day03.StudentFactoryBean" p:studentInfo="张三,25,三年二班"/>

</beans>
  • 新建测试用例
package com.lyc.cn.day03;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @author: LiYanChao
 * @create: 2018-09-05 11:59
 */
public class MyTest {
    @Before
    public void before() {
        System.out.println("---测试开始---\n");
    }

    @After
    public void after() {
        System.out.println("\n---测试结束---");
    }

    @Test
    public void testStudentFactoryBean() {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("day03.xml");
        System.out.println(applicationContext.getBean("student"));
        System.out.println(applicationContext.getBean("&student"));
    }
}
  • 运行
---测试开始---

Student{name='张三', age=25, className='三年二班'}
org.springframework.beans.factory_bean.StudentFactoryBean@1ae369b7

---测试结束---

这样我们就实现了通过BeanFactory接口达到了简化配置文件的作用。另外大家也可以发现getBean(“student”)返回的Student类的实例;而getBean(“&student”)返回的是StudentFactoryBean实例,即工厂bean其本身。

2.返回不同Bean的实例

既然FactoryBean是一种工厂bean,那么我们就可以根据需要的类型,返回不同的bean的实例,通过代码简单说明一下

  • 新建bean
/**
 * @author: LiYanChao
 * @create: 2018-09-05 11:49
 */
public interface Animal {
    void sayHello();
}

/**
 * @author: LiYanChao
 * @create: 2018-09-05 15:10
 */
public class Cat implements Animal {
    @Override
    public void sayHello() {
        System.out.println("hello, 喵喵喵...");
    }
}

/**
 * @author: LiYanChao
 * @create: 2018-09-05 15:11
 */
public class Dog implements Animal {
    @Override
    public void sayHello() {
        System.out.println("hello, 汪汪汪...");
    }
}

创建了一个Animal接口极其两个实现类Cat和Dog,并进行简单输出,那么如何通过FactoryBean来通过配置返回不同的Animal实例呢

  • 新建AnimalFactoryBean
package com.lyc.cn.day03;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.FactoryBean;

/**
 * @author: LiYanChao
 * @create: 2018-09-05 15:11
 */
public class AnimalFactoryBean implements FactoryBean<Animal> {

    private String animal;

    @Override
    public Animal getObject() throws Exception {
        if (null == animal) {
            throw new IllegalArgumentException("'animal' is required");
        }
        if ("cat".equals(animal)) {
            return new Cat();
        } else if ("dog".equals(animal)) {
            return new Dog();
        } else {
            throw new IllegalArgumentException("animal type error");
        }
    }

    @Override
    public Class<?> getObjectType() {
        if (null == animal) {
            throw new IllegalArgumentException("'animal' is required");
        }
        if ("cat".equals(animal)) {
            return Cat.class;
        } else if ("dog".equals(animal)) {
            return Dog.class;
        } else {
            throw new IllegalArgumentException("animal type error");
        }
    }

    public void setAnimal(String animal) {
        this.animal = animal;
    }
}
  • 修改day03.xml配置文件,增加bean
<bean id="animal" class="com.lyc.cn.day03.AnimalFactoryBean" p:animal="cat"/>
  • 在MyTest中添加测试用例
@Test
public void testAnimalFactoryBean() {
    ApplicationContext applicationContext = new ClassPathXmlApplicationContext("day03.xml");
    Animal animal = applicationContext.getBean("animal", Animal.class);
    animal.sayHello();
}
  • 测试
---测试开始---

hello, 喵喵喵...

---测试结束---

可以看到,配置文件里我们将animal配置成了cat,那么返回的就是cat的实例,也是简单工厂的一个实现

3.FactoryBean源码

该接口的源码比较少,只声明了三个接口

public interface FactoryBean<T> {

    //返回此工厂管理的对象的实例(可能Singleton和Prototype)
    //如果此FactoryBean在调用时尚未完全初始化(例如,因为它涉及循环引用),则抛出相应的FactoryBeanNotInitializedException。
    //从Spring 2.0开始,允许FactoryBeans返回null 对象。工厂会将此视为正常使用价值; 在这种情况下,它不再抛出FactoryBeanNotInitializedException。
    //鼓励FactoryBean实现现在自己抛出FactoryBeanNotInitializedException,视情况而定。
    T getObject() throws Exception;

    //返回此FactoryBean创建的对象类型,默认返回null
    Class<?> getObjectType();

    //实例是否单例模式,默认返回true
    default boolean isSingleton() {
        return true;
    }

}

关于FactoryBean就先分析到这里了,上面在实现FactoryBean接口时没有重写其isSingleto()方法,Spring中大部分的bean都是单例bean,如果非单例bean的话,大家可以重写该方法返回false即可。

猜你喜欢

转载自blog.csdn.net/lyc_liyanchao/article/details/82424122