2

I'm using Spring 3 with Hibernate

But when I trying to save data in database it giving me nullPointerException

<pre>
java.lang.NullPointerException
    com.ivalix.services.LoginServiceImpl.add(LoginServiceImpl.java:27)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
</pre>

My dispatcher-servlet.xml code is:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context" 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/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

    <context:component-scan base-package="com.ivalix.controller" />

    <context:property-placeholder location="classpath:jdbc.properties" />

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

    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.ResourceBundleViewResolver"
        p:basename="views" />

    <bean id="loginService" class="com.ivalix.services.LoginServiceImpl" />

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close" p:driverClassName="${jdbc.driver}"
        p:url="${jdbc.url}" p:username="${jdbc.user}"
        p:password="${jdbc.password}" p:maxActive="0" p:initialSize="50" />



    <bean id="sessionFactory"

        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">

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

        <property name="annotatedClasses">

            <list>

                <value>com.ivalix.dto.User</value>

            </list>

        </property>

        <property name="hibernateProperties">

            <props>

                <prop key="hibernate.dialect">${jdbc.dialect}</prop>

                <prop key="hibernate.show_sql">${jdbc.show_sql}</prop>

            </props>

        </property>

    </bean>

    <bean id="hibernatetransactionManager"

        class="org.springframework.orm.hibernate3.HibernateTransactionManager">

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

    </bean>

    <bean id="tilesConfigurer"
        class="org.springframework.web.servlet.view.tiles2.TilesConfigurer"
        p:definitions="/WEB-INF/tiles-defs.xml" />

    <bean id="userValidator" class="com.ivalix.validator.UserValidator" />
    <bean id="messageSource"
        class="org.springframework.context.support.ResourceBundleMessageSource"
        p:basename="validation" />
</beans>

Controller file is:

<pre>

package com.ivalix.controller;


import org.springframework.beans.factory.annotation.Autowired;
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 org.springframework.web.bind.annotation.SessionAttributes;

import com.ivalix.services.LoginService;
import com.ivalix.validator.UserValidator;
import com.ivalix.dto.User;

@Controller
@RequestMapping("/login.htm")
@SessionAttributes("user")
public class LoginController {

    private LoginService loginService;
    private UserValidator userValidator;

    @Autowired
    public void setUserService(LoginService loginService, UserValidator userValidator) {
        this.loginService = loginService;
        this.userValidator = userValidator;
    }


    @RequestMapping(method = RequestMethod.GET)
    public String showUserForm(ModelMap model) {
        User user = new User();
        model.addAttribute("user", user);
        return "loginForm";
    }

    @RequestMapping(method = RequestMethod.POST)
    public String onSubmit(@ModelAttribute(value="user") User user,BindingResult result) {
        userValidator.validate(user, result);       
        if (result.hasErrors()) {
            return "loginForm";
        } else {
            loginService.add(user);
            return "redirectLoginSuccess";
        }
    }
}

</pre>

ServiceImpl is:

<pre>
package com.ivalix.services;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.ivalix.dao.UserDao;
import com.ivalix.dao.impl.UserDaoImpl;
import com.ivalix.dto.User;

@Transactional
public class LoginServiceImpl implements LoginService {

    @Autowired
    private static UserDao userDao;

    LoginServiceImpl() {
        //userDao = UserDaoImpl.getInstance();
    }


    @Transactional(propagation = Propagation.REQUIRED, readOnly = false)
    public void add(User user) {
        userDao.saveUser(user);
    }

}


</pre>

DaoImpl is:

<pre>
package com.ivalix.dao.impl;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.ivalix.dao.UserDao;
import com.ivalix.dto.User;

@Repository("userDao")
public class UserDaoImpl implements UserDao {

    @Autowired
    private SessionFactory sessionFactory;

    private static UserDao userDao;

    public static UserDao getInstance() {
        if (userDao != null)
            return userDao;
        else {
            userDao = new UserDaoImpl();
            return userDao;
        }
    }


    // To Save the user detail
    public void saveUser(User user) {

        sessionFactory.getCurrentSession().saveOrUpdate(user);
    }

    // To get list of all user
    @SuppressWarnings("unchecked")
    public List<User> listUsers() {    
        return (List<User>) sessionFactory.getCurrentSession().createCriteria(User.class).list();
    }

}
</pre>

UserValidator Bean:

package com.ivalix.validator;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

import com.ivalix.dto.User;


public class UserValidator implements Validator {

    @Override
    public boolean supports(Class<?> clazz) {
        return User.class.isAssignableFrom(clazz);
    }

    @Override
    public void validate(Object target, Errors errors) {
        User user = (User) target;  
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "email", "email.required");
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "password", "password.required");
        if (!isValidEmailAddress(user.getEmail())) errors.rejectValue("email",
                "email.invalid");  
    }

    public boolean isValidEmailAddress(String emailAddress){
        String expression="^[\\w\\-]([\\.\\w])+[\\w]+@([\\w\\-]+\\.)+[A-Z]{2,4}$";
        CharSequence inputStr = emailAddress;
        Pattern pattern = Pattern.compile(expression,Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(inputStr);
        return matcher.matches();
    }  
}

List of jar I used:

</pre>

- antlr-2.7.6.jar
- aopalliance.jar
- appserv-jstl.jar
- asm-1.5.3.jar
- asm-attrs-1.5.3.jar
- c3p0-0.9.1.jar
- cglib-2.1_3.jar
- cglib-nodep-2.1_3.jar
- commons-beanutils-1.8.3.jar
- commons-codec-1.3.jar
- commons-collections-3.2.1.jar
- commons-dbcp-1.4.jar
- commons-digester-2.1.jar
- commons-fileupload-1.2.2.jar
- commons-io-1.2.jar
- commons-lang-2.6.jar
- commons-logging-1.1.1.jar
- commons-pool-1.5.5.jar
- displaytag-1.2.jar
- displaytag-export-poi-1.2.jar
- displaytag-portlet-1.2.jar
- dom4j-1.6.1.jar
- ejb3-persistence-1.0.1.GA.jar
- hibernate-3.2.6.ga.jar
- hibernate-annotations-3.4.0.GA.jar
- hibernate-commons-annotations-3.1.0.GA.jar
- hibernate-core-3.3.2.GA.jar
- javassist-3.12.0.GA.jar
- jstl-1.2.jar
- jta-1.1.jar
- mysql-connector-java-5.1.6.jar
- org.apache.servicemix.bundles.commons-io-1.3.2_1.jar
- org.springframework.aop-3.0.5.RELEASE.jar
- org.springframework.asm-3.0.5.RELEASE.jar
- org.springframework.aspects-3.0.5.RELEASE.jar
- org.springframework.beans-3.0.5.RELEASE.jar
- org.springframework.context.support-3.0.5.RELEASE.jar
- org.springframework.context-3.0.5.RELEASE.jar
- org.springframework.core-3.0.5.RELEASE.jar
- org.springframework.expression-3.0.5.RELEASE.jar
- org.springframework.instrument.tomcat-3.0.5.RELEASE.jar
- org.springframework.instrument-3.0.5.RELEASE.jar
- org.springframework.jdbc-3.0.5.RELEASE.jar
- org.springframework.jms-3.0.5.RELEASE.jar
- org.springframework.orm-3.0.5.RELEASE.jar
- org.springframework.oxm-3.0.5.RELEASE.jar
- org.springframework.test-3.0.5.RELEASE.jar
- org.springframework.transaction-3.0.5.RELEASE.jar
- org.springframework.web.portlet-3.0.5.RELEASE.jar
- org.springframework.web.servlet-3.0.5.RELEASE.jar
- org.springframework.web.struts-3.0.5.RELEASE.jar
- org.springframework.web-3.0.5.RELEASE.jar
- servlet-api.jar
- slf4j-api-1.6.1.jar
- spring-modules-validation.jar
- spring-security-acl-3.0.5.RELEASE.jar
- spring-security-config-3.0.5.RELEASE.jar
- spring-security-core-3.0.5.RELEASE.jar
- spring-security-taglibs-3.0.5.RELEASE.jar
- spring-security-web-3.0.5.RELEASE.jar
- tiles-api-2.2.2.jar
- tiles-core-2.2.2.jar
- tiles-jsp-2.2.2.jar
- tiles-servlet-2.2.2.jar
- tiles-template-2.2.2.jar
- urlrewrite-2.6.0.jar
- validation-api-1.0.0.GA.jar
- xalan.jar
- xercesImpl-2.9.1.jar
- xml-apis.jar

</pre>
4

3 回答 3

4

感谢所有的朋友,

现在问题已解决,这是由于下面描述的多个更改

在 dispatcher-servlet.xml 中

改变

<context:component-scan base-package="com.ivalix.controller" />

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

并为 userDao 添加条目,因为我在 LoginServiceImpl 中使用 userDao 作为 @Autowired

<bean id="userDao" class="com.ivalix.dao.impl.UserDaoImpl" />

按照 Orid 的建议,在 loginController 中使用 @Autowired 注释删除 setUserService()

在 UserDaoImpl 中删除 getInstance() 方法,因为现在我正在使用注释创建 useDao 引用

再次感谢所有帮助我的建议者。非常感谢 :)

于 2013-08-28T04:29:10.233 回答
1

UserDao是空的,而不是会话工厂

更改context:component-scan base-package="com.ivalix.controllercontext:component-scan base-package="com.ivalix

现在,您只是在扫描/注入控制器包。这就是为什么你的 DAO 没有注入到你的服务中,因此是空的。

还要从您的弹簧配置中删除bean id="loginService"和。bean id="userValidator"

编辑

之后,改变

private LoginService loginService;
private UserValidator userValidator;

@Autowired
public void setUserService(LoginService loginService, UserValidator userValidator) {
    this.loginService = loginService;
    this.userValidator = userValidator;
}

 @Autowired
 private LoginService loginService;

 @Autowired
 private UserValidator userValidator;

消除您的第二个异常(请删除该setUserService方法或将其重构为两个单独的 setter 方法)

于 2013-08-27T14:02:48.813 回答
0

似乎有多个问题

更改组件扫描以包括 DAO 和服务包

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

使用@Service 注解来注解服务

@Service
@Transactional
public class LoginServiceImpl implements LoginService {

删除该getInstance()方法,UserDao因为它是错误的,因为它没有sessionFactory在 dao 实例中设置

于 2013-08-28T03:28:45.387 回答