软件编程
位置:首页>> 软件编程>> java编程>> Spring BeanFactory和FactoryBean区别解析

Spring BeanFactory和FactoryBean区别解析

作者:天宇轩-王  发布时间:2023-09-18 15:38:28 

标签:Spring,Bean,Factory

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

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

来源:https://www.cnblogs.com/dalianpai/p/12011993.html

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com