在spring中配置sessionFactory(以hibernate为例):
</pre><pre name="code" class="html"><bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<!-- 是否自动更新表项目 -->
<prop key="hibernate.hbm2ddl.auto">update</prop>
<!-- 数据库使用的方言 -->
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<!-- 是否显示产生的sql语句 -->
<prop key="hibernate.show_sql">true</prop>
<!-- 是否显示格式化产生的sql语句 -->
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>bean/User.hbm.xml</value>
</list>
</property>
</bean>
然后getBean时打印它的类型:
<pre name="code" class="java">ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
System.out.println(applicationContext.getBean("sessionFactory").getClass().getName());
会发现它的类型并不是配置文件里的:org.springframework.orm.hibernate4.LocalSessionFactoryBean,
而是:org.hibernate.internal.SessionFactoryImpl ,因为取到的并不是factory的类型,而是factory创建出的bean类型,如果要得到factory实例:
applicationContext.getBean("&essionFactory");