我正在使用带有Hibernate的Spring 3
但是当我尝试在数据库中保存数据时,它给了我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>
我的dispatcher-servlet.xml代码是:
<?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>
控制器文件是:
<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是:
<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是:
<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();
}
}
我使用的jar列表:
</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>
答案 0 :(得分: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" />
在loginController中,使用Orid建议的@Autowired注释删除setUserService()
在UserDaoImpl中删除getInstance()方法,因为现在我正在使用注释创建useDao引用
再次感谢所有建议者帮助我。非常感谢:)
答案 1 :(得分:1)
您的UserDao
为空,而不是会话工厂
将context:component-scan base-package="com.ivalix.controller
更改为context: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方法)
答案 2 :(得分:0)
似乎存在多个问题
更改组件扫描以包含DAO和服务包
<context:component-scan base-package="com.ivalix" />
使用@Service注释
注释服务@Service
@Transactional
public class LoginServiceImpl implements LoginService {
从getInstance()
移除UserDao
方法,因为它没有在dao实例中设置sessionFactory