在applicationContext.xml配置文件中其中对sessionFactory的定义如下
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mappingLocations">
<list>
<value>classpath*:/com/lfsy/crm/vo/*.hbm.xml</value>
<value>classpath*:/com/lfsy/crm/vo/business/*.hbm.xml</value>
<value>classpath*:/com/lfsy/crm/vo/Common/*.hbm.xml</value>
<value>classpath*:/com/tgb/crm1/model/ControlPanel/*.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.cache.provider_class">
org.hibernate.cache.EhCacheProvider
</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop><!-- 这句话用于spring根据映射文件生成表 ,不会将存在的表删除掉-->
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.use_outer_join">true</prop>
<prop key="hibernate.cache.use_query_cache">false</prop>
</props>
</property>
</bean>
然而在程序中通过(ApplicationContext)context.getBean("sessionFactory")得到的类却是
org.hibernate.impl.SessionFactoryImpl
弄了半天才知道LocalSessionFactoryBean实现了org.springframework.beans.factory.FactoryBean接
口, spring在装配的时候,
如果发现实现了org.springframework.beans.factory.FactoryBean接口,
就会使用FactoryBean#getObject() 方法返回的对象装配,具体的可以看下文档.
如果你想拿到LocalSessionFactoryBean实例,
在id前面加个'&'就可以了,在你的配置文件中BeanFactory.getBean('&sessionFactory')拿到的
就是LocalSessionFactoryBean的实例.
以上内容来自网络:http://blog.sina.com.cn/s/blog_56d8ea9001013b3t.html
本文详细介绍了在Spring框架中如何配置SessionFactory,特别是LocalSessionFactoryBean的使用方式及其与SessionFactory的关系。文章解释了如何通过配置文件指定Hibernate映射文件的位置以及Hibernate的各种属性设置。
2564

被折叠的 条评论
为什么被折叠?



