配置文件
spring的,其中hibernate.cfg.xml能用,只用hibernate测试没问题,所以那个文件不贴了
<?xml version="1.0"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> 
 <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
</bean>
 
 
 <!-- 声明事物,做用就是对一系列操作在运行时错误的情况能回滚 -->
 <!-- 事物管理 -->
 <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  <property name="sessionFactory" ref="sessionFactory" />
 </bean>
 <!-- 事物拦截器 -->
 <bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
  <property name="transactionManager" ref="transactionManager" />
  <!-- 下面定义事物传播属性 -->
  <property name="transactionAttributes">
   <props>
    <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
    <prop key="*">PROPAGATION_REQUIRED</prop>
   </props>
  </property>
 </bean>
 <!-- 业务实例动态代理 -->
 <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
  <property name="beanNames">
   <!-- 下面是所有要创建代理事物的bean -->
   <list>
    <value>service</value>
   </list>
  </property>
  <!-- 下面定义 所需的事物拦截器 -->
  <property name="interceptorNames">
   <list>
    <value>transactionInterceptor</value>
   </list>
  </property>
 </bean>
 
 <!-- 业务逻辑的bean -->
 <bean id="service" class="com.lp.logic.impl.ServiceImpl">
  <property name="loginDao">
   <ref bean="loginDao"/>
  </property>
 </bean>
 
 <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
  <property name="sessionFactory">
   <ref bean="sessionFactory"/>
  </property>
  <property name="allowCreate">
   <value>true</value>
  </property>
</bean>
 <bean id="loginDao" class="com.lp.dao.impl.LoginDaoImpl">
 <property name="hibernateTemplate" ref="hibernateTemplate" />
 <property name="sessionFactory" ref="sessionFactory" />
 </bean>
 
 <bean id="baseAction" class="com.lp.action.base.BaseAction" scope="prototype">
  <property name="ser" ref="service" />
 </bean>
</beans>

解决方案 »

  1.   

    SessionFactory sf = getSessionFactory();  谁的getSessionFactory();??既然Spring配置文件中已经申明了这个bean,难道不是自动注入的吗? 
      

  2.   

    dao层继承自hibernateDaoSupport
    里面用getHibernateTemplate()方法操作吧,但是那个方法就返回null,所以我就获取了一下SessionFactory,结果也是null
      

  3.   

    改用annotation吧 ,用起来舒服的多啊!
      

  4.   

      你的spring配置文件中连接数据库的dataSource呢 ?  没看见么
      

  5.   

    如果你的配置文件没有错 并且你是通过Spring的容器获得对象的话 应该不会为NULL
      

  6.   

    为什么不用spring的配置文件获得sessionFactory               <bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="configLocation"
    value="classpath:hibernate.cfg.xml">
    </property>
    </bean>
      

  7.   

    8楼,用spring的配置文件获得sessionFactory也是那样的,我试过了...
      

  8.   

    要对注入的类进行这样的操作,如果new个对象出来进行这样的操作结果就为null
      

  9.   

    SessionFactory sf=new Configuration().configure().buildSessionFactory();