2013-04-24 42 views
1

使用.getCurrentSession()獲取Session時出現以下錯誤。使用Hibernate Spring的.getCurrentSession()後出錯

錯誤

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'miniVLEController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: miniVLE.service.StudentService miniVLE.controller.miniVLEController.studentService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'studentService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: miniVLE.dao.MiniVLEDAOImplementation miniVLE.service.StudentService.dao; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'miniVLEDAOImplementation' defined in file [C:\Users\1\Documents\NetBeansProjects\com3014_mini_VLE\build\web\WEB-INF\classes\miniVLE\dao\MiniVLEDAOImplementation.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [miniVLE.dao.MiniVLEDAOImplementation]: Constructor threw exception; nested exception is java.lang.NullPointerException

DAO實現:

import java.util.ArrayList; 
import java.util.List; 
import miniVLE.beans.Course; 
import miniVLE.beans.Department; 
import miniVLE.beans.Module; 
import miniVLE.beans.Student; 
import miniVLE.beans.TimeSlot; 
import org.hibernate.SessionFactory; 
import org.hibernate.criterion.Restrictions; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Repository; 

@Repository 
public class MiniVLEDAOImplementation implements MiniVLEDAO{ 

    // Used for communicating with the database 
    @Autowired 
    private SessionFactory sessionFactory; 

    /** 
    * DAO constructor filled with dummy data 
    */ 
    public MiniVLEDAOImplementation() { 
     System.out.println("*** MiniVLEDAOImplementation instantiated"); 
     Student s1 = new Student("123456","Bob","123"); 
     Department d1 = new Department("COM","Computing"); 
     Course c1 = new Course("COM3014","Web"); 
     c1.setDepartment(d1); 
     s1.setCourse(c1); 
     s1.setDept(d1); 

     // Add new student to the database 
     addStudentToDB(s1); 

    } 

    /** 
    * Gets the current session and adds new student row into the database 
    * @param student 
    */ 
    @Override 
    public void addStudentToDB(Student student) {   
     sessionFactory.getCurrentSession(); // here i got rid of full implementation as it fails on getting the current session 
    } ... 

調度小服務程序:

<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:mvc="http://www.springframework.org/schema/mvc" 
     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-3.0.xsd 
     http://www.springframework.org/schema/aop 
     http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
     http://www.springframework.org/schema/tx 
     http://www.springframework.org/schema/tx/spring-tx-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/mvc 
     http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> 

    <context:annotation-config /> 
    <context:component-scan base-package="miniVLE.controller" /> 
    <context:component-scan base-package="miniVLE.service" /> 
    <context:component-scan base-package="miniVLE.beans" /> 
    <context:component-scan base-package="miniVLE.dao" /> 

    <!-- Declare a view resolver--> 
    <bean id="viewResolver" 
      class="org.springframework.web.servlet.view.InternalResourceViewResolver" 
      p:prefix="/WEB-INF/jsp/" 
      p:suffix=".jsp" /> 


    <!-- Connects to the database based on the jdbc properties information--> 
    <bean id="dataSource" class ="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
     <property name ="driverClassName" value="com.mysql.jdbc.Driver"/> 
     <property name ="url" value="jdbc:derby://localhost:1527/minivledb"/> 
     <property name ="username" value="root"/> 
     <property name ="password" value="123" /> 
    </bean> 

    <!-- Declares hibernate object --> 
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> 
     <property name="dataSource" ref="dataSource" /> 
     <property name="hibernateProperties"> 
      <props> 
       <prop key="hibernate.dialect"> ${hibernate.dialect}</prop> 
       <prop key="hibernate.show_sql">${hibernate.show_sql}</prop> 
      </props> 
     </property> 
     <!-- A list of all the annotated bean files, which are mapped with database tables--> 
     <property name="annotatedClasses"> 
      <list> 
       <value> miniVLE.beans.Course </value> 
       <value> miniVLE.beans.Student </value> 
       <value> miniVLE.beans.Department </value> 
       <value> miniVLE.beans.Module </value> 
       <value> miniVLE.beans.TimeSlot </value> 
      </list> 
     </property> 
    </bean> 

</beans> 
+0

您需要自動裝配構造函數並將會話工廠傳遞給構造函數。 '@Autowired public MiniVLEDAOImplementation(SessionFactory sessionFactory){ \t this.sessionFactory = sessionFactory; \t //你的代碼 }' – 2013-04-24 16:21:01

回答

3

要創建您的bean,Spring首先使用您的類的無參數構造函數創建一個實例,然後使用反射來自動填寫@Autowired字段。

但是你的構造

public MiniVLEDAOImplementation() { 
    ... 
    // Add new student to the database 
    addStudentToDB(s1); 
} 

調用使用SessionFactory已經初始化之前的方法。因此它是null,當你嘗試調用一個方法時你會得到一個NullPointerException

您不能從該點將學生添加到數據庫。如果你想測試你的班級,試試單元測試。

+1

@exomen不是試圖從你的構造函數中插入數據庫中的條目。這是非常糟糕的做法。 – 2013-04-24 16:21:28

+0

好吧,我會嘗試在DAO啓動後將它添加到一個單獨的類中。 Sotirios Delimanolis感謝您的建議,目前我只是想解決它的工作原理。 – exomen 2013-04-24 16:32:52

+0

@exomen很高興我能幫到你。我建議閱讀關於spring如何管理bean並執行其自動裝配的內容,因爲它在所有Spring庫中都有使用。從長遠來看,這將有所幫助。 – 2013-04-24 16:33:33

相關問題