Spring BeanFactory和FactoryBeanjava
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); }
通常状况下,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=====