2013-02-19 104 views
0

我竭力要弄清楚確切的問題,爲什麼空指針錯誤被拋出的SessionFactory當我嘗試自動線的sessionFactory從春豆XML。有人可以幫我解決這個問題。我是新來的春天和部署JBoss服務器空指針錯誤,而使用的SessionFactory Hibernate的4春天3.2

這裏上的應用程序是Spring-config.xml中

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:c="http://www.springframework.org/schema/c" 
    xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" 
    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.2.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd"> 

    <context:annotation-config /> 
    <context:component-scan base-package="org.kp.db.dao.impl" /> 


    <bean id="myDataSource" 
     class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
     <property name="driverClassName" value="com.mysql.jdbc.Driver" /> 
     <property name="url" value="jdbc:mysql://localhost:3306/ebm" /> 
     <property name="username" value="root" /> 
     <property name="password" value="root" /> 
    </bean> 

    <bean id="sessionFactory" 
     class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 
     <property name="dataSource" ref="myDataSource" /> 
     <property name="configLocation" value="classpath:/hibernate.cfg.xml" /> 
    </bean> 

    <tx:annotation-driven transaction-manager="transactionManager" /> 

    <bean id="transactionManager" 
     class="org.springframework.orm.hibernate4.HibernateTransactionManager"> 
     <property name="sessionFactory" ref="sessionFactory"></property> 
    </bean> 
</beans> 

這裏是Hiberante.cfg.xml我放在WebContent文件夾

<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 
<hibernate-configuration> 
    <session-factory> 
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> 
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> 
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/ebm</property> 
    <property name="hibernate.connection.username">root</property> 
    <property name="hibernate.connection.password">root</property> 
    <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property> 
    <property name="hibernate.show_sql">true</property> 
    <property name="hibernate.hbm2ddl.auto">update</property> 
    <mapping resource="org/kp/db/model/Actioncode.hbm.xml"/> 
    <mapping class="org.kp.db.model.TblEBMFieldDetails"/> 
    <mapping class="org.kp.db.model.TbleOLI"/> 
    </session-factory> 
</hibernate-configuration> 

並在其中我想訪問SessionFactory的DAO類是

package org.kp.db.dao.impl; 

import org.hibernate.Session; 
import org.hibernate.SessionFactory; 
import org.kp.db.dao.ActionCodeDao; 
import org.kp.db.model.Actioncode; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Repository; 
import org.springframework.transaction.annotation.Propagation; 
import org.springframework.transaction.annotation.Transactional; 

/** 
* 
* @author Administrator 
*/ 
@Repository 
public class ActionCodeDaoImpl implements ActionCodeDao{ 

    @Override 
    public String[] getActionCodes() { 
     throw new UnsupportedOperationException("Not supported yet."); 
    } 


    @Autowired(required=true) 
    SessionFactory sessionFactory; 

    public SessionFactory getSessionFactory() { 
     return sessionFactory; 
    } 


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

    @Override 
    @Transactional(readOnly=true,propagation=Propagation.REQUIRED) 
    public String getActionCode(int id) { 

     //ApplicationContext context; 

     Session session = sessionFactory.getCurrentSession(); 
     Actioncode actionCode=null; 
     actionCode = (Actioncode)session.get(Actioncode.class, 1); 

     return actionCode.getActionName(); 
    } 

} 
+0

難道你還堆棧跟蹤添加到您的文章? – reporter 2013-02-19 08:58:52

+0

檢查sessionFactory和session是否爲空。 – BobTheBuilder 2013-02-19 09:02:51

+0

嗨是的SessionFactory是空的,因此它拋出空指針異常。我不知道如果MySQL連接不存在,會發生什麼,在這個時候會不會有SessionFactory的空,我怎麼能確認,請查收爲servlet的服務器日誌 – 2013-02-19 09:38:02

回答

1

我已經定勢d的問題,而不是訪問我使用getter方法的對象,它的工作原理,不知道它背後的真正概念是什麼。

public String getActionCode(int id) { 

    //ApplicationContext context; 

    Session session = getSessionFactory().getCurrentSession(); 
    Actioncode actionCode=null; 
    actionCode = (Actioncode)session.get(Actioncode.class, 1); 

    return actionCode.getActionName(); 
} 
1
@Named ("kpAction") 
public class Actions { 
    public String getActionCode() { 
     ActionCodeDao action =new ActionCodeDaoImpl(); 
     String str = action.getActionCode(1); System.out.println(str); return str; 
    } 
} 

當您運行這行代碼:ActionCodeDao action =new ActionCodeDaoImpl();你不是由Spring管理,因此不會包含自動裝配Autowired依賴的對象。

我會說這就是爲什麼你看到空指針。

但是你說你現在已經修好了,不知道你是否在不經意間改變了這個爲好,如果不是林不知道你已是如何解決它:)