Spring FactoryBean和BeanFactory 区别
一、BeanFactory 是IOC容器的底层实现接口
是ApplicationContext 顶级接口,Spring不允许我们直接操作 BeanFactory即bean工厂。
BeanFactory 是顶层容器(根容器),不能被实例化,它定义了所有 IoC 容器必须遵从的一套原则,具体的容器实现可以增加额外的功能。所以为我们提供了ApplicationContext 这个接口,此接口继成BeanFactory 接口,ApplicationContext包含BeanFactory的所有功能,同时还进行更多的扩展。
BeanFactory 接口又衍生出以下接口。比如我们常用到的ApplicationContext,其下更具体的实现如 ClassPathXmlApplicationContext 包含了解析 xml 等一系列的内容,
AnnotationConfigApplicationContext 则是包含了注解解析等⼀系列的内容。Spring IOC 容器继承体系非常聪明,需要使用哪个层次用哪个层次即可。
FileSystemXmlApplicationContext 和ClassPathXmlApplicationContext 是用来读取xml文件创建bean对象
ClassPathXmlApplicationContext : 读取类路径下xml 创建bean
FileSystemXmlApplicationContext :读取文件系统下xml创建bean
AnnotationConfigApplicationContext 主要是注解开发获取ioc中的bean实例
ApplicationContext 继承图
二、FactoryBean是Spirng提供的工厂bean的一个接口
FactoryBean 接口提供三个方法,用来创建对象,
FactoryBean 具体返回的对象是由getObject 方法决定的。
测试代码如下
FactoryBeanTest.class
package com.example.demo;
import org.springframework.stereotype.Component;
@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();
}
}
FactoryBean.class
package com.example.demo;
import org.springframework.lang.Nullable;
public interface FactoryBean<T> {
//工厂bean 具体创建具体对象是由此getObject()方法来返回的
@Nullable
T getObject() throws Exception;
//创建的具体bean对象的类型
@Nullable
Class<?> getObjectType();
//是否单例
default boolean isSingleton() {
return true;
}
}
User.class
package com.example.demo;
public class User {
private int id;
private String name;
private int age;
//省略set\get方法
public User() {
}
public User(int id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
}
DemoApplicationTests .class
package com.example.demo;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
@SpringBootTest
class DemoApplicationTests {
@Autowired
private ApplicationContext applicationContext;
@Test
void contextLoads() {
}
@Test
public void test() {
FactoryBeanTest bean = applicationContext.getBean(FactoryBeanTest.class);
try {
User object = bean.getObject();
object.setAge(100);
object.setName("张三丰");
object.setId(1);
System.out.println("User对象是:"+object.getName()+","+object.getId()+","+object.getAge());
System.out.println("bean对象类型:"+bean.getObjectType());
System.out.println("bean对象是否单例:"+bean.isSingleton());
} catch (Exception e) {
e.printStackTrace();
}
}
}
测试结果如下:
三、总结
BeanFactory是个bean 工厂,是一个工厂类(接口), 它负责生产和管理bean的一个工厂。 它是IOC
容器最底层的接口,是个IOC容器,是Spring用来管理和装配普通bean的IOC容器(这些bean成为普通bean)。FactoryBean是个bean,在IOC容器的基础上给Bean的实现加上了一个简单工厂模式和装饰模式,是一个可以生产对象和装饰对象的工厂bean,由Spring管理后,生产的对象是由getObject()方法决定的(从容器中获取到的对象不是 “ FactoryBeanTest ” 对象)。
参考文献:
https://blog.csdn.net/weixin\_38361347/article/details/92852611
还没有评论,来说两句吧...