Spring BeanFactory和FactoryBean

Spring BeanFactory和FactoryBeanjava

一、BeanFactory

BeanFactory定義了 IOC 容器的最基本形式,並提供了 IOC 容器應遵照的的最基本的接口,也就是 Spring IOC所遵照的最底層和最基本的編程規範。在  Spring 代碼中, BeanFactory 只是個接口,並非 IOC 容器的具體實現,可是 Spring 容器給出了不少種實現,如 DefaultListableBeanFactory 、 XmlBeanFactory 、 ApplicationContext等,都是附加了某種功能的實現。spring

public interface BeanFactory {

	String FACTORY_BEAN_PREFIX = "&";

	/**
	 * Return an instance, which may be shared or independent, of the specified bean.
	 */
	Object getBean(String name) throws BeansException;

	/**
	 * Return an instance, which may be shared or independent, of the specified bean.
	 */
	<T> T getBean(String name, Class<T> requiredType) throws BeansException;

	/**
	 * Return the bean instance that uniquely matches the given object type, if any.
	 * @param requiredType type the bean must match; can be an interface or superclass.
	 * {@code null} is disallowed.
	 */
	<T> T getBean(Class<T> requiredType) throws BeansException;

	/**
	 * Return an instance, which may be shared or independent, of the specified bean.
	
	 */
	Object getBean(String name, Object... args) throws BeansException;

	/**
	 * Does this bean factory contain a bean definition or externally registered singleton
	 * instance with the given name?
	 */
	boolean containsBean(String name);

	/**
	 * Is this bean a shared singleton? That is, will {@link #getBean} always
	 * return the same instance?
	 */
	boolean isSingleton(String name) throws NoSuchBeanDefinitionException;

	/**
	 * Is this bean a prototype? That is, will {@link #getBean} always return
	 * independent instances?
	 */
	boolean isPrototype(String name) throws NoSuchBeanDefinitionException;

	/**
	 * Check whether the bean with the given name matches the specified type.
	 * More specifically, check whether a {@link #getBean} call for the given name
	 * would return an object that is assignable to the specified target type.
	 */
	boolean isTypeMatch(String name, Class<?> targetType) throws NoSuchBeanDefinitionException;

	/**
	 */
	Class<?> getType(String name) throws NoSuchBeanDefinitionException;

	/**
	 * Return the aliases for the given bean name, if any.
	 * All of those aliases point to the same bean when used in a {@link #getBean} call.
	 */
	String[] getAliases(String name);

}

 

二、FactoryBean

通常狀況下,Spring 經過反射機制利用 <bean> 的 class 屬性指定實現類實例化 Bean。在某些狀況下,實例化Bean 過程比較複雜,若是按照傳統的方式,則須要在 <bean> 中提供大量的配置信息。配置方式的靈活性是受限的,這時採用編碼的方式可能會獲得一個簡單的方案。Spring 爲此提供了一個org.springframework.bean.factory.FactoryBean 的工廠類接口,用戶能夠經過實現該接口定製實例化 Bean 的邏輯。編程

FactoryBean接口對於 Spring 框架來講佔用重要的地位, Spring 自身就提供了 70 多個 FactoryBean 的實現。它們隱藏了實例化一些複雜 Bean 的細節,給上層應用帶來了便利。從 Spring 3.0 開始, FactoryBean 開始支持泛型,即接口聲明改成 FactoryBean<T> 的形式:設計模式

public interface FactoryBean<T> {

	/**
	 * Return an instance (possibly shared or independent) of the object
	 * managed by this factory.
	 */
	T getObject() throws Exception;

	/**
	 * Return the type of object that this FactoryBean creates,
	 * or <code>null</code> if not known in advance.
	 */
	Class<?> getObjectType();

	/**
	 * Is the object managed by this factory a singleton? That is,
	 * will {@link #getObject()} always return the same object
	 * (a reference that can be cached)?
	 */
	boolean isSingleton();

}

在該接口中還定義瞭如下3 個方法:緩存

T getObject():返回由 FactoryBean 建立的 Bean 實例,若是 isSingleton() 返回 true ,則該實例會放到 Spring容器中單實例緩存池中;框架

boolean isSingleton():返回由 FactoryBean 建立的 Bean 實例的做用域是 singleton 仍是 prototype ;ui

Class<T> getObjectType():返回 FactoryBean 建立的 Bean 類型。this

當配置文件中<bean> 的 class 屬性配置的實現類是 FactoryBean 時,經過 getBean() 方法返回的不是FactoryBean 自己,而是 FactoryBean#getObject() 方法所返回的對象,至關於 FactoryBean#getObject() 代理了getBean() 方法。編碼

例:若是使用傳統方式配置下面Car 的 <bean> 時, Car 的每一個屬性分別對應一個 <property> 元素標籤。spa

public class Car {
    private int maxSpeed;
    private String brand;
    private double price;

    public int getMaxSpeed() {
        return this.maxSpeed;
    }

    public void setMaxSpeed(int maxSpeed) {
        this.maxSpeed = maxSpeed;
    }

    public String getBrand() {
        return this.brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public double getPrice() {
        return this.price;
    }

    public void setPrice(double price) {
        this.price = price;
    }
}

若是用FactoryBean 的方式實現就靈活點,下例經過逗號分割符的方式一次性的爲 Car 的全部屬性指定配置值:

public class CarFactoryBean implements FactoryBean<Car> {
    private String carInfo;

    public Car getObject() throws Exception {
        Car car = new Car();
        String[] infos = carInfo.split(",");
        car.setBrand(infos[0]);
        car.setMaxSpeed(Integer.valueOf(infos[1]));
        car.setPrice(Double.valueOf(infos[2]));
        return car;
    }

    public Class<Car> getObjectType() {
        return Car.class;
    }

    public boolean isSingleton() {
        return false;
    }

    public String getCarInfo() {
        return this.carInfo;
    }

    // 接受逗號分割符設置屬性信息  
    public void setCarInfo(String carInfo) {
        this.carInfo = carInfo;
    }
}

有了這個CarFactoryBean 後,就能夠在配置文件中使用下面這種自定義的配置方式配置 Car Bean 了:

<bean id="car" class="com.baobaotao.factorybean.CarFactoryBean" p:carInfo="法拉利,400,2000000"/>

當調用getBean("car") 時, Spring 經過反射機制發現 CarFactoryBean 實現了 FactoryBean 的接口,這時 Spring容器就調用接口方法 CarFactoryBean#getObject() 方法返回。若是但願獲取 CarFactoryBean 的實例,則須要在使用getBean(beanName) 方法時在 beanName 前顯示的加上 "&" 前綴:如 getBean("&car");

 

三、區別

BeanFactory是個 Factory ,也就是 IOC 容器或對象工廠, FactoryBean 是個 Bean 。在 Spring 中,全部的Bean 都是由 BeanFactory( 也就是 IOC 容器 ) 來進行管理的。但對 FactoryBean 而言,這個 Bean 不是簡單的 Bean,而是一個能生產或者修飾對象生成的工廠 Bean, 它的實現與設計模式中的工廠模式和修飾器模式相似。

=====END=====

相關文章
相關標籤/搜索