gpt4 book ai didi

Spring休眠集成: Getting null in sessionFactory

转载 作者:行者123 更新时间:2023-12-02 21:51:53 24 4
gpt4 key购买 nike

我将 Spring 3 与 Hibernate 一起使用

但是当我尝试将数据保存在数据库中时,它给了我 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>

Controller 文件是:

<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>

用户验证器 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>

最佳答案

感谢所有 friend

问题已解决,这是由于多项更改所致,如下所述

在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 引用

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

关于Spring休眠集成: Getting null in sessionFactory,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18467537/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com