温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

BeanFactory和FactoryBean在Spring中的区别是什么

发布时间:2021-01-27 17:24:18 来源:亿速云 阅读:190 作者:Leah 栏目:编程语言

BeanFactory和FactoryBean在Spring中的区别是什么?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

BeanFactory接口:

IoC容器的顶级接口,是IoC容器的最基础实现,也是访问Spring容器的根接口,负责对bean的创建,访问等工作。

其实在容器的初始化的时候,会对BeanFactory做很多事情,如:

obtainFreshBeanFactory();获取BeanFactory;

prepareBeanFactory(beanFactory);BeanFactory的预准备工作(BeanFactory进行一些设置)

postProcessBeanFactory(beanFactory);BeanFactory准备工作完成后进行的后置处理工作;

invokeBeanFactoryPostProcessors(beanFactory);执行BeanFactoryPostProcessor的方法;

BeanFactoryPostProcessor:BeanFactory的后置处理器。在BeanFactory标准初始化之后执行的;

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开始支持泛型。

代码展示:

//创建一个Spring定义的FactoryBean
public class ColorFactoryBean implements FactoryBean<Color> {

  //返回一个Color对象,这个对象会添加到容器中
  @Override
  public Color getObject() throws Exception {
    // TODO Auto-generated method stub
    System.out.println("ColorFactoryBean...getObject...");
    return new Color();
  }
  @Override
  public Class<?> getObjectType() {
    // TODO Auto-generated method stub
    return Color.class;
  }
  //是单例?
  //true:这个bean是单实例,在容器中保存一份
  //false:多实例,每次获取都会创建一个新的bean;
  @Override
  public boolean isSingleton() {
    // TODO Auto-generated method stub
    return false;
  }
}
public class Color {
  
  private Car car;

  public Car getCar() {
    return car;
  }

  public void setCar(Car car) {
    this.car = car;
  }

  @Override
  public String toString() {
    return "Color [car=" + car + "]";
  }
  
}

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:context="http://www.springframework.org/schema/context"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">

  <bean id="colorFactoryBean" class="spring2.ColorFactoryBean"></bean>

</beans>

测试类:

public class Test1 {
    ClassPathXmlApplicationContext xmlBeanFactory = null;
    @Before
    public void initXmlBeanFactory() {
      System.out.println("\n========测试方法开始=======\n");
      xmlBeanFactory = new ClassPathXmlApplicationContext("spring3.xml");
    }

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

    @Test
    public void test8() {
      System.out.println(xmlBeanFactory.getBean("colorFactoryBean"));
       System.out.println("===================");
      System.out.println(xmlBeanFactory.getBean("&colorFactoryBean"));
    
    }
}

测试结果:

========测试方法开始=======

十二月 09, 2019 4:49:52 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@2e5c649: startup date [Mon Dec 09 16:49:52 CST 2019]; root of context hierarchy
十二月 09, 2019 4:49:52 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [spring3.xml]
ColorFactoryBean...getObject...
Color [car=null]
===================
spring2.ColorFactoryBean@6ddf90b0

========测试方法结束=======

看完上述内容,你们掌握BeanFactory和FactoryBean在Spring中的区别是什么的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI