Java getCurrentSession()导致NullPointerException

Java getCurrentSession()导致NullPointerException,java,spring,spring-framework-beans,Java,Spring,Spring Framework Beans,当我将文件作为JUnit测试运行时,UsersDAOImpl类应该添加一个新用户,但是当我调用sessionFactory.getCurrentSession()时,我一直得到一个NullPointerException,我不知道为什么 会话工厂放在beans.xml中 由于明显的原因,数据库的凭据被删除 [beans.XML] <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.spr

当我将文件作为JUnit测试运行时,UsersDAOImpl类应该添加一个新用户,但是当我调用sessionFactory.getCurrentSession()时,我一直得到一个NullPointerException,我不知道为什么

会话工厂放在beans.xml中

由于明显的原因,数据库的凭据被删除

[beans.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:tx="http://www.springframework.org/schema/tx"
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-4.2.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">
        <!-- enable autowiring -->
        <context:component-scan base-package="src"></context:component-scan>
        <!-- enable @Transactional -->
        <tx:annotation-driven />
        <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource">
            <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property>
            <property name="username" value="{Master}"></property>
            <property name="password" value="{password}"></property>
            <property name="url" value="{url}"></property>
        </bean>
        <bean id="mySessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
            <property name="dataSource" ref="myDataSource"></property>
            <property name="packagesToScan" value="src"></property>
            <property name="hibernateProperties">
                <props>
                    <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
                    <prop key="hibernate.show_sql">true</prop>
                    <prop key="hibernate.format_sql">true</prop>
                </props>
            </property>
        </bean>
        <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
            <property name="sessionFactory" ref="mySessionFactory">
    </property> 
                </bean>
        <bean id="usersDAO" class="dao.UsersDAOImpl">
            <property name="sessionFactory" ref="mySessionFactory">        </property>
        </bean>
     </beans>

null

每当第一次调用getCurrentSession()时,就会打开会话,并在事务结束时关闭会话。如果一个会话不存在,则创建一个全新的会话;如果一个会话已经存在,则使用现有的会话。它自动配置为“自动刷新”和“自动关闭”属性为true,这意味着会话将自动刷新和关闭

当事务长时间运行时,我们可以使用getCurrentSession()方法。要使用此方法,我们需要在hibernate.cfg.xml文件中配置一个属性。财产是

   <propertyname="hibernate.current_session_context_class">thread</property>
或者试试这个

Session session = sessionFactory.getCurrentSession();

if (session.isOpen()) {
    session.close();
}

TransactionSynchronizationManager.unbindResource(sessionFactory);
TransactionSynchronizationManager.clearSynchronization();

sessionFactory字段为空


至于原因,我只能猜测。您既可以自动连接sessionFactory字段,也可以通过其在applicationContext.xml中的配置将其作为属性注入。做一个或另一个,而不是两个,然后看看会发生什么。

假设您共享异常的完整堆栈跟踪。这可能会有帮助。假设您将此引用到与setter注入一起使用的
sessionFactory
中。另外,我在Dao类中没有看到
@Repository
。也请参考以下内容:您的课堂上为什么有
@Test
方法?这应该在一个单独的测试类中,该测试类使用Spring测试库加载上下文。你没有做任何这些,当然它将是空的。基本上你忽略了你的配置。当使用Spring时,不要乱动hibernate.current\u session\u context\u类。尤其不要将其设置为
thread
,因为这将破坏与Spring和声明性事务管理的正确集成。
   <propertyname="hibernate.current_session_context_class">thread</property>
 public org.hibernate.classic.Session getCurrentSession() throws HibernateException {
    if ( currentSessionContext == null ) {
       throw new HibernateException( "No CurrentSessionContext configured!" );
    }
    return currentSessionContext.currentSession();  }
Session session = sessionFactory.getCurrentSession();

if (session.isOpen()) {
    session.close();
}

TransactionSynchronizationManager.unbindResource(sessionFactory);
TransactionSynchronizationManager.clearSynchronization();