【问题标题】:Spring SessionFactory Injection IssueSpring SessionFactory 注入问题
【发布时间】:2011-07-03 20:05:14
【问题描述】:

我一直在尝试学习使用 Spring 3.0.x 的各种功能,当我尝试将会话工厂注入 DAO 实现时遇到了一个问题。当我尝试使用注入的 SessionFactory 实例变量时收到 NullPointerException,这让我相信问题存在于 bean 定义中。

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:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-2.5.xsd">

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

  <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 id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="oracle.jdbc.OracleDriver"/>
    <property name="url" value="jdbc:oracle:thin:@xxx.xxx.x.xxx:xxxx:xx"/>
    <property name="username" value="xxx"/>
    <property name="password" value="xxx"/>
  </bean>

  <bean id="mySessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="myDataSource"/>
    <property name="hibernateProperties">
        <value>
            hibernate.dialect=org.hibernate.dialect.Oracle10gDialect
        </value>
    </property>
  </bean>

</beans>

EntryImpl.java

package com.timerecorder.entity;

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

@Repository
public class EntryImpl implements EntryDao
{

  private SessionFactory sessionFactory;

  @Autowired
  public void setSessionFactory(SessionFactory sessionFactory) 
  {
      this.sessionFactory = sessionFactory;
  }

  @Override
  public boolean saveEntry(EntryForm efd) 
  {
      // Place the details from the entry form into the Entry entity
      Entry e = new Entry();
      e.setId(Long.valueOf(efd.getUserId()));

      this.sessionFactory.getCurrentSession().save(e);

      return true;
  }

  @Override
  public boolean removeEntry(Long id) 
  {
      throw new UnsupportedOperationException("Not supported yet.");
  }

  @Override
  public List getEntries(Long id) 
  {
      throw new UnsupportedOperationException("Not supported yet.");
  }

  @Override
  public Entry getEntry(Long id) 
  {
      return new Entry();
  }

}

任何帮助将不胜感激。

亲切的问候,

贾斯汀

【问题讨论】:

    标签: java hibernate spring spring-mvc dependency-injection


    【解决方案1】:

    看起来这可能是您的 bean 定义中的命名约定问题。

    尝试重命名

    id= "mySessionFactory"

    id= "sessionFactory"
    

    如果有帮助,请告诉我。

    【讨论】:

      【解决方案2】:

      看起来您正在按名称自动装配,但您需要按类型自动装配 sessionFactory - 或者您可以将 bean 定义重命名为 sessionFactory,它应该可以正常工作。

      此外,您需要确保您的 bean 脱离了应用程序上下文,而不仅仅是新的。

      【讨论】:

      • 感谢您的快速回复,但我已经尝试过这种可能性,虽然它可能是问题的一部分,但并不能解决整体问题。
      • 忘了问您是否可以详细说明您的回复的第二部分,因为我没有遵循我应该做的事情。再次感谢:-)
      • 好吧,通常你会这样做: ApplicationContext context = new ClassPathXmlApplicationContext("classpath:/beans.xml");然后当你想要一个bean的实例时,你会去 context.getBean("entryImpl");这将从应用程序上下文中获取正确构造的 bean。
      • Dataknife 建议的上述技术不是从容器中获取 Spring bean 的推荐方法。根据 IoC 原则,使用 @Autowired 是一种正确的方法。另一种方法只是将 Spring 视为一个大工厂,不推荐。
      【解决方案3】:

      尝试使用 init() 方法自动装配会话工厂。

      这就是我的工作方式->

      @Repository
      public class UserDaoImpl extends HibernateDaoSupport implements UserDao {
          @Autowired
          public void init(SessionFactory factory) {
              setSessionFactory(factory);
          }
      }
      

      setSessionFactory 是 HibernateDaoSupport 提供的方法

      【讨论】:

        猜你喜欢
        • 2011-07-14
        • 1970-01-01
        • 2013-12-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多