Spring FactoryBean和BeanFactory 区别

r囧r小猫 2022-10-06 13:57 246阅读 0赞

一、BeanFactory 是IOC容器的底层实现接口

是ApplicationContext 顶级接口,Spring不允许我们直接操作 BeanFactory即bean工厂。

BeanFactory 是顶层容器(根容器),不能被实例化,它定义了所有 IoC 容器必须遵从的一套原则,具体的容器实现可以增加额外的功能。所以为我们提供了ApplicationContext 这个接口,此接口继成BeanFactory 接口,ApplicationContext包含BeanFactory的所有功能,同时还进行更多的扩展。

BeanFactory 接口又衍生出以下接口。比如我们常用到的ApplicationContext,其下更具体的实现如 ClassPathXmlApplicationContext 包含了解析 xml 等一系列的内容,
AnnotationConfigApplicationContext 则是包含了注解解析等⼀系列的内容。Spring IOC 容器继承体系非常聪明,需要使用哪个层次用哪个层次即可。

  1. FileSystemXmlApplicationContext ClassPathXmlApplicationContext 是用来读取xml文件创建bean对象
  2. ClassPathXmlApplicationContext 读取类路径下xml 创建bean
  3. FileSystemXmlApplicationContext :读取文件系统下xml创建bean
  4. AnnotationConfigApplicationContext 主要是注解开发获取ioc中的bean实例

ApplicationContext 继承图
在这里插入图片描述

二、FactoryBean是Spirng提供的工厂bean的一个接口

FactoryBean 接口提供三个方法,用来创建对象,
FactoryBean 具体返回的对象是由getObject 方法决定的。
测试代码如下
FactoryBeanTest.class

  1. package com.example.demo;
  2. import org.springframework.stereotype.Component;
  3. @Component
  4. public class FactoryBeanTest implements FactoryBean<User> {
  5. //创建的具体bean对象的类型
  6. @Override
  7. public Class<?> getObjectType() {
  8. return User.class;
  9. }
  10. //是否单例
  11. @Override
  12. public boolean isSingleton() {
  13. return true;
  14. }
  15. //工厂bean 具体创建具体对象是由此getObject()方法来返回的
  16. @Override
  17. public User getObject() throws Exception {
  18. return new User();
  19. }
  20. }

FactoryBean.class

  1. package com.example.demo;
  2. import org.springframework.lang.Nullable;
  3. public interface FactoryBean<T> {
  4. //工厂bean 具体创建具体对象是由此getObject()方法来返回的
  5. @Nullable
  6. T getObject() throws Exception;
  7. //创建的具体bean对象的类型
  8. @Nullable
  9. Class<?> getObjectType();
  10. //是否单例
  11. default boolean isSingleton() {
  12. return true;
  13. }
  14. }

User.class

  1. package com.example.demo;
  2. public class User {
  3. private int id;
  4. private String name;
  5. private int age;
  6. //省略set\get方法
  7. public User() {
  8. }
  9. public User(int id, String name, int age) {
  10. this.id = id;
  11. this.name = name;
  12. this.age = age;
  13. }
  14. }

DemoApplicationTests .class

  1. package com.example.demo;
  2. import org.junit.jupiter.api.Test;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.boot.test.context.SpringBootTest;
  5. import org.springframework.context.ApplicationContext;
  6. @SpringBootTest
  7. class DemoApplicationTests {
  8. @Autowired
  9. private ApplicationContext applicationContext;
  10. @Test
  11. void contextLoads() {
  12. }
  13. @Test
  14. public void test() {
  15. FactoryBeanTest bean = applicationContext.getBean(FactoryBeanTest.class);
  16. try {
  17. User object = bean.getObject();
  18. object.setAge(100);
  19. object.setName("张三丰");
  20. object.setId(1);
  21. System.out.println("User对象是:"+object.getName()+","+object.getId()+","+object.getAge());
  22. System.out.println("bean对象类型:"+bean.getObjectType());
  23. System.out.println("bean对象是否单例:"+bean.isSingleton());
  24. } catch (Exception e) {
  25. e.printStackTrace();
  26. }
  27. }
  28. }

测试结果如下:
在这里插入图片描述

三、总结

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

发表评论

表情:
评论列表 (有 0 条评论,246人围观)

还没有评论,来说两句吧...

相关阅读