BeanFactory与FactoryBean的区别

BeanFactory

BeanFactory是spring中IOC容器的基础形式,给具体的IOC容器的实现提供了规范。正如ApplicationContext就是他派生而来,以及常用的具体实现ClassPathXmlApplicationContext等,作为框架中生产和管理bean的容器。

职责:实例化,定位,配置应用程序中的对象以及建立对象间的依赖。

FactoryBean

通常情况下,spring通过<bean>标签中的class与property等实例化bean,某些情况下,使用这种传统配置实例化bean很繁琐,配置方式灵活度有限,此时使用编码的方式可能会得到一个简单方案。spring为此提供了一个FactoryBean的工厂类接口,通过实现该接口定制化bean的逻辑。

FactoryBean以bean结尾,说明他也是一个bean,但是有别于普通的bean:他是实现了FactoryBean<T>接口的bean,通过id获取FactoryBean.getObject()返回的对象。而不是FactoryBean<T>本身,若想获取他本身需要在id前加一个&。

下面代码加深理解:

<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"  
             xmlns:aop="http://www.springframework.org/schema/aop"  
             xmlns:tx="http://www.springframework.org/schema/tx"  
             xsi:schemaLocation="http://www.springframework.org/schema/beans  
                     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
                     http://www.springframework.org/schema/context  
                     http://www.springframework.org/schema/context/spring-context-3.0.xsd  
                     http://www.springframework.org/schema/aop  
                     http://www.springframework.org/schema/aop/spring-aop-3.0.xsd  
                     http://www.springframework.org/schema/tx  
                     http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">  
      
 <bean id="student" class="com.spring.bean.Student">    
  <property name="name" value="zhangsan" />    
 </bean>    
   
 <bean id="school" class="com.spring.bean.School">    
 </bean>   
   
 <bean id="factoryBeanPojo" class="com.spring.bean.FactoryBeanPojo">    
    <property name="type" value="student" />  
 </bean>   
</beans>
public class FactoryBeanPojo implements FactoryBean{  
    private String type;  
  
    @Override  
    public Object getObject() throws Exception {  
        if("student".equals(type)){  
            return new Student();             
        }else{  
            return new School();  
        }  
    }  
    @Override  
    public Class getObjectType() {  
        return School.class;  
    }  
    @Override  
    public boolean isSingleton() {  
        return true;  
    } 
    public String getType() {  
        return type;  
    }  
    public void setType(String type) {  
        this.type = type;  
    }  
}  


public class FactoryBeanTest {  
    public static void main(String[] args){  
        String url = "com/spring/config/BeanConfig.xml";  
        ClassPathXmlApplicationContext cpxa = new ClassPathXmlApplicationContext(url);  
        Object school=  cpxa.getBean("factoryBeanPojo");  
        FactoryBeanPojo factoryBeanPojo= (FactoryBeanPojo) cpxa.getBean("&factoryBeanPojo");  
        System.out.println(school.getClass().getName());  
        System.out.println(factoryBeanPojo.getClass().getName());  
    }  
}  

输出:

INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@35b793ee: defining beans [student,school,factoryBeanPojo]; root of factory hierarchy  
com.spring.bean.Student  
com.spring.bean.FactoryBeanPojo  

以上说明,factoryBean的灵活性以及他的特性getBean('id'),是获取getObject的实例;getBean('&id'),获取的是factoryBean的实例

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章