主题 : 请问如何注入 sessionFactory
级别: 童生
UID: 1796
积分:27 加为好友
威望: 0 精华: 0
主题:2 回复:19
注册时间:2010-01-19
在线时长:0
1#   发表于:2010-07-09 09:41:30  IP:218.247.*.*
我写了一个测试类.代码如下
public class Test{

private  SessionFactory sessionFactory ;
@Autowired
public void setSessionFactory( SessionFactory sessionFactory ) {
this.sessionFactory = sessionFactory ;
}

public  Session getSession(){
return sessionFactory.getCurrentSession() ;
}


}
但是无法注入sessionFactory,调用getSession()方法就报空指针错.
我也试着用一个类继承BaseDaoImpl类,但是还是调用getSession()还是报错.

请问我该如何做才可以拿到sessionFactory  ?
级别: 童生
UID: 1796
积分:27 加为好友
威望: 0 精华: 0
主题:2 回复:19
注册时间:2010-01-19
在线时长:0
2#   发表于:2010-07-09 09:44:33  IP:218.247.*.*
补充一下:  这个是 applicationContext.xml  文件内容


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"
default-lazy-init="true">

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
     <property name="location">
     <value>classpath:jdbc.properties</value>
</property>
</bean>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="autoCommitOnClose" value="true"/>
<property name="checkoutTimeout" value="${cpool.checkoutTimeout}"/>
<property name="initialPoolSize" value="${cpool.minPoolSize}"/>
<property name="minPoolSize" value="${cpool.minPoolSize}"/>
<property name="maxPoolSize" value="${cpool.maxPoolSize}"/>
<property name="maxIdleTime" value="${cpool.maxIdleTime}"/>
<property name="acquireIncrement" value="${cpool.acquireIncrement}"/>
<property name="maxIdleTimeExcessConnections" value="${cpool.maxIdleTimeExcessConnections}"/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mappingDirectoryLocations">
<list>
<value>classpath:/com/jeecms/core/entity</value>
<value>classpath:/com/jeecms/cms/entity</value>
<value>classpath:/com/jeecms/article/entity</value>
<value>classpath:/com/jeecms/download/entity</value>
<value>classpath:/com/jeecms/auxiliary/entity</value>
</list>
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect
hibernate.query.substitutions=true 1, false 0
hibernate.jdbc.batch_size=20
hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider
hibernate.cache.provider_configuration_file_resource_path=/ehcache-hibernate.xml
</value>
</property>
<property name="entityInterceptor">   
<ref local="treeInterceptor"/>
</property>
</bean>

<bean id="treeInterceptor" class="com.jeecms.common.hibernate3.TreeIntercptor"/>
<!-- 事务配置 -->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>

<!-- 使用annotation 自动注册bean,并检查@Required,@Autowired的属性已被注入 -->
<context:component-scan base-package="com.jeecms" />

<!-- 使用annotation定义事务 -->
<tx:annotation-driven transaction-manager="txManager" />
</beans>
1 共1页