java - SessionFactory 注入(inject)不起作用

标签 java hibernate session spring-mvc sessionfactory

我的 SessionFactory 没有被注入(inject)到 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 没有被注入(inject)。程序启动正常。可能是什么问题?

最佳答案

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

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

public Session getSession() {
    return sessionFactory.getCurrentSession();
}

希望能帮到你:)

关于java - SessionFactory 注入(inject)不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15809476/

相关文章:

session - session 的最佳实践( gorilla / session )

java - Eclipse RCP 应用程序 Activity 标题栏

java - jSTL/jsp - 迭代 bean vector

java - 我应该使用哪个 LayoutManager?

java - Spring 中的 Hibernate - 使用子查询进行删除

java - 如何使用 FetchType.EAGER

nhibernate - 在与 nHibernate 的关系中保持图的一致性

java - 如何测试Hibernate Session是否打开?

security - 客户端-服务器和 Web 应用程序之间的身份验证

java - 当我调用不同的包时,如何在 Java 中返回多个对象类型(String、int、int)?