LocalSessionFactoryBean

LocalSessionFactoryBean

自动装配进行得很完美,并且一切正常。
但是我需要知道如何使用HibernateTransactionManagerLocalSessionFactoryBean中注入会话工厂
LocalSessionFactoryBean类和SessionFactory接口之间没有关系。

PFB我在项目中使用的代码

<beans:bean id="transactionManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <beans:property name="sessionFactory"
            ref="localSessionFactoryBean" />
    </beans:bean>
<beans:bean id="localSessionFactoryBean"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <beans:property name="dataSource" ref="dataSource" />
                <beans:property name="hibernateProperties">
            <beans:props>
                <beans:prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect
                </beans:prop>
                <beans:prop key="hibernate.show_sql">true</beans:prop>
            </beans:props>
        </beans:property>
    </beans:bean>


在上面的代码中,我为LocalSessionFactoryBean中的sessionFactory注入HibernateTransactionManager

请详细解释如何使用注释驱动在sessionFactory中从HibernateTransactionManager初始化LocalSessionFactoryBean

最佳答案

LocalSessionFactoryBean实现FactoryBean<SessionFactory>。因此,当Spring将sessionFactory设置为HibernateTransactionManager时,它将调用getObject()LocalSessionFactoryBean方法并获取SessionFactory

public class LocalSessionFactoryBean extends HibernateExceptionTranslator
        implements FactoryBean<SessionFactory> {

    public SessionFactory getObject() {
        return this.sessionFactory;
    }

}


What's a FactoryBean?

07-26 02:46