2013-12-20 81 views
2

我得到空指針異常。我猜猜測二次注射不起作用。我試圖查看服務器日誌,但無法找到根本原因。 任何幫助,將不勝感激。sessionFactory bean:使用setter注入時的空指針異常

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

     <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" /> 

     <!-- Database Configuration --> 

    <import resource="DataSource.xml"/> 
    <import resource="HibernateSessionFactory.xml"/> 

    <!-- Beans Declaration --> 
    <import resource="UsersBean.xml"/> 

</beans> 

DataSource.xml

<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> 

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
    <property name="driverClassName" value="com.mysql.jdbc.Driver" /> 
    <property name="url" value="jdbc:mysql://localhost:3306/test;create=true" /> 
    <property name="username" value="root" /> 
    <property name="password" value="deflection" /> 
    </bean> 

</beans> 

UsersBean.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:context="http://www.springframework.org/schema/context" 
    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">  


     <context:annotation-config/> 
    <bean id="mainAction" class="com.riteshsangwan.integration.MainAction"> 

    </bean> 
</beans> 

ModelClass:

package com.riteshsangwan.integration; 

import java.io.Serializable; 
import javax.persistence.Entity; 
import javax.persistence.GeneratedValue; 
import javax.persistence.GenerationType; 
import javax.persistence.Id; 

@Entity 
public class ModelClass implements Serializable { 

    @Id @GeneratedValue(strategy = GenerationType.AUTO) 
    private long mid; 
    private String name; 
    private String email; 

    public long getMid() { 
     return mid; 
    } 

    public void setMid(long mid) { 
     this.mid = mid; 
    } 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public String getEmail() { 
     return email; 
    } 

    public void setEmail(String email) { 
     this.email = email; 
    } 


} 

MainAction.java

package com.riteshsangwan.integration; 

import com.opensymphony.xwork2.ActionSupport; 
import org.hibernate.Session; 
import org.hibernate.SessionFactory; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Repository; 

@Repository 
public class MainAction extends ActionSupport 
{ 
    @Autowired 
    private SessionFactory sessionFactory; 

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

    @Override 
    public String execute(){ 
     ModelClass m=new ModelClass(); 
     m.setName("Ritesh"); 
     m.setEmail("[email protected]"); 
     Session s=sessionFactory.openSession(); 
     s.beginTransaction(); 
     s.save(m); 
     s.getTransaction().commit(); 
     s.close(); 
     return SUCCESS; 
    } 
} 

EDITS:

HibernateSessionFactory.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" 
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> 

<!-- Hibernate session factory --> 
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" scope="singleton"> 

    <property name="dataSource"> 
     <ref bean="dataSource"/> 
    </property> 
    <property name="packagesToScan" value="com.riteshsangwan.integration" /> 

    <property name="hibernateProperties"> 
     <props> 
     <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop> 
     <prop key="hibernate.show_sql">true</prop> 

     </props> 
    </property> 

    </bean> 
</beans> 

pom.xml

<dependencies> 
    <dependency> 
      <groupId>junit</groupId> 
      <artifactId>junit</artifactId> 
      <version>4.11</version> 
      <scope>test</scope> 
     </dependency> 

     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-core</artifactId> 
      <version>4.0.0.RELEASE</version> 
     </dependency> 

     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-web</artifactId> 
      <version>4.0.0.RELEASE</version> 
     </dependency> 

     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-orm</artifactId> 
      <version>4.0.0.RELEASE</version> 
     </dependency> 

     <dependency> 
      <groupId>org.apache.struts</groupId> 
      <artifactId>struts2-core</artifactId> 
      <version>2.3.16</version> 
     </dependency> 

     <dependency> 
      <groupId>org.hibernate</groupId> 
      <artifactId>hibernate-core</artifactId> 
      <version>4.2.7.SP1</version> 
     </dependency> 

     <dependency> 
      <groupId>org.hibernate</groupId> 
      <artifactId>hibernate-entitymanager</artifactId> 
      <version>4.2.7.SP1</version> 
     </dependency> 

     <dependency> 
      <groupId>log4j</groupId> 
      <artifactId>log4j</artifactId> 
      <version>1.2.17</version> 
     </dependency> 
    </dependencies> 

struts.xml

<!DOCTYPE struts PUBLIC 
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" 
"http://struts.apache.org/dtds/struts-2.0.dtd"> 

<struts> 
    <package name="default" namespace="/" extends="struts-default"> 
     <action name="main" class="com.riteshsangwan.integration.MainAction"> 
      <result name="success">/index.jsp</result> 
     </action> 
    </package> 
</struts> 
+0

你在哪一行得到NPE? –

+0

@ K.C。 Session s = sessionFactory.opensession();我調試應用程序並找出sessionFactory爲空。請幫我解決這個問題我一直試圖將spring與hibernate集成一週,但仍然沒有完成 – user3108790

+0

請發佈您的HibernateSessionFactory.xml :) –

回答

0

至於你的行動豆Spring所管理的嘗試

<constant name="struts.objectFactory" value="spring" /> 

默認使用彈簧插件的struts-plugin.xml

在動作配置使用的bean id作爲一個值 class屬性

<action name="main" class="mainAction"> 

至於你使用Spring註解

包括在Spring配置

<context:component-scan base-package="com.riteshsangwan.integration"/> 

也確保了Struts spring plugin被部署。添加下面的依賴關係pom.xml

<dependency> 
    <groupId>org.apache.struts</groupId> 
    <artifactId>struts2-spring-plugin</artifactId> 
    <version>2.3.16</version> 
</dependency> 

結合實例詳細解釋你能找到here

xsd模式的版本與pom.xml中的版本不同,您應該將spring config更新爲您使用的版本。

http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 

可用於春季3.2。

+0

它解決了sessionFactory爲空的問題。但是現在,當我嘗試運行該應用程序時,似乎這些表不是在啓動時由休眠創建的。什麼可能是沒有創建表的可能原因。我檢查完整的代碼,甚至逐行調試它。一切安好。 – user3108790

+0

您是否創建了架構和用戶,您是否授予用戶在架構中創建表的權限?你用' create'? –

+0

感謝哥們你是救命的一個愚蠢的錯誤 沒有設置hbm2ddl屬性。我一直在關注本教程 http://www.mkyong.com/struts2/struts-2-spring-hibernate-integration-example/ 在本教程中hbm2ddl缺失 – user3108790