当前位置: 代码迷 >> Java Web开发 >> spring AnnotationSessionFactoryBean 怎么转化SessionFactory类型的
  详细解决方案

spring AnnotationSessionFactoryBean 怎么转化SessionFactory类型的

热度:276   发布时间:2016-04-16 21:46:46.0
spring AnnotationSessionFactoryBean 如何转化SessionFactory类型的?
spring 配置:
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
......

java代码:
SessionFactiory sessionFactiory = (SessionFactiory )ctx.getBean("sessionFactory"); 

问题:
今天看了下AnnotationSessionFactoryBean的源码,里面竟然没有实现SessionFactory接口,
请问它是如何赋值给SessionFactory的?
望高手解答问题!

------解决思路----------------------
是的,new AnnotationSessionFactoryBean() instanceof SessionFactory 是false

因为
首先看继承 
AnnotationSessionFactoryBean extends LocalSessionFactoryBean extends AbstractSessionFactoryBean implements
FactoryBean<SessionFactory>
实现FactoryBean接口的类,通过getBean()方法时,返回的并不是bean定义的对象,而是getObject()返回的对象

AbstractSessionFactoryBean类实现了getObject()方法 返回的是SessionFactory
关键看getObject()方法
  相关解决方案