欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring BeanFactory和FactoryBean區(qū)別解析

 更新時(shí)間:2020年03月04日 12:09:44   作者:天宇軒-王  
這篇文章主要介紹了Spring BeanFactory和FactoryBean區(qū)別解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

BeanFactory接口:

IoC容器的頂級(jí)接口,是IoC容器的最基礎(chǔ)實(shí)現(xiàn),也是訪問(wèn)Spring容器的根接口,負(fù)責(zé)對(duì)bean的創(chuàng)建,訪問(wèn)等工作。

其實(shí)在容器的初始化的時(shí)候,會(huì)對(duì)BeanFactory做很多事情,如:

obtainFreshBeanFactory();獲取BeanFactory;

prepareBeanFactory(beanFactory);BeanFactory的預(yù)準(zhǔn)備工作(BeanFactory進(jìn)行一些設(shè)置)

postProcessBeanFactory(beanFactory);BeanFactory準(zhǔn)備工作完成后進(jìn)行的后置處理工作;

invokeBeanFactoryPostProcessors(beanFactory);執(zhí)行BeanFactoryPostProcessor的方法;

BeanFactoryPostProcessor:BeanFactory的后置處理器。在BeanFactory標(biāo)準(zhǔn)初始化之后執(zhí)行的;

FactoryBean接口:

可以返回bean的實(shí)例的工廠bean,通過(guò)實(shí)現(xiàn)該接口可以對(duì)bean進(jìn)行一些額外的操作,例如根據(jù)不同的配置類型返回不同類型的bean,簡(jiǎn)化xml配置等。在使用上也有些特殊,BeanFactory接口中有一個(gè)字符常量String FACTORY_BEAN_PREFIX = "&"; 當(dāng)我們?nèi)カ@取BeanFactory類型的bean時(shí),如果beanName不加&則獲取到對(duì)應(yīng)bean的實(shí)例;

如果beanName加上&,則獲取到BeanFactory本身的實(shí)例;FactoryBean接口對(duì)應(yīng)Spring框架來(lái)說(shuō)占有重要的地位,Spring本身就提供了70多個(gè)FactoryBean的實(shí)現(xiàn)。他們隱藏了實(shí)例化一些復(fù)雜的細(xì)節(jié),給上層應(yīng)用帶來(lái)了便利。從Spring3.0開(kāi)始,F(xiàn)actoryBean開(kāi)始支持泛型。

代碼展示:

//創(chuàng)建一個(gè)Spring定義的FactoryBean
public class ColorFactoryBean implements FactoryBean<Color> {

  //返回一個(gè)Color對(duì)象,這個(gè)對(duì)象會(huì)添加到容器中
  @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:這個(gè)bean是單實(shí)例,在容器中保存一份
  //false:多實(shí)例,每次獲取都會(huì)創(chuàng)建一個(gè)新的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>

測(cè)試類:

public class Test1 {
    ClassPathXmlApplicationContext xmlBeanFactory = null;
    @Before
    public void initXmlBeanFactory() {
      System.out.println("\n========測(cè)試方法開(kāi)始=======\n");
      xmlBeanFactory = new ClassPathXmlApplicationContext("spring3.xml");
    }

    @After
    public void after() {
      System.out.println("\n========測(cè)試方法結(jié)束=======\n");
    }

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

測(cè)試結(jié)果:

========測(cè)試方法開(kāi)始=======

十二月 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

========測(cè)試方法結(jié)束=======

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論