简体   繁体   中英

sessionFactory bean : null pointer exception while using setter injection

I am getting null pointer exception. The setter injection is not working I guess. I tried to look into server logs but not being able to find the root cause. Any help would be appreciated.

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("sangwan.ritesh@yahoo.in");
        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>

As far as your actions bean managed by spring try

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

which is used by default in struts-plugin.xml of the spring plugin.

in the action config use bean id as a value for class attribute

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

As far as you are using Spring annotations include in the spring config

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

also make sure the Struts spring plugin is deployed. Add the following dependency to pom.xml :

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

Detailed explanation with examples you could find here .

The version of xsd schema is not the same as in pom.xml, you should update spring config to the version you use.

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

is available for spring 3.2.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM