[html]
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="https://www.springframework.org/schema/beans"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="https://www.springframework.org/schema/aop"
xmlns:tx="https://www.springframework.org/schema/tx"
xsi:schemaLocation="https://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
https://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd
https://www.springframework.org/schema/tx https://www.springframework.org/schema/tx/spring-tx.xsd">
<!–SessionFactory Transaction******************************************–>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation"
value="classpath:hibernate.cfg.xml">
</property>
</bean>
——————————————————————————
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="https://www.springframework.org/schema/beans"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="https://www.springframework.org/schema/aop"
xmlns:tx="https://www.springframework.org/schema/tx"
xsi:schemaLocation="https://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
https://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd
https://www.springframework.org/schema/tx https://www.springframework.org/schema/tx/spring-tx.xsd">
<!–SessionFactory Transaction******************************************–>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation"
value="classpath:hibernate.cfg.xml">
</property>
</bean>
——————————————————————————
</beans>
問:
org.springframework.orm.hibernate3.LocalSessionFactoryBean;
這個類型根本沒有getCurrentSession的方法。
而productDAO中的sessionFactory是org.hibernate.SessionFactory,這中間的類型是怎麼轉換的啊?
試瞭一下往數據庫保存是成功的,就是想不通這個類型的轉換,我看LocalSessionFactoryBean也沒有實現SessionFactory這個接口啊? www.aiwalls.com
難道是LocalSessionFactoryBean的getObject()方法?
答:
正是如此!LocalSessionFactoryBean實現瞭org.springframework.beans.factory.FactoryBean接口, spring在裝配的時候, 如果發現實現瞭org.springframework.beans.factory.FactoryBean接口, 就會使用FactoryBean#getObject() 方法返回的對象裝配,具體的可以看下文檔.
如果你想拿到LocalSessionFactoryBean實例, 在id前面加個'&'就可以瞭,在你的配置文件中BeanFactory.getBean('&sessionFactory')拿到的就是LocalSessionFactoryBean的實例.
作者:Tender001