Spring FactoryBean和BeanFactory 区别
转自:https://blog.csdn.net/weixin_38361347/article/details/92852611
FactoryBean是一个工厂bean,我们可以通过实现这个接口来对我们的bean加一些其他的功能。
1 BeanFactory 是ioc容器的底层实现接口,是ApplicationContext 顶级接口
spring不允许我们直接操作 BeanFactory bean工厂,所以为我们提供了ApplicationContext 这个接口 此接口集成BeanFactory 接口,ApplicationContext包含BeanFactory的所有功能,同时还进行更多的扩展。
BeanFactory 接口又衍生出以下接口,其中我们经常用到的是ApplicationContext 接口
ApplicationContext 继承图

CongihuableApplicationContext 中添加了一些方法:
... 其他省略 //刷新ioc容器上下文 void refresh() throws BeansException, IllegalStateException; // 关闭此应用程序上下文,释放所有资源并锁定,销毁所有缓存的单例bean。 @Override void close(); //确定此应用程序上下文是否处于活动状态,即,是否至少刷新一次且尚未关闭。 boolean isActive(); ... 其他省略
主要作用在ioc容器进行相应的刷新,关闭等操作!
FileSystemXmlApplicationContext 和ClassPathXmlApplicationContext 是用来读取xml文件创建bean对象 ClassPathXmlApplicationContext : 读取类路径下xml 创建bean FileSystemXmlApplicationContext :读取文件系统下xml创建bean AnnotationConfigApplicationContext 主要是注解开发获取ioc中的bean实例
2 FactoryBean 是spirng提供的工厂bean的一个接口
FactoryBean 接口提供三个方法,用来创建对象,
FactoryBean 具体返回的对象是由getObject 方法决定的。
*/
public interface FactoryBean<T> {
//创建的具体bean对象的类型
@Nullable
T getObject() throws Exception;
//工厂bean 具体创建具体对象是由此getObject()方法来返回的
@Nullable
Class<?> getObjectType();
//是否单例
default boolean isSingleton() {
return true;
}
}
创建一个FactoryBean 用来生产User对象
@Component
public class FactoryBeanTest implements FactoryBean<User> {
//创建的具体bean对象的类型
@Override
public Class<?> getObjectType() {
return User.class;
}
//是否单例
@Override
public boolean isSingleton() {
return true;
}
//工厂bean 具体创建具体对象是由此getObject()方法来返回的
@Override
public User getObject() throws Exception {
return new User();
}
}
Junit测试
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {FactoryBeanTest.class})
@WebAppConfiguration
public class SpringBootDemoApplicationTests {
@Autowired
private ApplicationContext applicationContext;
@Test
public void tesst() {
FactoryBeanTest bean1 = applicationContext.getBean(FactoryBeanTest.class);
try {
User object = bean1.getObject();
System.out.println(object==object);
System.out.println(object);
} catch (Exception e) {
e.printStackTrace();
}
}
}
结果
true
User [id=null, name=null, age=0]
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;
}
}
简单的总结:
BeanFactory是个bean 工厂,是一个工厂类(接口), 它负责生产和管理bean的一个工厂
是ioc 容器最底层的接口,是个ioc容器,是spring用来管理和装配普通bean的ioc容器(这些bean成为普通bean)。
FactoryBean是个bean,在IOC容器的基础上给Bean的实现加上了一个简单工厂模式和装饰模式,是一个可以生产对象和装饰对象的工厂bean,由spring管理后,生产的对象是由getObject()方法决定的(从容器中获取到的对象不是
“ FactoryBeanTest ” 对象)。

浙公网安备 33010602011771号