【问题标题】:SessionFactory injection isn't workingSessionFactory 注入不起作用
【发布时间】:2013-03-26 10:12:15
【问题描述】:

我的 SessionFactory 没有被注入到 SessionFactory 变量中。我的配置如下:

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

    <mvc:annotation-driven/>

    <tx:annotation-driven/>

    <context:annotation-config/>

    <!-- Scans the package for contents -->
    <context:component-scan base-package="com.csu.library.mvc"/>

    <!-- Maps static resources like images, css, javascript files -->
    <mvc:resources location="/resources/" mapping="/resources/**"/>

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name = "viewClass" value = "org.springframework.web.servlet.view.JstlView"/>
        <property name = "prefix" value = "/WEB-INF/jsps/"/>
        <property name = "suffix" value = ".jsp"/>
    </bean>

    <bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/csu_library"/>
        <property name="username" value="csulibrary"/>
        <property name="password" value="csulibrary"/>

        <!-- Pool Properties -->
        <property name="initialSize" value="5" />
        <property name="maxIdle" value="5" />
        <property name="maxActive" value="10" />
    </bean>

    <bean id = "hibernateProperties" class = "java.util.Properties">
        <constructor-arg index="0">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.cglib.use_reflection_optimizer">true</prop>
                <prop key="hibernate.cache.provider_class">org.hibernate.cache.HashtableCacheProvider</prop>
            </props>
        </constructor-arg>
    </bean>

    <bean id = "sessionFactory" class = "org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="packagesToScan" value = "com.csu.library.mvc"/>
        <property name="dataSource" ref = "dataSource"/>        
        <property name="hibernateProperties" ref = "hibernateProperties"/>

    </bean>

    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name = "sessionFactory" ref = "sessionFactory"/>
    </bean>
</beans>

HibernateUtil.class

package com.csu.library.mvc.hibernate;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.orm.hibernate4.HibernateExceptionTranslator;
import org.springframework.orm.hibernate4.HibernateTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@org.springframework.context.annotation.Configuration
@EnableTransactionManagement
public class HibernateUtil {

    @Autowired
    @Qualifier("sessionFactory")
    private SessionFactory sessionFactory;

    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    @Bean
    public HibernateTransactionManager transactionManager() {
        return new HibernateTransactionManager(getSessionFactory());
    }

    @Bean
    public HibernateExceptionTranslator exceptionTranslator() {
        return new HibernateExceptionTranslator();
    }
}

调用 getSessionFactory() 方法时,程序会抛出 NullPointerException。显然, sessionFactory 没有被注入。程序启动良好。可能是什么问题?

【问题讨论】:

  • 你确定你的 Util 类在 com.csu.library.mvc 目录中?
  • 我想是的。 Util类的包名是com.csu.library.mvc.hibernate
  • 你的项目中Util类bean是如何创建的?
  • @code13 我已经添加了 HibernateUtil 类,它具有通过注入获取 SessionFactory 对象的 SessionFactory 变量。

标签: java hibernate session spring-mvc sessionfactory


【解决方案1】:

请注意,仅在相同的 bean 上查找注释 它在其中定义的应用程序上下文。这意味着,如果您输入 DispatcherServlet 的 WebApplicationContext,它只检查你的 @Autowired bean 控制器,而不是您的服务。

【讨论】:

    【解决方案2】:

    您需要使用org.springframework.beans.factory.annotation.Autowired 注释而不是javax.inject.Inject

    @Autowired
    private SessionFactory sessionFactory;
    

    【讨论】:

      【解决方案3】:

      像下面这样注入,它会正常工作

      @Autowired
      @Qualifier("sessionFactory")
      private SessionFactory sessionFactory;
      
      public Session getSession() {
          return sessionFactory.getCurrentSession();
      }
      

      希望能帮到你:)

      【讨论】:

      • 仍然抛出 NullPointerException
      • 请仔细看我的代码,SessionFactory的变量不是静态的。那你知道怎么做吗?
      • 我尝试进行更改。我已经从方法和变量中删除了“静态”,但 sessionFactory 仍然为空。请查看更新的 HibernateUtil 类。
      • 如何使用 HibernateUtil 类获取休眠会话工厂?新实例还是通过自动装配?
      • 我刚刚通过@Autowired private HibernateUtil util 尝试过;通过这种方式可以得到实例。
      【解决方案4】:

      在某些 Spring 版本中,不扫描子文件夹。 要么将你的类移动到包 com.csu.library.mvc 中,要么在扫描的包中添加包 com.csu.library.mvc.hibernate 并尝试一下。

      【讨论】:

      • 添加包和移动类都失败了?
      • 我将上述包添加到 base-package 属性中。没用。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-04
      相关资源
      最近更新 更多