spring SqlSessionFactoryBean创建SqlSessionFactory

原文地址:http://blog.csdn.net/wdyr321/article/details/11763979

Application.xml

[xml]  view plain  copy
  1. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
  2.   <property name="dataSource" ref="dataSource" />  
  3.   <property name="configLocation" value="classpath:SQL_MAP_SERVICE_CONFIG.xml" />  
  4. bean>  
创建SqlSessionFactory
[java]  view plain  copy
  1. ApplicationContext ctx = new ClassPathXmlApplicationContext("APPLICATION.xml");  
  2. SqlSessionFactory sqlSessionFactory=(SqlSessionFactory) ctx.getBean("sqlSessionFactory");  

为什么返回的是SqlSessionFactory对象而不是SqlSessionFactoryBean
首先spring在初始化的时候会将所有创建的单例以Map的形式放入singletonObjects,同时调用FactoryBean的getObject()将返回的对象以Map的形式放入factoryBeanObjectCache

如{sqlSessionFactory=org.apache.ibatis.session.defaults.DefaultSqlSessionFactory@d997f9}


然后getBean的时候,spring利用getSingleton从singletonObjects获取单例(这里是SqlSessionFactoryBean对象),判断是否实现了FactoryBean接口,若实现了就从factoryBeanObjectCache利用beanname(这里是sqlSessionFactory)重新获取,若未实现则返回getSingleton获取的单例

初始化的源码有些复杂,理不清,下面仅仅是getBean的源码:

1、首先调用的是ClassPathXmlApplicationContext父类AbstractRefreshableApplicationContext的父类AbstractApplicationContext的getBean()。

[java]  view plain  copy
  1. public Object getBean(String name) throws BeansException {  
  2.     return getBeanFactory().getBean(name);  
  3. }  
2、调用AbstractRefreshableApplicationContext的getBeanFactory()返回ConfigurableListableBeanFactory接口实现类DefaultListableBeanFactory
[java]  view plain  copy
  1.       
  2. @Override(重写了AbstractApplicationContext的getBeanFactory抽象方法)  
  3.     public final ConfigurableListableBeanFactory getBeanFactory() {  
  4.         synchronized (this.beanFactoryMonitor) {  
  5.             if (this.beanFactory == null) {  
  6.                 throw new IllegalStateException("BeanFactory not initialized or already closed - " +  
  7.                         "call 'refresh' before accessing beans via the ApplicationContext");  
  8.             }  
  9.             return this.beanFactory;  
  10.         }  
  11.     }  
3、调用DefaultListableBeanFactory的父类AbstractBeanFactory的geBean()
[java]  view plain  copy
  1.       
  2. public Object getBean(String name) throws BeansException {  
  3.         return doGetBean(name, nullnullfalse);  
  4.     }  
4、调用AbstractBeanFactory的doGetBean()
[java]  view plain  copy
  1.       
  2. protected  T doGetBean(  
  3.             final String name, final Class requiredType, final Object[] args, boolean typeCheckOnly)  
  4.             throws BeansException {  
  5.   
  6.         final String beanName = transformedBeanName(name);  
  7.         Object bean;  
  8.   
  9.         // Eagerly check singleton cache for manually registered singletons.  
  10.         Object sharedInstance = getSingleton(beanName);//根据bean路径获取单例  
  11.         if (sharedInstance != null && args == null) {  
  12.             if (logger.isDebugEnabled()) {  
  13.                 if (isSingletonCurrentlyInCreation(beanName)) {  
  14.                     logger.debug("Returning eagerly cached instance of singleton bean '" + beanName +  
  15.                             "' that is not fully initialized yet - a consequence of a circular reference");  
  16.                 }  
  17.                 else {  
  18.                     logger.debug("Returning cached instance of singleton bean '" + beanName + "'");  
  19.                 }  
  20.             }  
  21.             bean = getObjectForBeanInstance(sharedInstance, name, beanName, null);  
  22.         }  
5、调用AbstractBeanFactory的getObjectForBeanInstance(),判断是否实现FactoryBean接口,若未实现则返回上一步创建的单例
[java]  view plain  copy
  1.       
  2. protected Object getObjectForBeanInstance(  
  3.             Object beanInstance, String name, String beanName, RootBeanDefinition mbd) {  
  4.   
  5.         // Don't let calling code try to dereference the factory if the bean isn't a factory.  
  6.         if (BeanFactoryUtils.isFactoryDereference(name) && !(beanInstance instanceof FactoryBean)) {  
  7.             throw new BeanIsNotAFactoryException(transformedBeanName(name), beanInstance.getClass());  
  8.         }  
  9.   
  10.         // Now we have the bean instance, which may be a normal bean or a FactoryBean.  
  11.         // If it's a FactoryBean, we use it to create a bean instance, unless the  
  12.         // caller actually wants a reference to the factory.  
  13.         if (!(beanInstance instanceof FactoryBean) || BeanFactoryUtils.isFactoryDereference(name)) {  
  14.             return beanInstance;  
  15.         }//判断是否实现FactoryBean接口  
  16.   
  17.         Object object = null;  
  18.         if (mbd == null) {  
  19.             object = getCachedObjectForFactoryBean(beanName);//从factoryBeanObjectCache中取出FactoryBean创建的对象  
  20.         }  
  21.         if (object == null) {  
  22.             // Return bean instance from factory.  
  23.             FactoryBean factory = (FactoryBean) beanInstance;  
  24.             // Caches object obtained from FactoryBean if it is a singleton.  
  25.             if (mbd == null && containsBeanDefinition(beanName)) {  
  26.                 mbd = getMergedLocalBeanDefinition(beanName);  
  27.             }  
  28.             boolean synthetic = (mbd != null && mbd.isSynthetic());  
  29.             object = getObjectFromFactoryBean(factory, beanName, !synthetic);  
  30.         }  
  31.         return object;  
  32.     }  

你可能感兴趣的:(spring SqlSessionFactoryBean创建SqlSessionFactory)