Java Spring注释SessionFactoryBean NullPointerException

Java Spring注释SessionFactoryBean NullPointerException,java,spring,hibernate,Java,Spring,Hibernate,我是Spring的新手,尝试将Spring和Hibernate结合起来。但我在SessionFactory中面对的是空指针 错误说明: java.lang.NullPointerException com.ume.dao.UserDaoImpl.openSession(UserDaoImpl.java:29) com.ume.dao.UserDaoImpl.getUser(UserDaoImpl.java:34) com.ume.LoginController.getUserCredential

我是Spring的新手,尝试将Spring和Hibernate结合起来。但我在SessionFactory中面对的是空指针

错误说明:

java.lang.NullPointerException
com.ume.dao.UserDaoImpl.openSession(UserDaoImpl.java:29)
com.ume.dao.UserDaoImpl.getUser(UserDaoImpl.java:34)
com.ume.LoginController.getUserCredentials(LoginController.java:39)
另一个错误描述

org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
     org.springframework.orm.hibernate3.SpringSessionContext.currentSession(SpringSessionContext.java:63)
    org.hibernate.impl.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:544)
com.ume.dao.UserDaoImpl.openSession(UserDaoImpl.java:31)
com.ume.dao.UserDaoImpl.getUser(UserDaoImpl.java:37)
com.ume.service.UserServiceImpl.getUser(UserServiceImpl.java:23)
My 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:p="http://www.springframework.org/schema/p"
     xmlns:mvc="http://www.springframework.org/schema/mvc"
         xmlns:aop="http://www.springframework.org/schema/aop"
         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
           http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <context:property-placeholder location="classpath:jdbc.properties" />
<context:component-scan base-package="com.nptest" />
<mvc:annotation-driven />


  <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="viewClass"    value="org.springframework.web.servlet.view.JstlView" />
            <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>
    <bean class="org.springframework.context.support.ResourceBundleMessageSource"  id="messageSource">
    <property name="basename" value="messages" />
</bean>

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${database.driver}" />
    <property name="url" value="${database.url}" />
    <property name="username" value="${database.user}" />
    <property name="password" value="${database.password}" />
</bean>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="annotatedClasses">
        <list>
            <value>com.nptest.pojo.UserPojo</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">${hibernate.dialect}</prop>
            <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
        </props>
    </property>
</bean>

    <bean id="hibernateTransactionManager"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>     



</beans>
我的服务实现类

    @Repository
public class UserDaoImpl implements UserDao{

@Autowired
    private SessionFactory sessionFactory;

 private Session openSession()
 {

     return sessionFactory.getCurrentSession();
    }
     public UserPojo getUser(String username, String password) 
    {
        List<UserPojo> userlist=new ArrayList<UserPojo>();
        Query query=openSession().createQuery("from UserPojo u WHERE u.login=:username AND u.userpwd=:password");
        query.setParameter("login",username);
     query.setParameter("password",password);
        System.out.println("MADAN Query "+ query);
        userlist=query.list();
        if(userlist.size()>0)
            return userlist.get(0);
     else return null;

  }

}
    @Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Transactional
    public UserPojo getUser(String username, String password) {
        return userDao.getUser(username, password);
 }
在我进行这些调用的控制器类中,我通过以下方法实例化了服务类

@Autowired
private UserService userservice;
在我调用的方法中

 UserPojo user=userservice.getUser(username, password);

看起来您已经手动实例化了UserDaoImpl,sessionFactory没有连接,请发布如何使用UserDaoImpl的代码

将注释添加到UserDaoImpl并将其放在包com.nptest

package com.nptest.dao;

@Repository public class UserDaoImpl implements UserDao
然后在应用程序中自动连接UserDaoImpl

--编辑--

要启用基于注释的转换,必须添加

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


在应用程序上下文xml中

非常感谢。在添加了存储库和事务注释之后,我遇到了另一个异常。我已经更新了我的代码和异常。请查看。我想我们已经接近解决方案了。好伙伴!终于完成了!!
  <tx:annotation-driven transaction-manager="hibernateTransactionManager"/>