请求处理失败;嵌套异常是java.lang.NullPointerException Spring-hibernate集成

时间:2014-11-05 20:36:07

标签: java spring hibernate spring-mvc nullpointerexception

我是Spring和hibernate的新手。我创建了一个项目,这就是它的功能。 应用程序在JSP中具有表单元素,在按下提交后重定向到Controller类。控制器调用服务层,服务层又调用DAO,最后将记录添加到数据库。我遇到的问题是,在Controller类中,我可以接收请求参数(POST),但问题是调用服务层的addMember()时出现问题。问题在于对象上的Null Pointer Exception。你们可以查看代码并提供建议吗?

这是我的控制器类LoginController

package com.online.site.member;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.online.site.Member.Member;
import com.online.site.Member.MemberBoImpl;

@Controller
@RequestMapping("/Login.htm")
public class LoginController {
@Autowired
//@Qualifier(value="memberServiceImp")
private MemberBoImpl memberServiceImpl;


    public void setMemberServiceImp(MemberBoImpl memberServiceImpl) {
    this.memberServiceImpl = memberServiceImpl;
    System.out.println(memberServiceImpl.toString());
}

    @RequestMapping(method=RequestMethod.POST)
    public String executeLogin(@ModelAttribute("Login") Member member, BindingResult result)
    {
        /* call member record fetch method from service layer.
         * if login successful redirect to home page. If not a member 
         * call create member method from service layer and redirect to create account page*/
        memberServiceImpl.addMember(member);
        //memberServiceImp.getMemberDetails(member.getMemberId());
        return "CreateMemberProfile";
    }

    @RequestMapping(method=RequestMethod.GET)
    public String initializeMemberLoginForm(ModelMap model)
    {
        Member member = new Member();
        model.addAttribute("Login", member);
        return "Login";


    }
}

这是服务类

@Service
public class MemberBoImpl implements MemberInterface{
//@Autowired
    private MemberDaoImpl memberDaoImpl;
/*private HibernateTemplate hibernateTemplate;

    public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
        this.hibernateTemplate = hibernateTemplate;}*/

    public void setMemberDaoImpl(MemberDaoImpl memberDaoImpl) {
        this.memberDaoImpl = memberDaoImpl;
        System.out.println("in doa setter");
    }

    public boolean addMember(Member member) {
        //hibernateTemplate.save(member);
        System.out.println("in bo impl"+member.getMemberId());
        System.out.println(memberDaoImpl.toString());
        memberDaoImpl.addMember(member);
        return false;
    }
}

这是DAO类

package com.online.site.Member;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;

public class MemberDaoImpl implements MemberInterface{
@Autowired
    private HibernateTemplate hibernateTemplate;

    public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
        this.hibernateTemplate = hibernateTemplate;
        System.out.println("in hibernate setter");}

    public boolean addMember(Member member) {
        System.out.println("dao impl");
        hibernateTemplate.save(member);
        return false;
    }


}

这里是config.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"
    xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
        http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.2.xsd">

    <context:annotation-config/>

<!--    <context:component-scan base-package="com.online.site"></context:component-scan> -->

    <bean id="memberServiceImpl" class="com.online.site.Member.MemberBoImpl"
        scope="singleton" autowire="byType">
     <property name="memberDaoImpl" ref="memberDaoImpl"></property>
 <!--  <property name="hibernateTemplate" ref="hibernateTemplate"></property>-->
    </bean>

    <bean id="member" class="com.online.site.Member.Member"/>

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

    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">

        <property name="dataSource" ref="dataSource"></property>

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

        <property name="annotatedClasses">
            <list>
                <value>com.online.site.Member.Member</value>
            </list>
        </property>
    </bean>

    <bean name="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
     <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>

       <bean id="memberDaoImpl" class="com.online.site.Member.MemberDaoImpl" autowire="byType">
    <property name="hibernateTemplate" ref="hibernateTemplate"></property>
    </bean>
</beans>

这是dispatcher-servlet.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-2.5.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

    <import resource="classpath*:/onlineShop-config.xml" />


    <!-- step eight: http://localhost:8080/Spring3MVCDemo/customer.htm -->
<context:component-scan base-package="com.online.site,com.online.site.Billing,com.online.site.Member"></context:component-scan>


    <!-- step 12 public String initializeCustomerForm(ModelMap model) { returning 
        string value return "createCustomer" createCustomer prefix and suffix will 
        be added by the InternalResourceViewResolver http://localhost:8080/Spring3MVCDemo/createCustomer.jsp -->
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">

        <property name="prefix" value="/" />
        <property name="suffix" value=".jsp" />

    </bean>

</beans>

我在各种论坛上尝试过很多建议,但似乎没什么用。我很可能会错过一些basci步骤。任何人都可以看看我的代码并提出建议吗?

2 个答案:

答案 0 :(得分:1)

你的hibernateTemplate没有被注入b / c spring不知道它需要对MemberDaoImpl类做什么。您需要将其注释为@Component或@Repository,就像您使用@Controller一样。如你所知,它不是自动装配的相关目标。

答案 1 :(得分:0)

您的服务和存储库必须注释,避免通过XML声明它们

<强>第一

public class MemberDaoImpl implements MemberInterface{

@Repository
public class MemberDaoImpl implements MemberInterface{

<强>第二

不建议再使用HibernateTemplate,您可以直接在您的存储库中使用Hibernate SessionFactory

<强>第三

您必须使用@Transactional对@Service@Repository进行注释,因为Hibernate会要求提供事务支持。此外,还需要在配置中为transactionManager

定义一些额外的bean