你好,游客 登录 注册 搜索
背景:
阅读新闻

Spring中 FactoryBean和bean

[日期:2012-07-02] 来源:Linux社区  作者:without0815 [字体:大 中 小]

1.整合spring+hibernate中,让spring接管hibernate的session工厂有一段代码

  1. <bean id="sFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
查看源码:org.springframework.orm.hibernate3.LocalSessionFactoryBean:
  1. public class LocalSessionFactoryBean extends AbstractSessionFactoryBean implements BeanClassLoaderAware  
在到AbstractSessionFactoryBean:
  1. public abstract class AbstractSessionFactoryBean  
  2.         implements FactoryBean<SessionFactory>, InitializingBean, DisposableBean, PersistenceExceptionTranslator   

发现了实现了一个factoryBean<T>,里面给了个泛型(SeesionFactory).


注:这个bean和其他的bean不一样,spring中有两种bean.
普通bean和bean工厂,即FactoryBean.  FactoryBean 返回的不是这个类的实例,其返回的是该工厂Bean的getObject()方法所返回的对象
普通的Bean直接返回指定类(配置中的class属性的值)的一个实例.


一个小例子:

  1. package org.ymm.entity;  
  2.   
  3. import org.springframework.beans.factory.FactoryBean;  
  4.   
  5. /** 
  6.  * TestBean 类实现了FactoryBean<T> 
  7.  * @author witho_000 
  8.  * 
  9.  */  
  10.   
  11. public class TestBean implements FactoryBean<Object>{  
  12.   
  13.     public Object getObject() throws Exception {  
  14.         // TODO Auto-generated method stub   
  15.         return "string类型";  
  16.     }  
  17.   
  18.     @Override  
  19.     public Class<?> getObjectType() {  
  20.         // TODO Auto-generated method stub   
  21.         return null;  
  22.     }  
  23.   
  24.     @Override  
  25.     public boolean isSingleton() {  
  26.         // TODO Auto-generated method stub   
  27.         return false;  
  28.     }  
  29.   
  30. }  

 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.      xmlns:context="http://www.springframework.org/schema/context"  
  5.      xsi:schemaLocation="http://www.springframework.org/schema/beans   
  6.          http://www.springframework.org/schema/beans/spring-beans-3.0.xsd   
  7.          http://www.springframework.org/schema/context   
  8.          http://www.sprinsgframework.org/schema/context/spring-context-3.0.xsd">   
  9.              
  10.     <!-- 测试factorybean -->  
  11.       
  12.     <bean id="factorybean1" class="org.ymm.entity.TestBean" />  
  13.           
  14. </beans>  

 
  1. package org.ym.test;  
  2.   
  3. import org.springframework.context.ApplicationContext;  
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  5. import org.ymm.entity.TestBean;  
  6.   
  7. /** 
  8.  * Tester测试类 
  9.  * @author witho_000 
  10.  * 
  11.  */  
  12.   
  13. public class Tester {  
  14.       
  15.     public static void main(String[] args) {      
  16.           
  17.         ApplicationContext cont=new ClassPathXmlApplicationContext("beans.xml");  
  18.         String s= cont.getBean("&factorybean1",String.class);  
  19.         System.out.println(s.getClass());  
  20.     }  
  21. }  
显然没有报错,返回的正是String类型。而不是spring配置的TestBean类型;

网上找到的小技巧:
String s= cont.getBean("&factorybean1",String.class);
 bean的id前加个&得到的就是TestBean

linux
相关资讯       Spring教程 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款