如何解决Spring sessionfactory中的Nullpointerexception问题

时间:2013-10-03 11:38:54

标签: java spring-mvc

在我的dao文件中,

sessionFactory是null,我在我的春天配置了sessionFactory xml file

applicationContext文件

<?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:p="http://www.springframework.org/schema/p"
 xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-3.0.xsd
 http://www.springframework.org/schema/tx
 http://www.springframework.org/schema/tx/spring-tx.xsd">

 <context:annotation-config />
<context:component-scan base-package="com.tsc.erp" />
 <tx:annotation-driven transaction-manager="transactionManager" />

 <bean id="transactionManager"
 class="org.springframework.orm.hibernate4.HibernateTransactionManager" p:sessionFactory-ref="sessionFactory" />

 <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
 <property name="configLocation" value="classpath:hibernate.cfg.xml" />
 <property name="packagesToScan" value="com.tsc.erp.dao"/>
 <property name="dataSource" ref="dataSource" />
 <property name="hibernateProperties">
 <props>
 <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
 <prop key="hibernate.show_sql">false</prop>

 </props>
 </property>
 </bean>

    <!--  <bean id="datasourcePropertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location">
            <value>classpath:datasource.properties</value>
        </property>
        <property name="ignoreUnresolvablePlaceholders">
            <value>true</value>
        </property>
    </bean> -->

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="refresh">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/tschrms" />
        <property name="username" value="root" />
        <property name="password" value="root" />
    </bean>



<bean id="userdao" class="com.tsc.erp.dao.TscUserHome"  >
<property name="sessionFactory"><ref bean="sessionFactory"/></property>

</bean>


</beans>

DAO文件

package com.tsc.erp.dao;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;


import org.springframework.stereotype.Repository;

@Repository
public class TscUserHome implements Tscuserdao{

    @Autowired
    private SessionFactory sessionFactory;



    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public void setSessionFactory(SessionFactory sessionFactory) {

            this.sessionFactory = sessionFactory;  
    }





    public List showEmployee() {
          System.out.println(sessionFactory);
          List emp = null;
          Session session = sessionFactory.openSession();
        Transaction tx = null;
          try{
             tx = session.beginTransaction();

        emp = session.createQuery("from tsc_user").list();

          tx.commit();
          }

          catch (HibernateException e) {
             if (tx!=null) 
                 {tx.rollback();
             e.printStackTrace(); 
          }

          }
          catch(NullPointerException ne)
          {

              ne.getStackTrace();
          }

          finally {

          }
          session.close(); 
            sessionFactory.close(); 
        return emp;


      }




}

web.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>esystem</display-name>


  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>

  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

 <listener>
    <listener-class>
      org.springframework.web.context.ContextLoaderListener
    </listener-class>
  </listener>


  <welcome-file-list>

    <welcome-file>loginpage.jsp</welcome-file>
  </welcome-file-list>
</web-app>

服务文件(我在哪里使用我的TscUserHome)

package com.tsc.erp.services;

import java.util.ArrayList;
import java.util.List;

import com.tsc.erp.dao.TscUserHome;

public class Loginservice
{
     private  List parti_user = new ArrayList();
    private TscUserHome logindao=new TscUserHome();

    public  void authentication()
    {
        try{
            System.out.println("first");
        parti_user =logindao.showEmployee();

        System.out.println(parti_user);
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }




}

2 个答案:

答案 0 :(得分:0)

你必须在你的dao bean中注入sessionFactory对象。例如

<bean id="MyDAO" class="com.mine.MyDAO">
<property name="sessionFactory" ref="id of your sessionFactory bean" />
</bean>

答案 1 :(得分:0)

首先检查你是否在DAO中创建了sessionFactory的getter方法,否则请分享你创建sessionFactory和DAO的xml。